<!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="text" class="form-control" maxlength="10" id="username"
                                   placeholder="请输入用户名">
                        </div>
                        <div class="form-group">
                            <label>昵称</label>
                            <input type="text" class="form-control" maxlength="10" id="nickname"
                                   placeholder="请输入昵称">
                        </div>
                        <div class="form-group">
                            <label>手机号</label>
                            <input type="text" class="form-control" maxlength="11" id="mobile"
                                   placeholder="请输入手机号">
                        </div>
                        <div class="form-group">
                            <label>密码</label>
                            <input type="password" class="form-control" maxlength="20" id="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#username").maxlength({
            warningClass: "badge badge-info",
            limitReachedClass: "badge badge-warning"
        });

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

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

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

        $('#btnOk').on('click', function () {
            const username = $("#username").val();
            if (username === "") {
                $.alert({
                    title: '温馨提示',
                    icon: 'mdi mdi-alert',
                    type: 'orange',
                    content: '请输入用户名。',
                });
                return false;
            }

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

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

            const postData = {
                username: username,
                nickname: nickname,
                mobile: $("#mobile").val(),
                password: md5(password),
            };


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

                    $.alert({
                        title: '操作成功',
                        icon: 'mdi mdi-check-decagram',
                        type: 'green',
                        content: '编号：' + data.id + ' 创建完成。',
                        buttons: {
                            okay: {
                                text: '关闭',
                                action: function () {
                                    location.href = "/admin/list";
                                }
                            }
                        }
                    });
                },
                function (response) {
                    $("#btnLoading").hide();
                    $("#btnOk").show();

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