<%inherit file="login.phtml"/>
<%block name="content">
<div class="card">
  <div class="card-header bg-primary">
    ${_('Auth code')}
  </div>
  <div class="card-body">
    <form method="POST" action="#" id="auth_code_form" class="needs-validation" novalidate>
      <div class="mb-3">
        <label for="code_form" class="form-label">${_('Check your email for get instructions for complete login with double auth or')} <a href="${url_for('admin_app.logout_admin')}">logout</a> and login again with other user</label>
        <input type="text" class="form-control form-control-lg has-validation" id="code_form" name="code" aria-describedby="code" autocomplete="off" required>
        <div class="invalid-feedback" id="txt_error_code">
            ${_('You need a valid code')}
        </div>
      </div>
      <button type="submit" id="code_submit" class="btn btn-primary">${_('Check code')}</button>
      ${csrf_token()|n}
    </form>
  </div>


    <!--<div class="form">
        <p align="center">${_('Check your email for get instructions for complete login with double auth or')} <a href="${url_for('admin_app.logout_admin')}">logout</a> and login again with other user</p>
        <p><label>${_('Code')} *</label><input type="text" class="" name="code" id="code_form" value="" /> <span class="error" id="code_error"></span></p>
        ${csrf_token()|n}
    </div>
    <div id="submit_block">
        <input type="submit" value="${_('Send code')}" class="submit" id="code_submit"/>
        <span id="loading">&nbsp;</span>
    </div>-->

</%block>
<%block name="jscript">
<script language="javascript">
    $(document).ready( function () {
    
    var forms = document.querySelectorAll('.needs-validation');
  var error=false;
    
    Array.prototype.slice.call(forms)
    .forEach(function (form) {
      form.addEventListener('submit', function (event) {
          
            error=false;
        
            /*if (!form.checkValidity()) {
              
                error=true;
              
            }*/
            
            event.preventDefault();
            event.stopPropagation();
            
            //form.classList.add('was-validated');
            
            if(!error) {
                
                $('#code_submit').prop('disabled', true);
                
                console.log('Checking done!');
                
                $('#loading').show();
                
                data_form={'code': $('#code_form').val(), 'csrf_token': $("#csrf_token").val()};
                
                $.ajax({
                url: "${url_for('admin_app.auth_check')}",
                method: "POST",
                dataType: "json",
                data: data_form,
                error: function(jqXHR, textStatus, errorThrown ) {
                    
                    form.classList.add('was-validated');
                    alert(textStatus);
                    $('#code_submit').prop('disabled', false);
                    $('#code_form').get(0).setCustomValidity("${_('Error, please try again later')}");
                    
                }
                }).done(function(data) {
                    
                    form.classList.add('was-validated');
                    
                    if(data.error==0)
                    {
                        
                        //location.reload()
                        location.href="${url_for('admin_app.home_admin')}";
                    
                    }
                    else
                    {
                        $('#code_submit').prop('disabled', false);
                        
                        // Firefox have a horrible and stupid bug and you need attr for set de new csrf_token
                        
                        $('#csrf_token').attr('value', data.csrf_token);
                    
                        $('#loading').hide('slow');

                        if(data.hasOwnProperty('disable')) {
                            
                            //$('#code_error').html("${_('Error, your user is disabled, you need support of web administration')}");
                            $('#code_form').get(0).setCustomValidity("${_('Error, your user is disabled, you need support of web administration')}");
                            
                            $('#txt_error_code').html("${_('Error, your user is disabled, you need support of web administration')}");
                            
                        } else {
                            $('#code_form').get(0).setCustomValidity("${_('Error, wrong code')}");
                            //$('#code_error').html("${_('Error, wrong code')}");
                            $('#txt_error_code').html("${_('Error, wrong code')}");
                        }
                        
                        if(data.you_cannot_login) {
                            
                            //$('#code_error').html("${_('Error, excessive tries, wait some minutes for login again')}");
                            $('#code_form').get(0).setCustomValidity("${_('Error, excessive tries, wait some minutes for login again')}");
                            $('#txt_error_code').html("${_('Error, excessive tries, wait some minutes for login again')}");
                            
                        }
                        
                    }
                
                });
                
            }
            
           }
        )
    
    });
    
        /*$('#code_submit').click( function () {
            
            $('#code_submit').prop('disabled', true);
        
            $('#loading').show();
            
            data_form={'code': $('#code_form').val(), 'csrf_token': $("#csrf_token").val()};
            
            $.ajax({
            url: "",
            method: "POST",
            dataType: "json",
            data: data_form,
            error: function(jqXHR, textStatus, errorThrown ) {
                
                alert(textStatus);
                $('#code_submit').prop('disabled', false);
                
            }
            }).done(function(data) {
                
                if(data.error==0)
                {
                    
                    //location.reload()
                    location.href="";
                
                }
                else
                {
                    $('#code_submit').prop('disabled', false);
                    
                    // Firefox have a horrible and stupid bug and you need attr for set de new csrf_token
                    
                    $('#csrf_token').attr('value', data.csrf_token);
                
                    $('#loading').hide('slow');

                    if(data.hasOwnProperty('disable')) {
                        
                        $('#code_error').html("${_('Error, your user is disabled, you need support of web administration')}");
                        
                    } else {

                        $('#code_error').html("${_('Error, wrong code')}");
                        
                    }
                    
                    if(data.you_cannot_login) {
                        
                        $('#code_error').html("${_('Error, excessive tries, wait some minutes for login again')}");
                        
                    }
                    
                }
            
            });
            
            return false;
        
        });
    */
    });
</script>
</%block>
