<?php
session_start();

// ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);

if (count($_SESSION) > 1){
    header('Location: ./dashboard.php');
}

$page = (object)[
    'title'       =>  'Login',
    'description' =>  "Login to SSHub portal",
    'author'      =>  'Bowen DDS'
];
?>

<!DOCTYPE html>
<html lang="en">

<head>

    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <meta name="description" content=" <?= $page->description ?> ">
    <meta name="author" content=" <?= $page->author ?> ">
    <link rel="icon" href="img/bowen-bw.png">

    <title> <?= $page->title ?> </title>

    <!-- Custom fonts for this template-->
    <link href="vendor/fontawesome-free/css/all.min.css" rel="stylesheet" type="text/css">
    <link href="https://fonts.googleapis.com/css?family=Nunito:200,200i,300,300i,400,400i,600,600i,700,700i,800,800i,900,900i" rel="stylesheet">

    <!-- Custom styles for this template-->
    <link href="css/sb-admin-2.min.css" rel="stylesheet">

</head>

<body class="bg-gradient-primary" style="background-image: url('img/login-bg.jpg');">

    <div class="container">

        <!-- Outer Row -->
        <div class="row justify-content-center">

            <div class="col-xl-5 col-lg-6 col-md-8">

                <div class="card o-hidden border-0 shadow-lg my-5" style="opacity:0.9">
                    <div class="card-body p-0">
                        <!-- Nested Row within Card Body -->
                        <div class="row">
                            <!-- <div class="col-lg-6 d-none d-lg-block bg-login-image"></div> -->
                            <div class="col-lg-12">
                                <div class="p-5">
                                    <div class="text-center">
                                        <h1 class="h3 text-gray-900 mb-4 font-weight-bold">SSHUB <sup>PG</sup></h1>
                                        <h2 class="h5 text-gray-900 mb-4">Welcome Back!</h2>
                                    </div>
                                    <form class="user" id='login-form'>
                                        <div class="form-group">
                                            <input type="text" class="form-control form-control-user"
                                                id="id"
                                                placeholder="Matric Number">
                                        </div>
                                        <div class="form-group pb-3">
                                            <input type="password" class="form-control form-control-user"
                                                id="password" placeholder="Password">
                                        </div>
                                        <button type='submit' class="btn btn-primary btn-user btn-block" id='login-btn'>
                                            Login
                                            <span class="spn text-light" role="status">
                                                <span class="sr-only">Loading...</span>
                                            </span>
                                        </button>
                                    </form>
                                    <div class="text-primary text-center mt-3 small">
                                        Default Password: firstname
                                    </div>
                                </div>
                                <div class='text-center small bg-dark text-light' style='margin-top: -20px;'>© Bowen University - Directorate of Digital Services</div>
                            </div>
                        </div>
                    </div>
                </div>

            </div>
        </div>

    </div>

    <!-- Bootstrap core JavaScript-->
    <script src="vendor/jquery/jquery.min.js"></script>
    <script src="vendor/bootstrap/js/bootstrap.bundle.min.js"></script>

    <!-- Core plugin JavaScript-->
    <script src="vendor/jquery-easing/jquery.easing.min.js"></script>

    <!-- Custom scripts for all pages-->
    <script src="js/sb-admin-2.min.js"></script>
    
    <script>
        let auth = false;
        $(document).ready(function(e) {
            // Submit form data via Ajax
            if (auth) {
                return;
            }
            $("#login-form").on('submit', function(e) {
                let id = $('#id').val();
                let password = $('#password').val();
                
                if (id==="" || password===""){
                    e.preventDefault();
                    alert("Enter matric number and or password");
                    return;
                }
        
                $('.spn').addClass("spinner-border").addClass("spinner-border-sm");
                $('#login-btn').prop('disabled', true);
        
                e.preventDefault();
        
                $.ajax({
                    type: 'POST',
                    url: './api/auth.php',
                    data: { id: id, password: password },
                    success: function(resp) {
                        if (resp.data == 1) {
                            auth = false;
                            window.location.replace("./dashboard.php");
                        } else {
                            alert(resp.message);
                        }
                    },
                    fail: function(xhr, textStatus, errorThrown) {
                        alert("Network Error, please retry!");
                        done();
                    },
                    error: function(jqXHR, exception) {
                        if (jqXHR.status === 0) {
                            alert('Network Error.\n Please Verify Network.');
                        } else if (exception === 'timeout') {
                            alert('Time out error.\n Please Verify Network.');
                        } else if (exception === 'abort') {
                            alert('Request aborted.\n Please Verify Network.');
                        } else {
                            alert('Network Error Please Retry! \n if error persist contact Support!');
                        }
                        done();
                    }
                }).done(function() {
                    done();
                })
        
              
            });
        
        });
        
        function done() {
            $('.spn').removeClass("spinner-border").removeClass("spinner-border-sm");
            $('#login-btn').prop('disabled', false);
        }
    </script>

</body>

</html>