<!DOCTYPE html>
<html lang="zh" 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">
    <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="./layui/css/layui.css" th:href="@{/layui/css/layui.css}" media="all">
    <link rel="stylesheet" href="./style/admin.css" th:href="@{/style/admin.css}" media="all">
    <link rel="stylesheet" href="./style/myself.css" th:href="@{/style/myself.css}" media="all">
    <!-- 让IE8/9支持媒体查询，从而兼容栅格 -->
    <!--[if lt IE 9]>
    <script src="./js/html5.min.js" th:src="@{/js/html5.min.js}"></script>
    <script src="./js/respond.min.js" th:src="@{/js/respond.min.js}"></script>
    <![endif]-->
    <script type="text/javascript" charset="utf-8" th:inline="javascript">
        var ctxPath = /*[[@{/}]]*/'';
    </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-header">修改密码</div>
                <div class="layui-card-body" pad15>
                    <form class="layui-form" lay-filter="">
                        <div class="layui-form-item">
                            <label class="layui-form-label">当前密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="oldPassword" lay-verify="oldPassword" lay-verType="tips"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">新密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="password" lay-verify="password" lay-verType="tips"
                                       autocomplete="off" id="LAY_password" class="layui-input">
                            </div>
                            <div class="layui-form-mid layui-word-aux">6到16个字符</div>
                        </div>
                        <div class="layui-form-item">
                            <label class="layui-form-label">确认新密码</label>
                            <div class="layui-input-inline">
                                <input type="password" name="repassword" lay-verify="repassword" lay-verType="tips"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-form-item">
                            <div class="layui-input-block">
                                <button class="layui-btn" lay-submit lay-filter="userPassword">确认修改</button>
                            </div>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script src="./js/common.js" th:src="@{/js/common.js}" charset="utf-8"></script>
<script src="./js/jquery.min.js" th:src="@{/js/jquery.min.js}" charset="utf-8"></script>
<script src="./js/jquery.base64.js" th:src="@{/js/jquery.base64.js}" charset="utf-8"></script>
<script src="./layui/layui.js" th:src="@{/layui/layui.js}" charset="utf-8"></script>
<script charset="utf-8" th:inline="javascript">
    layui.config({
        base: ctxPath //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'form', 'element', 'layer'], function () {
        var admin = layui.admin;
        var form = layui.form;
        form.render();
        //自定义验证规则
        var reg = /^[\S]{6,30}$/;
        form.verify({
            oldPassword: function (value) {
                if (!reg.test(value)) {
                    return '密码必须6到30位，且不能出现空格'
                }
            },
            password: function (value) {
                if (!reg.test(value)) {
                    return '密码必须6到30位，且不能出现空格'
                }
            },
            repassword: function (value) {
                if (!reg.test(value)) {
                    return '密码必须6到30位，且不能出现空格'
                }
            }
        });
        // 监听密码提交
        form.on('submit(userPassword)', function (data) {
            var field = data.field;
            var oldPassword = field.oldPassword;
            var password = field.password;
            var repassword = field.repassword;
            // 两次密码输入不相等
            if (password !== repassword) {
                layer.msg('两次输入的新密码不一致，请重新输入！', {icon: 5, shift: 6});
                return false;
            }
            // 修改密码
            admin.req({
                type: 'put',
                url: ctxPath + 'myself/update-password',
                data: {oldPassword: $.base64.encode(oldPassword), password: $.base64.encode(password)},
                dataType: 'json',
                headers: {
                    [[${_csrf.headerName}]]: [[${_csrf.token}]]
                },
                success: function (result) {
                    var data = result.data;
                    if (data === webConst.VERIFY_FAIL) {
                        layer.msg('当前密码输入不正确，请重新输入！', {icon: 5, shift: 6});
                    } else if (data === webConst.SUCCESS) {
                        layer.msg('密码修改成功！', {icon: 6});
                    } else {
                        layer.msg('密码修改失败！', {icon: 5, shift: 6});
                    }
                },
                error: function () {
                    layer.msg('系统错误！', {icon: 5, shift: 6});
                }
            });
            return false;
        });
    });
</script>
</body>
</html>