<%@ 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=ISO-8859-1">
    <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;
        }
    </style>
</head>
<body class="bg-white">
<form class="layui-form" style="text-align: center;" method="post">
    <div class="layui-form-item" style="margin-top: 15px; width: 320px">
        <label class="layui-form-label">新密码:</label>
        <div class="layui-input-block">
            <input type="password" id="Xpassword" placeholder="请输入新密码" maxlength="100" autocomplete="off"
            style="width: 95%; margin-top: 6px;margin-left:-80px;height: 30px"><font
                color="red">*</font>
        </div>
    </div>
    <div class="layui-form-item" style="margin-top: 15px; width: 320px">
        <label class="layui-form-label" style="width: 86px;margin-left: -8px;">再次输入:</label>
        <div class="layui-input-block">
            <input type="password" id="ZXpassword" placeholder="请再次输入新密码" maxlength="100" autocomplete="off"
            style="width: 95%; margin-top: 6px;margin-left:-80px;height: 30px"><font
                color="red">*</font>
        </div>
    </div>
    <div style="width: 100%;text-align: center;">
        <button type="button" class="layui-btn themeBGColor"
                id="updatePassWord"
                style=" margin-top: 10px">提交
        </button>
        <button type="reset" class="layui-btn cancleBGColor"
                style="margin-left: 10%; margin-top: 10px"
                id="close">取消
        </button>
    </div>
</form>
<input type="hidden" id="username" value="${username}">
<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}/js/plugin/xss/aes.js"></script>
<script src="${ctx}/jspJS/sm2.js"></script>
<script src="${ctx}/jspJS/sm3.js"></script>
<script src="${ctx}/js/plugin/xss/security.js"></script>
<script src="${ctx}/jspJS/utils/judgeAjaxStatus.js"></script>
<script src="${ctx}/jspJS/confirmIndentity.js"></script>
<script>
    $(document).ready(function () {
        var userIndex = parent.$("#userIndex").val();
        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 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: "/pqs9900/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 = sm3Encrypt(passwordX);//SM3加密
                                                var sm2Pwd = sm2Encrypt(sm3Pwd + "|" + passwordX, pubkeyHex, 0);//SM2公钥加密*/

                                                var pubkeyHex = info.body;//获取SM2加密公钥
                                                var sm3Pwd = sm3Digest(Xpassword);//SM3加密
                                                var sm2Pwd =sm2Encrypt(sm3Pwd + "|" + Xpassword, pubkeyHex, 0);//SM2公钥加密
                                                var token = $("#token").val();
                                                $.ajax({
                                                    method: "post",
                                                    url: "/pqs9900/managers/updatePassword",
                                                    data: {
                                                        "username": usernames,
                                                        // "password": sm2Pwd,
                                                        "passwordX": sm2Pwd,
                                                        token: token
                                                    },
                                                    success: function (info) {
                                                        if (info.code === 500) {
                                                            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.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();
    }
</script>
</body>
</html>
