



$(function(){
    const url = window.location.href;
    const searchParams = new URLSearchParams(new URL(url).search);
    const rParam = searchParams.get('r');
    let submitFlag = false;
    if(localStorage.getItem("invitCode")){
        $("#keyword input").val(localStorage.getItem("invitCode"))
    }
    $(".login_btn").on("click",function (){
        let _lang = $('#language').val();

        var email = $("#login_email input").val();
        var password = $("#login_pwd input").val();
        var code = $("#keyword input").val();
        if (email == "" || email == undefined){
            $("#login_email").addClass("error");
            $("#login_email .error_tip").show();
            return;
        }else {
            $("#login_email").removeClass("error");
            $("#login_email .error_tip").hide();
        }
    
        if (password == "" || password == undefined){
            $("#login_pwd").addClass("error");
            $("#login_pwd .error_tip").show();
            return;
        }else {
            $("#login_pwd").removeClass("error");
            $("#login_pwd .error_tip").hide();
        }
        if(submitFlag) return;
        submitFlag = true;
        $('.loading-mask').show();
        $.ajax({
            url: requestApi+"/v1/signup",
            dataType:"json",
            type:"POST",
            aysc:false,
            data:{
                "lang": _lang,
                "email": email,
                "pwd": password,
                "kwd":localStorage.getItem("keyword"),
                "pcode": code
                
            },
            success:function(res){
                submitFlag = false;
                $('.loading-mask').hide();
                if(res.code == 0){
                    localStorage.setItem('session',res.data.token);
                    localStorage.setItem("user", JSON.stringify(res.data));
                    const expiresDate = new Date();
                    const expires = res.data.token_cache == 0 ? 9999999999999 : res.data.token_cache;
                    expiresDate.setDate(expiresDate.getDate() + expires);
                    const domain = window.location.host.split(".").slice(-2).join(".");
                    Cookies.set("common_session", res.data.token, { expires: expiresDate, domain});
                    if(res.data.country != 'CN'){
                        console.log(res.data.country)
                        fbq('track', 'Lead');
                    }
                    if(!rParam){
                        window.location.href = '/';
                    }else{
                        window.location.href = 'https://cliproxy.com'+rParam;
                    }
                }else{
                    messageFn(res.msg);
                }
            },error:function () {
                submitFlag = false;
                $('.loading-mask').hide();
            }
        })
    
    });
    const $passwordInput = $('#login_pwd input');
    const $eyeIcon = $('#eyeIcon');

    $eyeIcon.on('click', function () {
        if ($passwordInput.attr('type') === 'password') {
        $passwordInput.attr('type', 'text');
        $eyeIcon.attr('src', '/static/common/login/img/pwd-2.png');
        } else {
        $passwordInput.attr('type', 'password');
        $eyeIcon.attr('src', '/static/common/login/img/pwd-1.png');
        }
    });
})
