$(document).ready(function () {
    /*START TWO STEP*/
    var timerSpan = document.getElementById('timer');
    var countDownDate = new Date();
    countDownDate.setMinutes(2);
    var countDownTime = countDownDate.getTime();
    var timer = null;
    var timeEnd = 120;
    var count = 0;
     var invalidChars = ['e', '-', '+'];
    $('input[type=number]').keydown(function (e) {
        if (invalidChars.includes(e.key)) {
            e.preventDefault();
        }
    });

    $('#btnCancel').click(function () {
        window.location.href = $('#redirect-url').val();
    });
    //$("#alertSms").hide();
    //$('#btnReenviar').hide();
    $('#btnReenviar').click(function () {
        var email = $('#userEmail').val();
        $.ajax({
            method: 'post',
            url: '/SmsSend',
            data: { email },
            success: function ({token}) {
                console.log(token);
                $('#btn-submit').prop('disabled', false);
                $('#codeSrt').text(`(${token})`);
                $('#btnReenviar').hide(300);
                $("#alertSms").show(300);
                count = 0;
                timer = setInterval(interval, 1000);
            }
        });
    });

    var interval = function () {
        count++
        console.log('Start Timer');
        var now = new Date().getTime();
        var seconds = timeEnd - count;
        timerSpan.innerHTML = `${seconds} segundos`;
        if (count >= timeEnd) {
            clearInterval(timer);
            logOut();
        }
    }

    var logOut = function () {
        console.log('Log Out');
        $('#btnReenviar').show(300);
        $('#btn-submit').prop('disabled', true);
    };
    
    timer = setInterval(interval, 1000);
    /*END TWO STEP*/
});