$('.account_verification_button').click(async function (e) {
    e.preventDefault();
    $(this).attr('disabled', true);
    let form = $('.account_verification_form'),
        data = new FormData(form[0]),
        url = form.attr('action'),
        method = form.attr('method');

    const response=await ajax({
        url,
        data,
        dataType: 'json',
        method,
        contentType: false,
        processData: false,
    });

    if (response.errors) {
        toasterError(response.errors);
        $(this).removeAttr('disabled');
    }
    if (response.url) {
        window.location.href = response.url;
    }
});

$(".code-input").keyup(function() {
    if (this.value.length == this.maxLength) {
        if (currentLocal === 'ar'){
            $(this).prev('.code-input').focus();
        }
        if (currentLocal === 'en'){
            $(this).next('.code-input').focus();
        }

    }
});

// $('input[name="code[]"]').keyup(function () {
//     if ($(this).val() !== ''){
//         $(this).parents().next().children('input').focus();
//     }
// });

/**
 * Fire timer
 * @param countDown seconds
 */
function startTimer(countDown = null) {
    let countDownElement = $('#countdown'),
        countDownDate = countDown ? countDown : 600,
        interval = setInterval(function () {
            countDownDate--;
            let days = Math.floor( countDownDate/(60*60*24) ),
                hours = Math.floor( (countDownDate/(60*60)) % 24 ),
                minutes = Math.floor( (countDownDate/60) % 60 ),
                seconds = Math.floor( (countDownDate) % 60 );
            countDownElement.html(hours + ':' + minutes + ":" + seconds);

            if (countDownDate === 0) {
                clearInterval(interval);
                const url = "<span>لم اتلقى الرساله بعد؟</span> <a href=\"#\">إعادة إرسال</a>";
                $('.resend_cond').html(url);

            }
        }, 1000);
}

startTimer();

let resendCodeRoute = "{{route('user.resendCode')}}";
$(document).on('click', '.resend_cond a', async function (e) {
    e.preventDefault();

    let url = resendCodeRoute,
        method = 'get';

    const response=await ajax({
        url,
        dataType: 'json',
        method,
        contentType: false,
        processData: false,
    });

    if (response.errors) {
        toasterError(response.errors);
    }
    if (response.success) {
        $('.resend_cond').html('');
        startTimer(600);
    }

});
