
$(document).ready(function () {
    
    $("img").unveil();
    $("img").trigger("unveil");
    
    // Reset all views
    var verificationSent = $("#verification-sent");
    var verificationPending = $("#verification-pending");
    var verificationExpired = $("#verification-expired");
    var verificationDone = $("#verification-done");
    var verificationBroken = $("#verification-broken");
    var verificationError = $("#verification-error");

    
    $(verificationSent).hide();
    $(verificationPending).hide();
    $(verificationExpired).hide();
    $(verificationDone).hide();
    $(verificationBroken).hide();
    $(verificationError).hide();

    
    var params={};window.location.search.replace(/[?&;#]+([^=&]+)=([^&]*)/gi,function(str,key,value){params[key] = value;});
    var path = window.location.pathname;
    path = path.substring(path.lastIndexOf("/")+1);

    if(params.verify === "true" && params.vtoken){

    	$.post("API.php", { register: "VERIFY_EMAIL_CONF", reset_link: params.vtoken }, function (data) {
                
                if(data.response && data.message === "EMAIL VERIFIED SUCCESSFULLY"){
                     
                    $.post("API.php", { session_update: "UPDATE_VERIFICATION"}, function (data) {

                    },"json");
                    $(verificationDone).show(); 

                } else if(data.message === "LINK EXPIRED") {
                    $(verificationExpired).show();
                } else if(data.message === "NO SUCH LINK"){
                    $(verificationBroken).show();
                }
                
            },"json");


    } else if (params.verify == "sent") {
    	// console.log('inside verify true');
        $(verificationSent).show();
    } else if (params.verify == "error") {
        $(verificationError).show();
    } else if (params.verify == "pending" && user_email != "") {
        
        $.post("API.php", { register: "GETUSER", username: user_email }, function (data) {
                            if(data){
                                    // console.log('success ');
                                    // console.log(data[0]['ts_created']);  
                                    // console.log(data[0]['reset_link']);
                                    // const d = new Date(data[0]['ts_created']);
                                    // const dtf = new Intl.DateTimeFormat('en-US', { year: 'numeric', month: 'numeric', day: 'numeric' });
                                    // const [{ value: month },,{ value: day },,{ value: year }] = dtf.formatToParts(d) ;
                                    // var estDate = `${month} ${day} ${year}`;  
                                    var estDate = data[0]['ts_created'].split(' ')[0];
                                    console.log("estDate:",estDate, "data[0]['reset_link']", data[0]['reset_link']);
                                    if(new Date(estDate) < new Date('2021-04-05') && data[0]['reset_link'] == null){
                                        // console.log('Inside first if ');
                                        $.post("API.php", { register: "VERIFY_EMAIL_SEND", username: user_email }, function (data) {
                                                if(data.response){
                                                        console.log('sent mail ');
                                                        $(verificationPending).show();
                                                        $(verificationPending).find("#new-accounts").hide();    
                                                        $(verificationPending).find("#existing-accounts").show();
                                                } else {

                                                        window.location.replace("verify.php?verify=error");
                                                }
                                                //alert(data.response);
                                            },"json");
                                        
                                    }  
                                    else if(new Date(estDate) < new Date('2021-04-05') && data[0]['reset_link'] != null){
                                        // console.log('Inside second if ');
                                        // $(existingAccountVerificationPending).show();
                                        $(verificationPending).show();
                                        $(verificationPending).find("#new-accounts").hide();    
                                        $(verificationPending).find("#existing-accounts").show();
                                    }
                                    else{
                                        // console.log("Inside 3rd if else");
                                         $(verificationPending).show();
                                    }
                                    // console.log(res);     

                            } else {
                                    // console.log();  
                                    $(verificationPending).show();
                            }
                            
                        },"json");

         // $(existingAccountVerificationPending).show();
         // $(verificationPending).show();

    } else if (Object.keys(params) == "") {
		$(verificationBroken).show();
    } else if (params.verify==="" || params.vtoken===""){
        $(verificationBroken).show();
    }

    $(".id-btn-verify-resend").click(function(){
     
       $.post("API.php", { register: "VERIFY_EMAIL_SEND", username: user_email }, function (data) {
                            if(data.response){
                                    window.location.replace("verify.php?verify=sent");              
                            } else {
                                    window.location.replace("verify.php?verify=error");
                            }
                            //alert(data.response);
                        },"json");
       return false;
       
    });
    


});;