<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <base th:href="${#request.getContextPath() + '/'}">
    <meta charset="utf-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=0">
    <link rel="stylesheet" href="assets/fonts/font-awesome/css/font-awesome.css"/>
    <link rel="stylesheet" href="assets/layuiadmin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="assets/layuiadmin/style/admin.css" media="all">
</head>
<body>
<div class="layui-fluid layui-anim layui-anim-fadein">
    <div class="layui-card">
        <div class="layui-card-header">
            <span class="layui-breadcrumb" lay-filter="breadcrumb" style="visibility: visible;">
              <a class="close" href="javascript:void(0);">上级列表</a><span lay-separator="">/</span>
              <a href="javascript:void(0);"><cite>编辑内容</cite></a>
            </span>
        </div>
        <div class="layui-card-body" style="padding: 15px;">
            <form class="layui-form layui-form-pane" lay-filter="dataForm">
                <div class="layui-row">
                    <div class="layui-col-md6 layui-col-sm8 layui-col-xs12">
                        <div class="layui-form-item">
                            <label class="layui-form-label">用户名 *</label>
                            <div class="layui-input-block">
                                <input type="text" name="userUsername" lay-verify="userUsername"  placeholder="请输入用户名" class="layui-input" readonly>
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">新密码</label>
                            <div class="layui-input-block">
                                <input type="password" name="userPassword" placeholder="修改密码，请输入新密码，不修改为空" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">昵称 *</label>
                            <div class="layui-input-block">
                                <input type="text" name="userName" lay-verify="required"  placeholder="请输入昵称" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item" pane>
                            <label class="layui-form-label">类型 *</label>
                            <div class="layui-input-block">
                                <input type="radio" name="userType" value="1" title="系统用户">
                                <input type="radio" name="userType" value="2" title="普通用户">
                                <input type="radio" name="userType" value="3" title="公共用户">
                            </div>
                        </div>
                        <blockquote class="layui-elem-quote layui-quote-nm">
                            <div><b>类型说明</b></div>
                            <div>系统用户：具有后台登录权限且参与后台、APP业务的用户</div>
                            <div>普通用户：具有APP登录权限且参与APP业务的用户</div>
                            <div>公共用户：来自小程序、公众号等公共平台自动创建且参与此类业务的的用户</div>
                        </blockquote>
                        <div class="layui-form-item" pane>
                            <label class="layui-form-label">状态 *</label>
                            <div class="layui-input-block">
                                <input type="radio" name="userState" value="0" title="正常">
                                <input type="radio" name="userState" value="1" title="锁定">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">手机</label>
                            <div class="layui-input-block">
                                <input type="number" name="userPhone" placeholder="请输入手机" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">邮箱</label>
                            <div class="layui-input-block">
                                <input type="text" name="userEmail" placeholder="请输入邮箱" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">过期时间</label>
                            <div class="layui-input-block">
                                <input type="text" name="userExpiredDate" id="userExpiredDate" placeholder="请选择过期日期，空表示永不过期" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">备注</label>
                            <div class="layui-input-block">
                                <input type="text" name="remarks" placeholder="请输入备注" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-col-md2 layui-col-sm4 layui-col-xs12">
                        <div class="layui-form-item" style="text-align: center;">
                            <img id="avatarImage" src="assets/images/profile-photo.jpg" title="点击修改头像">
                            <input type="hidden" id="userAvatar" name="userAvatar">
                        </div>
                    </div>
                </div>
                <div class="layui-form-item layui-layout-admin">
                    <div class="layui-input-block">
                        <div class="layui-footer" style="left: 0;">
                            <button type="button" class="layui-btn" lay-submit lay-filter="submitForm">提交编辑</button>
                            <button type="button" class="layui-btn layui-btn-primary close">返回上级</button>
                        </div>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<script src="assets/layuiadmin/layui/layui.js"></script>
<script>
    layui.config({
        base: 'assets/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'laydate'], function(){
        var $ = layui.$;
        var laydate = layui.laydate;
        var form = layui.form;
        var userId = top.restAjax.params(window.location.href).userId;

        function closeBox() {
            parent.layer.close(parent.layer.getFrameIndex(window.name));
        }

        function initDate() {
            laydate.render({
                elem: '#userExpiredDate',
                trigger: 'click',
                format: 'yyyy-MM-dd HH:mm:ss'
            });
        }
        initDate();

        // 初始化
        function initData() {
            var self = this;
            var loadLayerIndex;
            top.restAjax.get(top.restAjax.path('api/user/get/{userId}', [userId]), {}, null, function(code, data) {
                form.val('dataForm', {
                    userAvatar: data.userAvatar,
                    userUsername: data.userUsername,
                    userName: data.userName,
                    userType: data.userType +'',
                    userState: data.userState +'',
                    userPhone: data.userPhone,
                    userEmail: data.userEmail.toString(),
                    userExpiredDate: data.userExpiredDate
                });
                form.render(null, 'dataForm');
                if($('#userAvatar').val() != '') {
                    $('#avatarImage').attr('src', 'route/file/download/true/' + $('#userAvatar').val());
                } else {
                    $('#userAvatar').val($('#userAvatar').val());
                }
            }, function(code, data) {
                top.dialog.msg(data.msg);
            }, function() {
                loadLayerIndex = top.dialog.msg(top.dataMessage.loading, {icon: 16, time: 0, shade: 0.3});
            }, function() {
                top.dialog.close(loadLayerIndex);
            });
        }
        initData();
        // 提交表单
        form.on('submit(submitForm)', function(formData) {
            var authorizedGrantTypes = top.restAjax.checkBoxToString(formData.field, 'authorizedGrantTypes');
            formData.field.authorizedGrantTypes = authorizedGrantTypes;
            top.dialog.confirm(top.dataMessage.commit, function(index) {
                top.dialog.close(index);
                var loadLayerIndex;
                top.restAjax.put(top.restAjax.path('api/user/update/{userId}', [userId]), formData.field, null, function(code, data) {
                    var layerIndex = top.dialog.msg(top.dataMessage.commitSuccess, {
                        time: 0,
                        btn: [top.dataMessage.button.yes, top.dataMessage.button.no],
                        shade: 0.3,
                        yes: function(index) {
                            top.dialog.close(index);
                            window.location.reload();
                        },
                        btn2: function() {
                            closeBox();
                        }
                    });
                }, function(code, data) {
                    top.dialog.msg(data.msg);
                }, function() {
                    loadLayerIndex = top.dialog.msg(top.dataMessage.committing, {icon: 16, time: 0, shade: 0.3});
                }, function() {
                    top.dialog.close(loadLayerIndex);
                });
            });
            return false;
        });
        form.verify({
            userUsername: function(value, item) {
                if(!new RegExp('^[a-zA-Z0-9_\\s]+$').test(value)) {
                    return '用户名不能有特殊字符';
                }
                if(/(^\_)|(\__)|(\_+$)/.test(value)){
                    return '用户名首尾不能出现下划线\'_\'';
                }
            }
        });

        $('#avatarImage').on('click', function() {
            top.dialog.open({
                url: 'route/file/uploadimage?fileId='+ $('#userAvatar').val(),
                title: '上传头像',
                width: '800px',
                height: '470px',
                onClose: function() {
                    var uploadImage = top.dialog.dialogData.uploadImage;
                    if(typeof(uploadImage) != 'undefined' && null != uploadImage && '' != uploadImage) {
                        $('#avatarImage').attr('src', 'route/file/download/true/' + uploadImage);
                        $('#userAvatar').val(uploadImage);
                    }
                }
            })
        });
        $('.close').on('click', function() {
            closeBox();
        });
    });
</script>
</body>
</html>