

$(function(){

    let sendFlag = false;
    
    $('.code_box>button').on('click',function(){
        let _lang = $('#language').val();
        var $this = $(this);
        var email = $("#forgot_email input").val();
        if (email == "" || email == undefined){
            $("#forgot_email").addClass("error");
            $("#forgot_email .error_tip").show();
            return;
        }
        if(sendFlag) return;
        sendFlag = true;
        $.ajax({
            url: requestApi+"/v1/mailCode",
            dataType:"json",
            type:"POST",
            aysc:false,
            data:{
                "lang":_lang,
                "email": email,
                
            },
            success:function(res){
                sendFlag = false;
                if(res.code == 0){
                    sendFlag = false;

                    settime($this)
                }else if(res.code == 5){   // timeout
                    sendFlag = false;
    
                }
                messageFn(res.msg);
                return;
            },error:function () {
                sendFlag = false;
            }
        })
       
    })

    $('.login_btn').click(forgotSubmit)
    const $passwordInput = $('#forgot_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');
        }
    });
    
})
let countdown = 90
const settime =async(obj)=>{
    if (countdown == 0) {
      obj.text("Send");
      countdown = 90;
      return;
    } else {
      obj.text("" + countdown + "s");
      countdown--;
    }
    setTimeout(function () {
      settime(obj);
    }, 1000);
}
let clickFlag = false;
const forgotSubmit = () => {
    const email = $('#forgot_email input').val()
    const code = $('#forgot_code input').val()
    const password = $('#forgot_pwd input').val();
    let _lang = $('#language').val();
    if (clickFlag) return;
    clickFlag = true;
    $.ajax({
        url: requestApi+"/v1/repwd",
        dataType:"json",
        type:"POST",
        aysc:false,
        data:{
            "lang":_lang,
            "email": email,
            "pwd": password,
            "code": code
            
        },
        success:function(res){
            clickFlag = false;

            if(res.code == 0){
                setTimeout(() => {
                    window.location.href = webDomain+'/sign-in/';
                }, 1000);
                clickFlag = false;
            }else{
                messageFn(res.msg);
            }
            return;
        },error:function () {
            clickFlag = false;

        }
    })
}
