<?php

use PhangoApp\PhaRouter\Url;
use PhangoApp\PhaUtils\Utils;

function loginView($login=1) {

?>

<!DOCTYPE html>
<html>
    <head>
    <?php if($login) { ?>
    <title><?php echo _('Login'); ?></title>
    <?php } else { ?>
    <title><?php echo _('Signup'); ?></title>    
    <?php } ?>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
    <link href='https://fonts.googleapis.com/css?family=Open+Sans' rel='stylesheet' type='text/css'>
    <link href="<?php echo Url::make_media_url('modules/admin/media/css/login.css'); ?>" rel='stylesheet' type='text/css'>
    <link href="<?php echo Url::make_media_url('modules/admin/media/css/font-awesome.min.css'); ?>" rel='stylesheet' type='text/css'>
    <script language="Javascript" src="<?php echo Url::make_media_url('modules/admin/media/js/jquery.min.js'); ?>"></script>
    <script language="javascript">
        <?php
        
        if($login) {
            
        ?>
        $(document).ready( function () {
        
            $('#login_submit').click( function () {
            
                $('#loading').show();
                
                data_form={'username': $('#username_form').val(), 'password': $('#password_form').val(), 'csrf_token': $("#csrf_token").val()};
            
                if($('#remember_login:checked').val())
                {
                    
                    data_form.remember_login=$('#remember_login').val();
                }
            
                $.ajax({
                url: "<?php echo Url::make_url('admin', 'app', 'login_check'); ?>",
                method: "POST",
                dataType: "json",
                data: data_form
                }).done(function(data) {
                
                    console.log(data);
                
                    if(data.error==0)
                    {
                        
                        //location.reload()
                        location.href="<?php echo Url::make_url('admin'); ?>";
                    
                    }
                    else
                    {
                        
                        //$('#csrf_token').val(data.csrf_token);
                        
                        // 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');

                        $('#username_error').html("<?php echo _('Error, wrong username or password'); ?>");
                        
                    }
                
                });
                
                return false;
            
            });
        
        });
        <?php
        }
        else {
        ?>
        $(document).ready( function () {
            
            $("#login_submit").click( function () {
                
                $('#loading').show();
                
                $.ajax({
                url: "<?php echo Url::make_url('admin', 'app', 'signup_check'); ?>",
                method: "POST",
                dataType: "json",
                data: {'username': $('#username_form').val(), 'email': $('#email_form').val(), 'password': $('#password_form').val(), 'repeat_password': $('#repeat_password_form').val(),  'csrf_token': $('#csrf_token').val()}
                }).done(function(data) {
                //$( this ).addClass( "done" );
                //Redirect if register
                
                
                    $('#username_error').html("");
                    $('#email_error').html("");
                    $('#password_error').html("");
                
                    if(data.error==0)
                    {
                    
                        //$('#result_register').html('Done!. Redirecting...');
                        $('#loading').hide();
                        window.location.href="<?php echo Url::make_url('admin'); ?>";
                    
                    }
                    else
                    {
                        
                        $('#loading').hide();
                        
                        $('#csrf_token').attr('value', data.csrf_token);
                        
                        $('#username_error').html(data.username);
                        $('#email_error').html(data.email);
                        $('#password_error').html(data.password);
                        
                        $('#repeat_password_error').html(data.password_repeat);
                    }
                
                });
                
                return false;
                
            });
            
        
        });
        <?php
        }
        ?>
    </script>
    </head>
    <body>
        <?php
        
        if($login) {
        
        ?>
        <form id="login">
            <div id="title">
                <?php echo _('Login'); ?>
            </div>
              <div class="form">
                <p><label for="username"><?php echo _('Username'); ?>:</label>
                <input type="text" id="username_form" name="username" required><span class="error" id="username_error"></span></p>
                <label for="password"><?php echo _('Password'); ?>:</label>
                <input type="password" id="password_form" name="password" required>
                <?php Utils::set_csrf_key(); ?>
              </div>
            <div class="form">
                <?php echo _('Remember login?'); ?> <input type="checkbox" id="remember_login" name="remember_login" value="1">
            </div>
            <div id="submit_block">
                <input type="submit" value="<?php echo ('Login'); ?>" class="submit" id="login_submit"/>
                <span id="loading">&nbsp;</span>
            </div>
            <div class="form"><?php echo _('Remember that only have 3 attempts'); ?></div>
        </form>
        <?php
        }
        else {
        ?>
         <form id="login">
            <div id="title">
                <?php echo _('Signup'); ?>
            </div>
              <div class="form">
            <p><label><?php echo _('Username'); ?> *  </label><input type="text" class="" name="username" id="username_form" value="" /> <span class="error" id="username_error"></span></p>
            <p><label><?php echo _('Email'); ?> *  </label><input type="text" class="" name="email" id="email_form" value="" /> <span class="error" id="email_error"></span></p>
            <p><label><?php echo _('Password'); ?> *  </label><input type="password" class="" name="password" id="password_form" value="" /> <span class="error" id="password_error"></span></p>
            <p><label><?php echo _('Repeat Password'); ?> *  </label><input type="password" class="" name="repeat_password" id="repeat_password_form" value="" /> <span class="error" id="repeat_password_error"></span></p>
            <?php Utils::set_csrf_key(); ?>
</div>

    <div id="result_register"></div>
            <div id="submit_block">
                <input type="submit" value="<?php echo ('Signup'); ?>" class="submit" id="login_submit"/>
                <span id="loading">&nbsp;</span>
            </div>
        </form>
        <?php
        }
        ?>
    </body>
</html>

<?php

}

