<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>修改密码</title>
    <link rel="stylesheet" href="{{ asset('CmsProUi/component/pear/css/pear.css') }}" />
    <link rel="stylesheet" href="{{ asset('CmsProUi/font-awesome/4.7.0/css/font-awesome.min.css') }}" />
    <link rel="stylesheet" href="{{ asset('Admin/css/admin.css') }}" />
    <link rel="stylesheet" href="{{ asset('Admin/css/variables.css') }}" />
    <link rel="stylesheet" href="{{ asset('Admin/css/reset.css') }}" />
</head>
<style>
    .layui-form-label {
        padding: 9px 4px;
        width: unset;
    }
    .layui-input {
        width: 95%;
    }
</style>

<body style="padding: 10px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">旧密码</label>
            <div class="layui-input-block">
                <input type="password" name="old_password" lay-verify="required" lay-reqText="旧密码不能为空" placeholder="请输入旧密码" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">新密码</label>
            <div class="layui-input-block">
                <input type="password" name="new_password" lay-verify="required" lay-reqText="新密码不能为空" placeholder="请输入新密码（6-20位）" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">确认密码</label>
            <div class="layui-input-block">
                <input type="password" name="confirm_password" lay-verify="required" lay-reqText="确认密码不能为空" placeholder="请再次输入新密码" class="layui-input" autocomplete="off">
            </div>
        </div>
        <div class="bottom">
            <div class="button-container">
                <button type="submit" class="layui-btn layui-btn-normal layui-btn-sm" lay-submit lay-filter="save">
                    <i class="layui-icon layui-icon-ok"></i> 提交
                </button>
                <button type="reset" class="layui-btn layui-btn-primary layui-btn-sm">
                    <i class="layui-icon layui-icon-refresh"></i> 重置
                </button>
            </div>
        </div>
    </form>

    <script src="{{ asset('CmsProUi/component/layui/layui.js') }}"></script>
    <script src="{{ asset('CmsProUi/component/pear/pear.js') }}"></script>
    <script>
        layui.use(['form', 'jquery'], function() {
            var form = layui.form;
            var $ = layui.jquery;

            $.ajaxSetup({
                headers: {
                    'X-CSRF-TOKEN': '{{ csrf_token() }}'
                },
                statusCode: {
                    401: function() {
                        layer.msg('登录已过期，请重新登录', {icon: 2, time: 1500}, function() {
                            window.location.href = '/admin/login?redirect=' + encodeURIComponent(window.location.pathname + window.location.search);
                        });
                    }
                }
            });

            form.on('submit(save)', function(data) {
                var field = data.field;
                if (field.new_password !== field.confirm_password) {
                    layer.msg('两次密码输入不一致', { icon: 2, time: 1500 });
                    return false;
                }
                if (field.new_password.length < 6 || field.new_password.length > 20) {
                    layer.msg('新密码长度需在6-20位之间', { icon: 2, time: 1500 });
                    return false;
                }
                $.ajax({
                    url: '/api/admin/auth/password',
                    type: 'PUT',
                    dataType: 'json',
                    contentType: 'application/json',
                    data: JSON.stringify({
                        old_password: field.old_password,
                        new_password: field.new_password,
                        confirm_password: field.confirm_password
                    }),
                    success: function(res) {
                        if (res.code === 0) {
                            layer.msg('密码修改成功', { icon: 1, time: 1000 }, function() {
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.msg(res.message || '密码修改失败', { icon: 2, time: 1500 });
                        }
                    },
                    error: function() {
                        layer.msg('请求失败', { icon: 2, time: 1500 });
                    }
                });
                return false;
            });
        });
    </script>
</body>
</html>
