<?php defined('ALTUMCODE') || die() ?>

<?= \Altum\Alerts::output_alerts() ?>

<h1 class="h5"><?= l('sms_verification.header') ?></h1>
<p class="text-muted"><?= l('sms_verification.subheader') ?></p>
<p class="text-muted"><?= l('sms_verification.mobileNumber') ?> <?= $data->values['mobile']?></p>
<?php if($data->status == 0): ?>
<form action="" method="post" class="mt-4" role="form">
    <div class="form-group">
        <label for="code"><?= l('sms_verification.code') ?></label>
        <input id="code" type="text" name="code" class="form-control <?= \Altum\Alerts::has_field_errors('code') ? 'is-invalid' : null ?>"  required="required" autofocus="autofocus" />
        <?= \Altum\Alerts::output_field_error('code') ?>
    </div>

    <?php if(settings()->captcha->resend_activation_is_enabled): ?>
    <div class="form-group">
        <?php $data->captcha->display() ?>
    </div>
    <?php endif ?>

    <div class="form-group mt-4">
        <button type="submit" name="submit" class="btn btn-primary btn-block my-1"><?= l('sms_verification.submit') ?></button>
    </div>
</form>
<div class="mt-5 text-center">
    <span id="timer"></span>
</div>
<div class="mt-5 text-center">
    <a id="sender" onclick="sendFunction()" class="text-muted" style="cursor: pointer;"><?= l('sms_verification.resend_code') ?></a>
</div>
<?php endif ?>
<?php if($data->status == 1): ?>
    <div class="mt-5 text-center">
    <a href="/dashboard" class="text-muted"><?= l('pay_thank_you.button') ?></a>
</div>
<?php endif ?>

<?php ob_start() ?>
<script>
    'use strict';

   var mobile = '';

    /* Function to check the discount code */
    let sendCode = () => {
        

        fetch(`${url}sms-verification/code`, {
            method: 'POST',
            body: JSON.stringify({
                 mobile: mobile
            }),
            headers: {
                'Content-Type': 'application/json; charset=UTF-8'
            }
        })
            .then(response => {
                return response.ok ? response.json() : Promise.reject(response);
            })
            .then(data => {
                if(data.status == 'success') {
                    
                    runTimer();

                } else {
                    
                }

                
            })
            .catch(error => {});

    };

    var countdownfrom = 0
    var currentsecond = 0
    function runTimer() {
        document.querySelector('#sender').style.display = 'none';
        countdownfrom = 60;
        var msg = 'امکان ارسال مجدد تا ' + (countdownfrom + 1) + ' ثانیه دیگر';
        document.querySelector('#timer').innerHTML=msg;
        currentsecond = countdownfrom + 1;
        countredirect()
    }

    function countredirect() {

        if (currentsecond != 0) {
            currentsecond -= 1
            var msg = 'امکان ارسال مجدد تا ' + (currentsecond) + ' ثانیه دیگر';
            document.querySelector('#timer').innerHTML=msg;

        }
        else {
            myFunction()
            return
        }
        setTimeout("countredirect()", 1000)
    }

    countredirect()

    function myFunction() {
        
        document.querySelector('#sender').style.display = '';
        document.querySelector('#timer').innerHTML='';
    }
    

    function sendFunction()
    {
       sendCode();
    }
   

</script>
<?php \Altum\Event::add_content(ob_get_clean(), 'javascript') ?>
                                