<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:set var="ctx" value="${pageContext.request.contextPath}"/>
<!DOCTYPE html >
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <title>新增管理员</title>
    <link rel="stylesheet" href="${ctx}/js/plugin/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${ctx}/css/plugin/bootstrap/bootstrap.min.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/datatables/datatables.bootstrap.css">
    <link rel="stylesheet" href="${ctx}/css/plugin/datatables/buttons.dataTables.min.css">
    <link rel="stylesheet" href="${ctx}/css/basic.css">
    <style type="text/css">
        html {
            overflow: hidden;
        }

        div {
            float: left;
        }
        .btn {
            padding: 1px 9px !important;
            font-size: 12px !important
        }
        .align-center {
            margin: 0 auto; /* 居中 这个是必须的，，其它的属性非必须 */
            text-align: center; /* 文字等内容居中 */
        }
    </style>
</head>
<body class="bg-white">
<div class="align-center">
    <div align="center" style="width:45%">
        <label class="layui-form-label mt10">昵称:</label>
        <div style="margin-left: -30px; margin-top: 5px;">
            <input type="text" id="name" placeholder="请输入昵称" maxlength="200"
                   autocomplete="off" style="width: 182.37px;margin-top: 10px;margin-left: 23.2px;height: 30px;"><font
                color="red">*</font>
        </div>
    </div>
    <div align="center" style="width:45%;margin-left: -50px">
        <label class="layui-form-label" style="margin-top: 10px">登录名:</label> <input
            type="text" id="loginName" placeholder="请输入登录名" autocomplete="off" maxlength="50"
            style="width: 58%; margin-top: 14px;height: 30px;margin-left: -60px"><font color="red">*</font>
    </div>
    <div align="center" style="width:45%">
        <label class="layui-form-label" style="margin-top: 10px">密码:</label> <input
            type="password" id="password" placeholder="请输入密码" autocomplete="off" maxlength="100"
            style="width: 58%; margin-top: 14px;height: 30px;margin-left: -60px"><font color="red">*</font>
    </div>
    <div align="center" style="width:45%;margin-left: -64px">
        <label class="layui-form-label" style="margin-top: 10px;width: 30%">再次输入:</label>
        <input type="password" id="Xpassword" placeholder="请再次输入密码" maxlength="100"
               autocomplete="off" style="width: 58%; margin-top: 14px;height: 30px;margin-left: -46px"><font
            color="red">*</font>
    </div>
    <div align="center" style="width:45%">
        <label class="layui-form-label" style="margin-top: 10px">邮箱:</label> <input
            type="text" id="email" placeholder="请输入邮箱" autocomplete="off" maxlength="200"
            style="width: 58%; margin-top: 14px;height: 30px;margin-left:-60px"><font color="red">*</font>
    </div>
    <div align="center" style="width:45%;margin-left: -50px">
        <label class="layui-form-label" style="margin-top: 10px;">手机号:</label>
        <input type="text" id="phone" placeholder="请输入手机号码" autocomplete="off" maxlength="20"
               style="width: 58%; margin-top: 14px;height: 30px;margin-left: -58px"><font color="red">*</font>
    </div>
    <div align="center" style="width:45%">
        <label class="layui-form-label" style="margin-top: 10px;">起始IP:</label>
        <input type="text" id="ipStart1" autocomplete="off" maxlength="3"
               style="width: 14.5%;margin-top: 12px;height:30px;margin-left: -64px;">.<input type="text" id="ipStart2" maxlength="3"
                                                                                             autocomplete="off"
                                                                                             style="width: 13.5%; margin-top: 10px;height: 30px">.<input
            type="text" id="ipStart3" autocomplete="off" maxlength="3"
            style="width:13.5%; margin-top: 10px;height: 30px">.<input type="text" id="ipStart4" autocomplete="off" maxlength="3"
                                                                       style="width:13.5%; margin-top: 10px;height: 30px">
    </div>
    <div align="center" style="width:45%;margin-left: -50px">
        <label class="layui-form-label" style="margin-top: 10px;">结束IP:</label>
        <input type="text" id="ipEnd1" autocomplete="off" maxlength="3"
               style="width: 14.5%;margin-top: 12px;height:30px;margin-left: -64px;">.<input type="text" id="ipEnd2" maxlength="3"
                                                                                             autocomplete="off"
                                                                                             style="width: 13.5%; margin-top: 10px;height: 30px">.<input
            type="text" id="ipEnd3" autocomplete="off" maxlength="3"
            style="width:13.5%; margin-top: 10px;height: 30px">.<input type="text" id="ipEnd4" autocomplete="off" maxlength="3"
                                                                       style="width:13.5%; margin-top: 10px;height: 30px">
    </div>
    <div align="center" style="width:45%">
        <label class="layui-form-label" style="margin-top: 10px">角色:</label> <select
            style="width: 58%;margin-top: 14px;margin-left: -65px;height: 30px;" id="rolegpIndex">
        <option>-------请选择------</option>
    </select>
    </div>
    <div align="center" style="width:45%;margin-left: -50px">
        <label class="layui-form-label" style="margin-top: 10px">时间段:</label> <select
            style="width: 28.7%;margin-top: 10px; height: 30px;margin-left: -64px" id="times1">
    </select>-<select
            style="width: 27.7%;margin-top: 10px; height: 30px" id="times2">
    </select>
    </div>


    <div style="width: 700px;" class="mt10 text-center ">
        <button type="button" class="layui-btn themeBGColor"
                id="insertUser"
                style="margin-top: 10px;margin-left: -130px">提交
        </button>
        <button type="reset" class="layui-btn cancleBGColor"
                style="margin-left: 20%; margin-top: 10px"
                id="close">取消
        </button>
    </div>

</div>
<input type="hidden" id="token" value="${token}">
<script src="${ctx}/js/plugin/jquery/jquery.min.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap-datetimepicker.js"></script>
<script src="${ctx}/js/plugin/bootstrap/bootstrap-datetimepicker.zh-CN.js"></script>
<script src="${ctx}/js/plugin/layui/layer.js"></script>
<script src="${ctx}/js/basic.js"></script>
<script src="${ctx}/js/plugin/xss/xss.js"></script>
<script src="${ctx}/js/plugin/xss/security.js"></script>
<script src="${ctx}/jspJS/utils/judgeAjaxStatus.js"></script>
<script src="${ctx}/js/plugin/xss/aes.js"></script>
<script src="${ctx}/jspJS/sm2.js"></script>
<script src="${ctx}/jspJS/sm3.js"></script>
<script>
    /**
     * 新增用户
     */
    $(document).ready(function () {
        for (var i = 0; i <= 23; i++) {
            var option = $("<option style='text-align: center;' value='"
                + i + "'>"
                + i + "</option>");
            $("#times1").append(option);
        }
        for (var i = 24; i >= 1; i--) {
            var option = $("<option style='text-align: center;' value='"
                + i + "'>"
                + i + "</option>");
            $("#times2").append(option);
        }
        $.ajax({
            method: "post",
            url: "/pqs9900/managers/allManagerRole",
            type: "json",
            success: function (data) {
                if (data.code === 500) {
                    //获取数据异常
                } else {
                    $.each(data.body, function (i, item) {
                        var option = $("<option value='"
                            + item.roleGpIndex + "'>"
                            + item.rolegpDescription + "</option>");
                        $("#rolegpIndex").append(option);
                    })
                }
            }
        })
        $("#close").click(function () {
            parent.layer.closeAll();
        })
        // 新增
        $("#insertUser").click(
                function () {
                    var name = $("#name").val();
                    name = name.replace(/(^\s*)|(\s*$)/g,
                        '');
                    name = filterXSS(name);
                    var re = chinesez;// 只能输入中文
                    if (!re.test(name)) {
                        layer.tips('名称只由中文组成', '#name', {
                            tips: [1, 'red'],
                            time: 2000
                            // 还可配置颜色
                        });
                        $("#name").val("");
                    } else {
                        var loginName = $("#loginName").val();
                        var re = englishz;// 只能输入英文字母
                        loginName = filterXSS(loginName);
                        if (!re.test(loginName)) {
                            layer.tips('登录名由3-16的字母和数字以及_组成',
                                '#loginName', {
                                    tips: [1, 'red'],
                                    time: 2000
                                    // 还可配置颜色
                                });
                            $("#loginName").val("");
                        } else {
                            var password = $("#password").val();
                            password = password.replace(/(^\s*)|(\s*$)/g, '');
                            password = filterXSS(password);
                            var re = passwordz;
                            if (!re.test(password)) {
                                layer.tips(
                                        '密码格式不正确,需要包含特殊字符字母数字，长度为8-16',
                                        '#password',
                                        {
                                            tips: [
                                                2,
                                                'red'],
                                            time: 2000
                                            // 还可配置颜色
                                        });
                                $("#password").val("");
                            } else {
                                var Xpassword = $("#Xpassword").val();
                                Xpassword = Xpassword.replace(/(^\s*)|(\s*$)/g, '');
                                Xpassword = filterXSS(Xpassword);
                                var re = passwordz;
                                if (!re.test(Xpassword)) {
                                    layer.tips(
                                            '密码需要包含特殊字符字母数字，长度为8-16',
                                            '#Xpassword',
                                            {
                                                tips: [
                                                    2,
                                                    'red']
                                                // 还可配置颜色
                                            });
                                    $("#Xpassword").val("");
                                } else {
                                    if (Xpassword != password) {
                                        layer.tips(
                                                '两次密码不一致,请重新输入',
                                                '#Xpassword',
                                                {
                                                    tips: [
                                                        2,
                                                        'red']
                                                    // 还可配置颜色
                                                });
                                        $("#Xpassword")
                                            .val("");
                                    } else {
                                        var tel = $('#phone').val();
                                        tel = tel.replace(/(^\s*)|(\s*$)/g, '');
                                        tel = filterXSS(tel);
                                        var re = phonez;
                                        if (!re.test(tel)) {
                                            layer.tips(
                                                    '手机号必须有11位，并且为数字，是正常的手机号码开头',
                                                    '#phone',
                                                    {
                                                        tips: [
                                                            2,
                                                            'red'],
                                                        time: 2000
                                                        // 还可配置颜色
                                                    });
                                            $("#phone").val("");
                                        } else {
                                            var email = $('#email').val();
                                            email = email.replace(/(^\s*)|(\s*$)/g, '');
                                            email = filterXSS(email);
                                            var re = emailz;
                                            if (!re.test(email)) {
                                                layer.tips('邮箱含有@ .在@后，以.com、.con结尾（qq,163等）',
                                                    '#email',
                                                    {
                                                        tips: [
                                                            2,
                                                            'red']
                                                        // 还可配置颜色
                                                    });
                                                $("#email").val("");
                                                $("#email").prop("placeholder", "邮箱格式不正确");
                                            } else {
                                                var rolegpIndex = $("#rolegpIndex").val();
                                                rolegpIndex = filterXSS(rolegpIndex);
                                                if (rolegpIndex === "=======请选择=======") {
                                                    layer.tips('请选择具体角色', '#rolegpIndex', {
                                                        tips: [2, 'red']
                                                    });
                                                } else {
                                                    var ipStart1 = $("#ipStart1").val();
                                                    var ipStart2 = $("#ipStart2").val();
                                                    var ipStart3 = $("#ipStart3").val();
                                                    var ipStart4 = $("#ipStart4").val();
                                                    var ip = ipStart1 + "." + ipStart2 + "." + ipStart3 + "." + ipStart4;
                                                    ip = ip.replace(/(^\s*)|(\s*$)/g, '');
                                                    ip = filterXSS(ip);
                                                    var re = ipz;
                                                    if (!re.test(ip)) {
                                                        layer.tips('ip地址格式不正确', "#ipStart4",
                                                            {
                                                                tips: [
                                                                    2,
                                                                    'red']
                                                                // 还可配置颜色
                                                            });
                                                        $("#ipStart1").val("");
                                                        $("#ipStart2").val("");
                                                        $("#ipStart3").val("");
                                                        $("#ipStart4").val("");
                                                    } else {
                                                        var ipEnd1 = $("#ipEnd1").val();
                                                        var ipEnd2 = $("#ipEnd2").val();
                                                        var ipEnd3 = $("#ipEnd3").val();
                                                        var ipEnd4 = $("#ipEnd4").val();
                                                        var ipEnd = ipEnd1 + "." + ipEnd2 + "." + ipEnd3 + "." + ipEnd4;
                                                        ipEnd = ipEnd.replace(/(^\s*)|(\s*$)/g, '');
                                                        ipEnd = filterXSS(ipEnd);
                                                        var re = ipz;
                                                        if (!re.test(ipEnd)) {
                                                            layer.tips('ip地址格式不正确', "#ipEnd4",
                                                                {
                                                                    tips: [
                                                                        2,
                                                                        'red']
                                                                    // 还可配置颜色
                                                                });
                                                            $("#ipEnd1").val("");
                                                            $("#ipEnd2").val("");
                                                            $("#ipEnd3").val("");
                                                            $("#ipEnd4").val("");
                                                        } else {
                                                            if (ip === ipEnd) {
                                                                layer.msg("起始ip和结束ip相同", {icon: 5},
                                                                    function () {
                                                                        layer
                                                                            .close();
                                                                    })
                                                            } else {
                                                                var times1 = $("#times1").val();
                                                                var times2 = $("#times2").val();
                                                                if (times1 === times2) {
                                                                    layer.msg("时间段选择相同", {
                                                                            icon: 5
                                                                        },
                                                                        function () {
                                                                            layer
                                                                                .close();
                                                                        })
                                                                } else {
                                                                    var time = times1 + "-" + times2;
                                                                    time = filterXSS(time);
                                                                    loginName = encrypt(loginName);
                                                                    $.ajax({
                                                                        type: "POST",
                                                                        url: "/pqs9900/user/generateSM2Key",
                                                                        dataType: "text",
                                                                        data: {
                                                                            username: loginName
                                                                        },
                                                                        success: function (data) {
                                                                            var info = $.parseJSON(data);
                                                                            if (info.code === 200) {
                                                                                /*var pubkeyHex = info.body;//获取SM2加密公钥
                                                                                var sm3Pwd = sm3Encrypt(password);//SM3加密
                                                                                var sm2Pwd =sm2Encrypt(sm3Pwd + "|" + password, pubkeyHex, 0);//SM2公钥加密*/
                                                                                var pubkeyHex = info.body;//获取SM2加密公钥
                                                                                var sm3Pwd = sm3Digest(password);//SM3加密
                                                                                var sm2Pwd =sm2Encrypt(sm3Pwd + "|" + password, pubkeyHex, 0);//SM2公钥加密
                                                                                var token = $("#token").val();
                                                                                $.ajax({
                                                                                    url: '/pqs9900/managers/saveManager',
                                                                                    type: 'POST',
                                                                                    dataType: "json",
                                                                                    data: {
                                                                                        name: name,
                                                                                        loginName: loginName,
                                                                                        password: sm2Pwd,
                                                                                        rolegpIndex: rolegpIndex,
                                                                                        phone: tel,
                                                                                        email: email,
                                                                                        ip: ip,
                                                                                        ipEnd: ipEnd,
                                                                                        time: time,
                                                                                        mark: 1,
                                                                                        token: token
                                                                                    },
                                                                                    success: function (info) {
                                                                                        if (info.code === 200) {
                                                                                            layer.msg(
                                                                                                    info.message,
                                                                                                    {
                                                                                                        icon: 6,
                                                                                                        time: 1000
                                                                                                    },
                                                                                                    function () {
                                                                                                        parent.load(parent.userState, parent.careUser);
                                                                                                        parent.layer
                                                                                                            .closeAll();
                                                                                                    });
                                                                                        } else {
                                                                                            layer.msg(
                                                                                                    info.message,
                                                                                                    {
                                                                                                        icon: 5,
                                                                                                        time: 1000
                                                                                                    }
                                                                                                );
                                                                                        }
                                                                                    }
                                                                                });
                                                                            }
                                                                        }
                                                                    });
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            )
    });

    /**
     * 加密（需要先加载lib/aes/aes.min.js文件）
     * @param word
     * @returns {*}
     */
    function encrypt(word) {
        var key = CryptoJS.enc.Utf8.parse("njcnpqsqpncjnggg");
        var srcs = CryptoJS.enc.Utf8.parse(word);
        var encrypted = CryptoJS.AES.encrypt(srcs, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
        return encrypted.toString();
    }

    /**
     * 解密
     * @param word
     * @returns {*}
     */
    function decrypt(word) {
        var key = CryptoJS.enc.Utf8.parse("njcnpqsqpncjnggg");
        var decrypt = CryptoJS.AES.decrypt(word, key, {mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.Pkcs7});
        return CryptoJS.enc.Utf8.stringify(decrypt).toString();
    }
</script>
</body>
</html>