/**
 * 修改用户密码
 */
$(document)
    .ready(
        function () {
            var userIndex = parent.$("#userIndex").val();
            // var password;
            var passwordX;
            var usernames;
            $.ajax({
                type: "POST",
                url: "/pqs9900/user/selectId",
                data: {
                    userIndex: userIndex,
                    flag:0
                },
                success: function (data) {
                    // password = data.password;
                    usernames = data.loginName;
                }
            })
            $("#close").click(function () {
                parent.layer.closeAll();
            });
            $("#updatePassWord")
                .click(
                    function () {
                        // var Ypassword = $("#password").val();
                        // Ypassword = filterXSS(Ypassword);
                        var Xpassword = $("#Xpassword").val();
                        Xpassword = filterXSS(Xpassword);
                        var Zpassword = $("#ZXpassword").val();
                        if (Xpassword != "" && Zpassword != "") {
                            if (Xpassword.search(passwordz) != -1) {
                                if (Zpassword.search(passwordz) != -1) {
                                    if (Xpassword == Zpassword) {
                                        passwordX = Xpassword;
                                        usernames = encrypt(usernames);
                                        $.ajax({
                                            type: "POST",
                                            url: "/sso/user/generateSM2Key",
                                            dataType: "text",
                                            data: {
                                                username: usernames
                                            },
                                            success: function (data) {
                                                var info = $.parseJSON(data);
                                                if (info.code === 200) {
                                                    var pubkeyHex = info.body;//获取SM2加密公钥
                                                    var sm3Pwd = sm3Digest(passwordX);//SM3加密
                                                    var sm2Pwd =sm2Encrypt(sm3Pwd + "|" + passwordX, pubkeyHex, 0);//SM2公钥加密
                                                    var token = $("#token").val();
                                                    $.ajax({
                                                        method: "post",
                                                        url: "/pqs9900/user/updatePassword",
                                                        data: {
                                                            "username": usernames,
                                                            // "password": sm2Pwd,
                                                            "passwordX": sm2Pwd,
                                                            token: token
                                                        },
                                                        success: function (info) {
                                                            if (info.code == 300) {
                                                                layer
                                                                    .msg(
                                                                        info.message,
                                                                        {
                                                                            icon: 5
                                                                        },
                                                                        function () {
                                                                            parent.layer
                                                                                .closeAll();
                                                                        });
                                                            } else if (info.code == 500) {
                                                                layer
                                                                    .msg(
                                                                        info.message,
                                                                        {
                                                                            icon: 5
                                                                        },
                                                                        function () {
                                                                            parent.layer
                                                                                .closeAll();
                                                                        });
                                                            } else if (info.code == 301) {
                                                                layer
                                                                    .msg(
                                                                        info.message,
                                                                        {
                                                                            icon: 5
                                                                        },
                                                                        function () {
                                                                            parent.layer
                                                                                .closeAll();
                                                                        });
                                                            } else {
                                                                layer.msg(
                                                                    info.message,
                                                                    {
                                                                        icon: 6,
                                                                        time: 1000
                                                                    },
                                                                    function () {
                                                                        parent.load(parent.userState, parent.careUser);
                                                                        parent.layer
                                                                            .closeAll();
                                                                    });
                                                            }
                                                        }
                                                    })
                                                }
                                            }
                                        })
                                    } else {
                                        layer.msg('两次密码输入不一致！', {
                                            icon: 2,
                                            time: 2000
                                        });
                                    }
                                } else {
                                    layer
                                        .msg(
                                            '密码由8-16位字母、数字及特殊字符组成！',
                                            {
                                                icon: 2,
                                                time: 2000
                                            });
                                }
                            } else {
                                layer
                                    .msg(
                                        '新密码由8-16位字母、数字及特殊字符组成！',
                                        {
                                            icon: 2,
                                            time: 2000
                                        });
                            }
                        } else {
                            layer.msg('密码均不能为空！', {
                                icon: 2,
                                time: 2000
                            });
                        }
                    })
        })

/**
 * 加密（需要先加载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();
}