<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no"/>
    <link href="../../bootstrap/js/jquery-confirm/jquery-confirm.min.css" rel="stylesheet">
    <link href="../../bootstrap/css/bootstrap.min.css" rel="stylesheet">
    <link href="../../bootstrap/js/bootstrap-multitabs/multitabs.min.css" rel="stylesheet" type="text/css">
    <link href="../../bootstrap/css/materialdesignicons.min.css" rel="stylesheet">
    <link href="../../bootstrap/css/style.min.css" rel="stylesheet">
</head>

<body>
<div class="container-fluid p-t-15">
    <div class="row">
        <div class="col-lg-12">
            <div class="card">
                <div class="card-header">
                    <div class="card-title">新增管理员</div>
                </div>
                <div class="card-body">
                    <form>
                        <div class="form-group">
                            <label>旧密码</label>
                            <input type="password" class="form-control" maxlength="20" id="old_password"
                                   placeholder="输入账号的原登录密码">
                        </div>
                        <div class="form-group">
                            <label>新密码</label>
                            <input type="password" class="form-control" maxlength="20" id="new_password"
                                   placeholder="输入新的密码">
                        </div>
                        <div class="form-group">
                            <label>确认新密码</label>
                            <input type="password" class="form-control" maxlength="20" id="confirm_password"
                                   placeholder="再次输入新的密码">
                        </div>
                        <button type="button" id="btnOk" class="btn btn-primary">确认</button>
                        <button type="button" id="btnLoading" class="btn btn-primary" disabled style="display: none">
                            <span class="spinner-grow spinner-grow-sm" role="status" aria-hidden="true"></span>
                            执行中...
                        </button>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript" src="../../bootstrap/js/jquery.min.js"></script>
<script type="text/javascript" src="../../bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="../../bootstrap/js/bootstrap-maxlength/bootstrap-maxlength.min.js"></script>
<script type="text/javascript" src="../../bootstrap/js/jquery-confirm/jquery-confirm.min.js"></script>
<script type="text/javascript" src="../../bootstrap/js/bootstrap-multitabs/multitabs.min.js"></script>
<script type="text/javascript" src="../../bootstrap/js/httpclient/httpclient.js"></script>
<script type="text/javascript" src="../../bootstrap/js/authorization/md5.min.js"></script>
<script type="text/javascript">
    $(document).ready(function () {

        $("input#old_password, input#new_password, input#confirm_password").maxlength({
            warningClass: "badge badge-info",
            limitReachedClass: "badge badge-warning"
        });

        $('#btnOk').on('click', function () {
            const old_password = $("#old_password").val();
            if (old_password === "") {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请输入账号的原登录密码。',
                });
                return false;
            }

            const new_password = $("#new_password").val();
            if (new_password === "") {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请输入新的密码。',
                });
                return false;
            }

            const confirm_password = $("#confirm_password").val();
            if (confirm_password === "") {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请再次输入新的密码。',
                });
                return false;
            }

            if (new_password !== confirm_password) {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请确认两次密码一致。',
                });
                return false;
            }

            const postData = {
                old_password: md5(old_password),
                new_password: md5(new_password),
            };


            AjaxForm(
                "PATCH",
                "/api/admin/modify_password",
                postData,
                function () {
                    $(this).hide();
                    $("#btnLoading").show();
                },
                function () {
                    $("#btnLoading").hide();
                    $("#btnOk").show();

                    $.alert({
                        title: '操作成功',
                        icon: 'mdi mdi-check-decagram',
                        type: 'green',
                        content: '密码修改成功。',
                        buttons: {
                            okay: {
                                text: '关闭',
                                action: function () {
                                    parent.window.close();
                                    window.open("/login");
                                }
                            }
                        }
                    });
                },
                function (response) {
                    $("#btnLoading").hide();
                    $("#btnOk").show();

                    AjaxError(response);
                }
            );
        });
    })
</script>
</body>
</html>
