<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>后台管理系统</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <link rel="shortcut icon" th:href="@{/images/logo.jpg}"/>
    <link rel="stylesheet" th:href="@{/js/layui/css/layui.css}">
    <link rel="stylesheet" th:href="@{/js/layui/css/admin.css}">
    <script type="text/javascript" th:src="@{/js/jquery-3.3.1.min.js}"></script>
    <script th:src="@{/js/layui/layui.js}" charset="utf-8"></script>
</head>
<body>
<div class="layui-form" style="padding: 20px 0 0 0;">
    <form id="formId" onsubmit="return false;">
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>管理员账号</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="adminAccount" placeholder="请输入管理员账号"
                       th:value="${obj.adminAccount}">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="content-red">*</span>账号长度6~18位
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" class="layui-input" name="adminPassword">
            </div>
            <div class="layui-form-mid layui-word-aux">
                <span class="content-red">*</span>需要修改时填写,密码长度6~18位
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>管理员姓名</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="adminName" placeholder="请输入管理员姓名"
                       th:value="${obj.adminName}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span style="color: red">*</span>管理员角色</label>
            <div class="layui-input-inline">
                <select name="roleId" lay-filter="roleId" lay-search>
                    <option value="">请选择角色</option>
                    <option th:each="list:${roleList}" th:value="${list.roleId}" th:text="${list.roleName }"
                            th:selected="${obj.roleId}==${list.roleId}"></option>
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">联系电话</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="adminPhone" placeholder="请输入联系电话" style="width: 600px"
                       th:value="${obj.adminPhone}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">地址</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="adminAddress" style="width: 600px"
                       th:value="${obj.adminAddress}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="adminRemark" style="width: 600px"
                       th:value="${obj.adminRemark}">
            </div>
        </div>
        <button class="layui-btn" style="display: none;" id="saveBtn" onclick="save()">保存</button>
    </form>
</div>
<script>

    layui.use(['table', 'laydate', 'form'], function () {
        var form = layui.form;
        form.render('select');
        form.render();
    });

    function save() {
        var adminAccount = $("#formId input[name='adminAccount']").val();
        if (adminAccount == '') {
            layer.msg("管理员帐号不能为空!");
            return false;
        }
        if (adminAccount.length < 6 || adminAccount.length > 18) {
            layer.msg("管理员帐号长度不符!");
            return false;
        }
        var adminPassword = $("#formId input[name='adminPassword']").val();
        if (adminPassword != '' && (adminPassword.length < 6 || adminPassword.length > 18)) {
            layer.msg("密码长度不符!");
            return false;
        }
        if ($("#formId input[name='adminName']").val() == '') {
            layer.msg("管理员姓名不能为空!");
            return false;
        }
        if ($("#formId select[name='roleId']").val() == '') {
            layer.msg("管理员角色不能为空!");
            return false;
        }

        var form = document.querySelector("#formId");
        var formdata = new FormData(form);
        formdata.append("adminId", '[[${obj.adminId}]]');
        $.ajax({
            url: "edit",
            data: formdata,
            type: "POST",
            dataType: "json",
            processData: false,
            contentType: false,
            success: function (result) {
                if (result.code == 100) {
                    layer.msg(result.msg, {icon: 6, time: 1000}, function () {
                        closeLayer();
                        parent.layui.table.reload('tableId');
                    });
                } else {
                    layer.msg(result.msg, {icon: 5});
                }
            }
        });
    }

    function closeLayer() {
        var index = parent.layer.getFrameIndex(window.name);
        //关闭当前frame
        parent.layer.close(index);
    }

</script>
</body>

</html>