<%inherit file="login.phtml"/>
<%block name="title">${_('Paramecio Login')}</%block>
<%block name="content">
<form id="login">
    <div id="title">
        ${_('Paramecio Login')}
    </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')}">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>
</form>
</%block>
<%block name="ajax">
<script language="javascript">
    $(document).ready( function () {
    
        $('#code_submit').click( function () {
            
            $('#code_submit').prop('disabled', true);
        
            $('#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 ) {
                
                alert(textStatus);
                $('#code_submit').prop('disabled', false);
                
            }
            }).done(function(data) {
                
                if(data.error==0)
                {
                    
                    //location.reload()
                    location.href="${url_for('admin_app.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')}");
                        
                    } 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>
