<html>
<head>
    <title>新建/编辑 用户</title>
    <% include("base/meta.html"){} %>

    <script src="/js/md5.js"></script>


</head>
<body>
<div class="layui-fluid">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <div class="layui-card-body ">
                    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
                        <legend>新建/编辑 用户</legend>
                    </fieldset>
                </div>
                <div class="layui-card-body ">
                    <form class="layui-form layui-form-pane" action="" id="editUserForm">
                        <input type="hidden" name="id" id="id" value="${editData.userId!}">

                        <div class="layui-form-item">
                            <input type="text" name="ename" lay-verify="ename" autocomplete="off" placeholder="请输入账号"
                                   class="layui-input" value="${editData.ename!}" <%if(editData!=null){%> disabled <%}%>>
                        </div>

                        <div class="layui-form-item">
                            <input type="text" name="cname" lay-verify="cname" autocomplete="off" placeholder="请输入昵称"
                                   class="layui-input" value="${editData.cname!}">
                        </div>

                        <div class="layui-form-item">
                            <label for="roleId" class="layui-form-label" style="width:140px;">用户角色</label>
                            <div class="layui-input-inline">


                                <%if(isNotEmpty(roles)){%>
                                <select name="roleId">
                                    <option value="">-请选择-</option>

                                    <% for(data in roles){ %>

                                        <%if(editData==null){%>
                                            <option value="${data.id}">${data.cname}</option>
                                        <%}else{%>
                                            <%if(data.id==editData.roleId){%>
                                                <option value="${data.id}" selected>${data.cname}</option>
                                            <%}else{%>
                                                <option value="${data.id}">${data.cname}</option>
                                            <%}%>
                                        <%}%>

                                    <%} %>
                                </select>
                                <%}%>

                            </div>
                        </div>


                        <div class="layui-form-item">
                            <input type="text" name="mobile" lay-verify="mobile" autocomplete="off" placeholder="请输入手机号"
                                   class="layui-input" value="${editData.mobile!}">
                        </div>


                        <div class="layui-form-item">
                            <input type="email" name="email" lay-verify="email" autocomplete="off" placeholder="请输入邮箱"
                                   class="layui-input" value="${editData.email!}">
                        </div>


                        <div class="layui-form-item">
                            <input type="text" name="twitter" lay-verify="twitter" autocomplete="off"
                                   placeholder="请输入twitter"
                                   class="layui-input" value="${editData.twitter!}">
                        </div>


                        <div class="layui-form-item">
                            <input type="text" name="facebook" lay-verify="facebook" autocomplete="off"
                                   placeholder="请输入facebook"
                                   class="layui-input" value="${editData.facebook!}">
                        </div>


                        <div class="layui-form-item">
                            <input type="text" name="school" lay-verify="school" autocomplete="off"
                                   placeholder="请输入学校名称"
                                   class="layui-input" value="${editData.school!}">
                        </div>

                        <div class="layui-form-item">
                            <input type="text" name="weibo" lay-verify="weibo" autocomplete="off" placeholder="请输入微博账号"
                                   class="layui-input" value="${editData.weibo!}">
                        </div>


                        <div class="layui-form-item">
                            <input type="text" name="wechat" lay-verify="wechat" autocomplete="off"
                                   placeholder="请输入微信账号"
                                   class="layui-input" value="${editData.wechat!}">
                        </div>


                        <div class="layui-form-item">
                            <input type="text" name="qq" lay-verify="qq" autocomplete="off" placeholder="请输入QQ账号"
                                   class="layui-input" value="${editData.qq!}">
                        </div>

                        <%if(editData==null){%>
                        <div class="layui-form-item">
                            <input type="password" name="password" lay-verify="password" autocomplete="off"
                                   placeholder="请输入密码" autocomplete='password_cl_autoc'
                                   class="layui-input">
                        </div>
                        <%}%>


                        <div class="layui-form-item">
                            <button type="button" class="layui-btn layui-btn-normal" lay-submit lay-filter="save">保存
                            </button>
                        </div>

                    </form>
                </div>
            </div>
        </div>
    </div>
</div>

<script>


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


        //监听提交
        form.on('submit(save)', function (data) {
            var lastData = data.field;

            if (lastData.password) {
                lastData.password = hex_md5(lastData.password);
            }

            if (!lastData.ename) {
                layer.msg("用户名不能为空", {
                    time: 2000
                });
                return;
            }

            if (!lastData.cname) {
                layer.msg("昵称不能为空", {
                    time: 2000
                });
                return;
            }

            if (!lastData.roleId) {
                layer.msg("角色不能为空", {
                    time: 2000
                });
                return;
            }

            if (!lastData.email) {
                layer.msg("邮箱不能为空", {
                    time: 2000
                });
                return;
            }

            $.ajax({
                cache: false,
                type: "POST",
                contentType: 'application/json',
                url: "/user/addup",
                data: JSON.stringify(lastData),
                async: false,
                success: function (data) {
                    if (data.code === 0) {
                        layer.msg(data.message, {
                            time: 2000
                        });

                        self.setTimeout(function () {
                            $("#editUserForm")[0].reset();
                            layui.form.render();
                            try {
                                parent.layui.table.reload('userList');
                            } catch (e) {

                            }
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        }, 2000);

                    } else {
                        layer.msg(data.message);
                    }
                },
                error: function (data) {
                    layer.msg(data.message);
                }
            });

            return false;
        });
    });
</script>
</body>
</html>