<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
<head>
    <meta charset="UTF-8">
    <title>修改密码</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css">
</head>
<body>
<div class="row">
    <div class="layui-col-md12">
        <div class="layui-card">
            <div class="layui-card-header"><span><shiro:principal property="userName"/></span></div>
            <div class="layui-card-body">
                <form class="layui-form" action="/admin/user/updatePwd" method="post">
                    <div class="layui-form-item">
                        <label class="layui-form-label">当前密码</label>
                        <div class="layui-input-inline">
                            <input type="password" name="oldPassword" lay-verify="required" lay-vertype="tips"
                                   class="layui-input">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">新密码</label>
                        <div class="layui-input-inline">
                            <input type="password" id="np" name="password" lay-verify="pass" lay-vertype="tips"
                                   autocomplete="off" class="layui-input layui-form-danger">
                        </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="repass" 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="altPwd">确认修改</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
<script src="/plugins/layui/layui.js"></script>
<script type="text/javascript">
    layui.use(['element', 'form'], function () {
        $ = layui.jquery;
        var form = layui.form;
        //表单校验
        form.verify({
            // 校验原始密码
            pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ],
            repass: function (value) {
                var pass = $('#np').val();
                if (value != pass) {
                    return '两次密码不一致';
                }
            },
        });
        //监听提交
        form.on('submit(altPwd)', function (data) {
            var headers = {};
            headers['X-CSRF-TOKEN'] = "[[${_csrf.token}]]";
            $.ajax({
                url: data.form.action,
                type: data.form.method,
                headers: headers,
                data: data.field,
                dataType: 'json',
                success: function (data) {
                    if (data.code == 0) {
                        layer.msg(data.msg, {icon: 1, time: 600}, function () {
                            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                            parent.location.reload();
                            parent.layer.close(index);
                        });
                    } else {
                        layer.msg(data.msg, {time: 1000});
                    }
                }
            });
            return false;
        });
    });
</script>
</body>
</html>