<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <title>添加用户</title>
    <%@include file="/views/common.jsp" %>
    <link rel="stylesheet" href="${path}/static/css/formSelects-v4.css">
    <style>
        .layui-input, .layui-input-inline {
            width: 350px
        }
    </style>
</head>
<body>
<div class="layui-input-block layui-form" lay-filter="addUser"
     style="margin-top: 30px">
    <form class="layui-form " action="">
        <input type="hidden" value="1" name="sysStatus"/>
        <div class="layui-form-item">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="username" lay-verify="username"
                       placeholder="请输入用户名" class="layui-input" id="username">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">密码</label>
            <div class="layui-input-inline">
                <input type="password" name="password" placeholder="请输入密码"
                       class="layui-input" id="userpwd" lay-verify="userpwd">
            </div>
            <!-- <div class="layui-form-mid layui-word-aux">请务必填写用户名</div> -->
        </div>

        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">真实姓名</label>
                <div class="layui-input-inline">
                    <input type="text" name="realname" id="realname" placeholder="请输入真实姓名"
                           class="layui-input" lay-verify="required">
                </div>
            </div>
        </div>
        <label class="layui-form-label">角色</label>
        <div class="layui-input-inline">
            <div id="role" style="width: 350px;"></div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">状态</label>
            <div class="layui-input-block" id="idSuper">
                <input type="radio" name="userStatus" value="1" title="启用" checked="">
                <input type="radio" name="userStatus" value="0" title="禁用">
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="enadd">确认添加</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script>
    //全局定义一次, 加载formSelects
    layui.config({
        base: '${path}/static/js/' //js文件的路径
    }).extend({
        formSelects: 'formSelects-v4',
        xmSelect: 'xm-select'
    });

    layui.use(['form', 'layer', 'formSelects', 'xmSelect'], function () {
        var $ = layui.$,
            form = layui.form,
            layer = layui.layer,
            formSelects = layui.formSelects,
            xmSelect = layui.xmSelect;

        //自定义验证规则
        form.verify({
            username: function (value, item) { //value：表单的值、item：表单的DOM对象
                if (!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)) {
                    return '用户名不能有特殊字符';
                }
            },
            userpwd: [/^[\S]{3,12}$/, '密码必须3到12位，且不能出现空格'],
            realname: [/^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$/, '姓名不能有特殊字符']
        });
        $(document).ready(function () {
            $('#username').blur(function () {
                var reg = /^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$/;
                var con = $(this).val();
                var result = reg.test(con);
                if (result) {
                    var param = {
                        username: $("#username").val()
                    }
                    $.getJSON("${path}/checkUsername", param, function (data) {
                        if (data.code == 200) {
                            layer.tips('用户名符合规范', '#username', {tips: [2, '#009688'], time: 1500});
                        } else {
                            layer.tips('用户名被占用，请重新输入', '#username', {tips: [2, '#009688'], time: 0});
                        }
                    })
                } else {
                    layer.tips('用户名不能有特殊字符', '#username', {tips: [2, '#009688'], time: 0});
                }
            });
            $('#userpwd').blur(function () {
                var reg = /^[\S]{3,12}$/;
                var con = $(this).val();
                var result = reg.test(con);
                if (result) {
                    layer.tips('密码符合规范', '#userpwd', {tips: [2, '#009688'], time: 1500});
                } else {
                    layer.tips('密码必须3到12位，且不能出现空格', '#userpwd', {tips: [2, '#009688'], time: 0});
                }
            });
            $('#realname').blur(
                function () {
                    var reg = /^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$/;
                    var con = $(this).val();
                    var result = reg.test(con);
                    if (!result) {
                        layer.tips('姓名中不能有特殊字符', '#realname', {tips: [2, '#009688'], time: 0});
                    }
                });
        })

        //监听指定开关
        form.on('switch(switchTest)', function (data) {
            layer.msg('启用管理员：'
                + (this.checked ? 'true' : 'false'), {
                offset: '6px'
            });
        });

        //监听提交
        form.on('submit(enadd)', function (data) {
            $.ajax({
                url: '${path}/addUser',
                dataType: 'json',
                data: $(data.form).serialize(),
                type: 'post',
                success: function (data) {
                    if (data.code == 200) {
                        layer.msg('录入成功！', {
                            icon: 6,
                            offset: "auto",
                            time: 2000
                        });//提示框
                    } else {
                        layer.msg('录入失败！', {
                            icon: 5,
                            offset: "auto",
                            time: 2000
                        });//提示框
                    }
                    setTimeout(function () {
                        var index = parent.layer.getFrameIndex(window.name);//获取窗口索引
                        parent.layer.close(index);//关闭弹出层
                        parent.layui.table.reload("userTableAll"); //重新加载页面表格
                    }, 2100);
                }
            })
            return false;
        });
        // 获取角色
        $.ajax({
            // 获取所有角色
            // url : '${path}/getRoleList',
            // 获取用户拥有的角色
            url: '${path}/getUserRoles',
            dataType: 'json',
            data: {},
            type: 'post',
            success: function (data) {
                // 获取所有角色
                // var datas = data.data.allRoleList
                // 获取用户拥有的角色
                var datas = data.data.userRoleList
                var dataArr = [];
                $.each(datas, function (index, item) {
                    var data1 = {};
                    data1.name = item.roleName;
                    data1.value = item.roleId;
                    dataArr.push(data1)
                });
                var role = xmSelect.render({
                    el: '#role',
                    data: dataArr, 		//数据
                    name: 'role',		//表单提交的name属性
                    repeat: true,
                    toolbar: {
                        show: true		//开启头部工具栏
                    },
                    height: '200px',	//高度
                    paging: true,		//分页
                    filterable: true,	//搜索
                    searchTips: '搜索店铺名称',
                    max: 10,			//选择上限
                    autoRow: true,		//自动换行
                    direction: 'down',	//方向
                })
            }
        });
    })
</script>
</body>
</html>