if(window.top!==window.self){window.top.location=window.location};
document.write('<script type="text/javascript" src="/js/jsencrypt_3.3.2.min.js"></script>');

layui.use(['element', 'layer'], function () {
    var layer = layui.layer;
    var $ = layui.jquery;
    $(document).on('click', '.captcha-img', function () {
        var src = this.src.split("?")[0];
        this.src = src + "?" + Math.random();
    });
    $(document).on('click', '.ajax-login', function (e) {
        e.preventDefault();
        // 获取表单数据并将其转换为 JSON 对象
        var form = $(this).parents("form");
        var serializeArray = form.serializeArray();

        var jsonData = {}
        $.each(serializeArray, function (index, item) {
            jsonData[item.name] = item.value;
        });
        let encryptor = new JSEncrypt();
        encryptor.setPublicKey(localStorage.getItem("pubkey"));
        jsonData.credential = encryptor.encrypt(localStorage.getItem("pubkeyR") + jsonData.credential);
        let jsonStr = JSON.stringify(jsonData);
        // 手动提交表单
        $.ajax({
            type: 'POST',
            url: '/login',
            contentType: "application/json;charset=utf-8",
            data: jsonStr,
            success: function(resp) {
                // 处理表单提交成功后的响应
                if (resp.code === 0) {
                    let redirectPath = resp.data
                    window.location.replace(redirectPath)
                } else {
                    $.fn.Messager(resp)
                }
            },
            error: function(xhr, status, error) {
                // 处理表单提交失败后的错误
                layer.msg(resp.responseText, {offset: '15px', time: 5000, icon: 2});
            }
        });

        /*var form = $(this).parents("form");
        var url = form.attr("action");
        var serializeArray = form.serializeArray();
        $.post(url, serializeArray, function (result) {
            console.log(result)
            if (result.code === 0) {
                let redirectPath = result.data
                window.location.replace(redirectPath)
            } else {
                $.fn.Messager(result)
            }
        }).error(function (result) {
            layer.msg(result.responseText, {offset: '15px', time: 5000, icon: 2});
        });*/
    });
    $('.layui-layer-loading').hide();
});