var remindLayer;
var flag;

function confirmIndentity(username, fn) {
    username = encrypt(username);
    flag = false;
    remindLayer = layer.open({
        type: '1',
        title: "密码确认",
        resize: false,
        shadeClose: true,
        scrollbar: false,
        area: "350px",
        closeBtn: 0,
        content: "<form class='form-horizontal pdr15 pdt15 pdl15'><div class='form-group'><label for='newPwd' class='col-sm-3 control-label'>密码</label><div class='col-sm-9'><input type='password' class='form-control' id='confirmPwd' autocomplete='new-password'></div></div></form>",
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            var confirmPwd = $("#confirmPwd").val();
            confirmPwd = confirmPwd.replace(/(^\s*)|(\s*$)/g, '');
            if (validPassword(confirmPwd)) {
                if (!flag) {
                    flag = true;
                    //执行更新用户密码的操作
                    $.ajax({
                        type: "POST",
                        url: "/sso/user/generateSM2Key",
                        dataType: "text",
                        async: false,
                        data: {
                            username: username
                        },
                        success: function (data) {
                            var info = $.parseJSON(data);
                            if (info.code === 200) {
                                var pubkeyHex = info.body;//获取SM2加密公钥
                                var sm3Pwd = sm3Digest(confirmPwd);//SM3加密
                                var sm2Pwd = sm2Encrypt(sm3Pwd + "|" + confirmPwd, pubkeyHex, 0);//SM2公钥加密
                                confirmUserPwd(sm2Pwd, fn);
                            } else {
                                layer.msg(info.message, {icon: 2, time: 2000});
                            }
                        }
                    });
                }
            } else {
                layer.msg('密码不正确', {icon: 2, time: 2000});
            }
        }, btn2: function (index, layero) {
            layer.close(index);
        },
        end: function (index) {
            layer.close(index);
        }
    })
}

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


//验证用户密码
function validPassword(password) {
    password = password.replace(/(^\s*)|(\s*$)/g, '');
    var pwdRepx = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@#_!$%^&*()?<>/|~,\\\-+\.]).{8,16}$/;
    if (!pwdRepx.test(password)) {
        return false;
    }
    return true;
}

//验证密码的正确性
function validPasswordIndentity(password) {
    password = password.replace(/(^\s*)|(\s*$)/g, '');
    var pwdRepx = /^[0-9]{10,16}$/;
    if (!pwdRepx.test(password)) {
        return false;
    }
    return true;
}

//验证密码的正确性
function validLocalPasswordIndentity(password) {
    password = password.replace(/(^\s*)|(\s*$)/g, '');
    var pwdRepx = /^^[A-Za-z]{7,16}$/;
    // var pwdRepx = /^(?=.*[a-zA-Z])(?=.*\d)(?=.*[@#_!$%^&*()?<>/|~,\\\-+\.]).{8,16}$/;
    if (!pwdRepx.test(password)) {
        return false;
    }
    return true;
}

//判断用户名密码是否匹配
/**
 * 请求密码认证前，生成一个随机码，随机码进行加密，随加密后的密码传到后台。后台密码验证正确后，对前台传递的随机码进行解密，并加入salt重新加密传递到前台，
 * 前台首先解密后台的明文，看其中是否有随机码，然后再获取密码结果
 */
function confirmUserPwd(pwd, fn) {
    $.ajax({
        type: "POST",
        url: "/pqs9900/user/confirmPwd",
        dataType: "json",
        async: false,
        data: {
            password: pwd
        },
        statusCode: {
            203: function () {
                layer.msg("密码正确", {icon: 6, time: 2000}, function () {
                    layer.close(remindLayer);
                    //执行传递过来的函数内容
                    fn();
                });
            }
        },
        success: function (data) {
            flag = false;
            if (data.code === 500) {
                layer.msg(data.message, {icon: 2, time: 2000});
            }
        }
    })
}

//下发定值等
function confirmLocalIndentity(pwd, fn) {
    $.ajax({
        type: "POST",
        url: "/pqs9900/user/confirmIndentity",
        dataType: "json",
        async: false,
        data: {
            password: pwd
        },
        statusCode: {
            203: function () {
                layer.msg("密码正确", {icon: 6, time: 2000}, function () {
                    layer.close(remindLayer);
                    //执行传递过来的函数内容
                    fn();
                });
            }
        },
        success: function (data) {
            flag = false;
            if (data.code === 500) {
                layer.msg(data.message, {icon: 2, time: 2000});
            }
        }
    })
}

function confirmUserPwdIndentity(pwd, fn) {
    $.ajax({
        type: "POST",
        url: "/pqs9900/user/confirmUserPwdIndentity",
        dataType: "json",
        async: false,
        data: {
            password: pwd
        },
        statusCode: {
            203: function () {
                layer.msg("密码正确", {icon: 6, time: 2000}, function () {
                    layer.close(remindLayer);
                    //执行传递过来的函数内容
                    fn();
                });
            }
        },
        success: function (data) {
            flag = false;
            if (data.code === 500) {
                layer.msg(data.message, {icon: 2, time: 2000});
            }
        }
    })
}

//定值修改等密码验证（njcnpqs)
function passwordLocalIndentity(username, resetName, fn) {
    username = encrypt(username);
    flag = false;
    remindLayer = layer.open({
        type: '1',
        title: resetName + "密码确认",
        resize: false,
        shadeClose: true,
        scrollbar: false,
        area: "350px",
        closeBtn: 0,
        content: "<form class='form-horizontal pdr15 pdt15 pdl15'><div class='form-group'><label for='newPwd' class='col-sm-3 control-label'>验证密码</label><div class='col-sm-9'><input type='password' class='form-control' id='confirmPwd' autocomplete='new-password'></div></div></form>",
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            var confirmPwd = $("#confirmPwd").val();
            confirmPwd = confirmPwd.replace(/(^\s*)|(\s*$)/g, '');
            if (validLocalPasswordIndentity(confirmPwd)) {
                if (!flag) {
                    flag = true;
                    //执行更新用户密码的操作
                    $.ajax({
                        type: "POST",
                        url: "/pqs9900/user/generateSM2Key",
                        dataType: "text",
                        async: false,
                        data: {
                            username: username
                        },
                        success: function (data) {
                            var info = $.parseJSON(data);
                            if (info.code === 200) {
                                var pubkeyHex = info.body;//获取SM2加密公钥
                                var sm3Pwd = sm3Digest(confirmPwd);//SM3加密
                                var sm2Pwd = sm2Encrypt(sm3Pwd + "|" + confirmPwd, pubkeyHex, 0);//SM2公钥加密
                                confirmUserPwdIndentity(sm2Pwd, fn);
                            } else {
                                layer.msg(info.message, {icon: 2, time: 2000});
                            }
                        }
                    });
                }
            } else {
                layer.msg('密码不正确', {icon: 2, time: 2000});
            }
        }, btn2: function (index, layero) {
            layer.close(index);
        },
        end: function (index) {
            layer.close(index);
        }
    })
}

//定值下发等新增密码的验证
var info;
function passwordIndentity(username, resetName, fn) {
    username = encrypt(username);
    flag = false;
    remindLayer = layer.open({
        type: '1',
        title: resetName + "密码确认",
        resize: false,
        shadeClose: true,
        scrollbar: false,
        area: "350px",
        closeBtn: 0,
        content: "<form class='form-horizontal pdr15 pdt15 pdl15'><div class='form-group'><label for='newPwd' class='col-sm-3 control-label'>验证密码</label><div class='col-sm-9'><input type='password' class='form-control' id='confirmPwd' autocomplete='new-password'></div></div></form>",
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            var confirmPwd = $("#confirmPwd").val();
            confirmPwd = confirmPwd.replace(/(^\s*)|(\s*$)/g, '');
            if (validPasswordIndentity(confirmPwd)) {
                if (!flag) {
                    flag = true;
                    //执行更新用户密码的操作
                    $.ajax({
                        type: "POST",
                        url: "/pqs9900/user/generateSM2Key",
                        dataType: "text",
                        async: false,
                        data: {
                            username: username
                        },
                        success: function (data) {
                            info = $.parseJSON(data);
                            if (info.code === 200) {
                                var pubkeyHex = info.body;//获取SM2加密公钥
                                var sm3Pwd = sm3Digest(confirmPwd);//SM3加密
                                var sm2Pwd = sm2Encrypt(sm3Pwd + "|" + confirmPwd, pubkeyHex, 0);//SM2公钥加密
                                confirmLocalIndentity(sm2Pwd, fn);
                            } else {
                                layer.msg(info.message, {icon: 2, time: 2000});
                            }
                        }
                    });
                } else {
                    layer.msg(info.message, {icon: 2, time: 2000});
                }
            } else {
                layer.msg('密码不正确', {icon: 2, time: 2000});
            }
        }, btn2: function (index, layero) {
            layer.close(index);
        },
        end: function (index) {
            layer.close(index);
        }
    })
}

//版本升级专用密码确认
function passwordIndentityForEd(username, resetName, fn) {
    username = encrypt(username);
    flag = false;
    remindLayer = layer.open({
        type: '1',
        title: resetName + "密码确认",
        resize: false,
        shadeClose: true,
        scrollbar: false,
        area: ['250px', '150px'],
        closeBtn: 0,
        content: "<form class='form-horizontal pdt15'><label for='newPwd' class='col-sm-3 col-xs-3 control-label' style='padding-right: 0px;padding-left: 0px'>验证密码</label><div class='col-sm-9 col-xs-9'><input type='password' class='form-control' id='confirmPwd' autocomplete='new-password'></div></form>",
        btn: ['确定', '取消'],
        yes: function (index, layero) {
            var confirmPwd = $("#confirmPwd").val();
            confirmPwd = confirmPwd.replace(/(^\s*)|(\s*$)/g, '');
            if (validPasswordIndentity(confirmPwd)) {
                if (!flag) {
                    flag = true;
                    //执行更新用户密码的操作
                    $.ajax({
                        type: "POST",
                        url: "/pqs9900/user/generateSM2Key",
                        dataType: "text",
                        async: false,
                        data: {
                            username: username
                        },
                        success: function (data) {
                            info = $.parseJSON(data);
                            if (info.code === 200) {
                                var pubkeyHex = info.body;//获取SM2加密公钥
                                var sm3Pwd = sm3Digest(confirmPwd);//SM3加密
                                var sm2Pwd = sm2Encrypt(sm3Pwd + "|" + confirmPwd, pubkeyHex, 0);//SM2公钥加密
                                confirmLocalIndentity(sm2Pwd, fn);
                            } else {
                                layer.msg(info.message, {icon: 2, time: 2000});
                            }
                        }
                    });
                } else {
                    layer.msg(info.message, {icon: 2, time: 2000});
                }
            } else {
                layer.msg('密码不正确', {icon: 2, time: 2000});
            }
        }, btn2: function (index, layero) {
            layer.close(index);
        },
        end: function (index) {
            layer.close(index);
        }
    })
}