/**
 * 新增用户
 */
var usertype = ['长期账号', '临时账号'];
var isnotice = ['否','是']
$(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);
    }

    $.each(usertype, function (i, item) {
        var option = $("<option value='"
            + i + "'>"
            + item + "</option>");
        $("#usertype").append(option);
    });

    $("#usertype").val(0);
    $.each(isnotice, function (i, item) {
        var option = $("<option value='"
            + i + "'>"
            + item + "</option>");
        $("#isnotice").append(option);
    });

    $("#isnotice").val(0);

    $.ajax({
        method: "post",
        url: "/pqs9900/depts/selectAll",
        type: "json",
        data: "",
        success: function (data) {
            if (data.code === 500) {
                //获取数据异常
            } else {
                $.each(data.body.deptsList, function (i, item) {
                    var option = $("<option value='"
                        + item.deptsIndex + "'>"
                        + item.deptsName + "</option>");
                    $("#deptsIndex").append(option);
                })

                $('#deptsIndex').selectpicker({
                    noneSelectedText: '',
                    width:180,
                    noneResultsText: '',
                    liveSearch: true,
                    size: 6  //设置select高度，同时显示5个值
                });
            }
        }
    })
    $("#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 deptsIndex = $("#deptsIndex").val();
                                            deptsIndex = filterXSS(deptsIndex);
                                            if (deptsIndex == "=======请选择=======") {
                                                layer.tips('请选择具体部门', '#deptsIndex', {
                                                    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;
                                                ipStart4 = ipStart4.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 usertype = $("#usertype").val();
                                                                usertype = filterXSS(usertype);

                                                                var isnotice = $("#isnotice").val();
                                                                isnotice = filterXSS(isnotice);

                                                                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 = sm3Digest(password);//SM3加密
                                                                            var sm2Pwd =sm2Encrypt(sm3Pwd + "|" + password, pubkeyHex, 0);//SM2公钥加密
                                                                            var token = $("#token").val();
                                                                            $.ajax({
                                                                                url: '/pqs9900/user/saveUser',
                                                                                type: 'POST',
                                                                                dataType: "json",
                                                                                data: {
                                                                                    name: name,
                                                                                    loginName: loginName,
                                                                                    password: sm2Pwd,
                                                                                    deptsIndex: deptsIndex,
                                                                                    phone: tel,
                                                                                    email: email,
                                                                                    ip: ip,
                                                                                    ipEnd: ipEnd,
                                                                                    time: time,
                                                                                    mark: 1,
                                                                                    usertype: usertype,
                                                                                    isnotice:isnotice,
                                                                                    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();
}
