<?php
declare(strict_types=1);

require_once __DIR__ . '/../includes/config.php';
require_once __DIR__ . '/../includes/db.php';
require_once __DIR__ . '/../includes/functions.php';
require_once __DIR__ . '/../includes/admin-auth.php';

$pdo = db();

if (isAdminLoggedIn()) {

    redirectTo('dashboard.php');
}

$error = '';

if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    requirePostCsrf();

    $email =
        trim($_POST['email'] ?? '');

    $password =
        trim($_POST['password'] ?? '');

    if (
        $email === ''
        ||
        $password === ''
    ) {

        $error =
            'Please complete all fields.';
    }

    else {

        if (
            adminLogin(
                $pdo,
                $email,
                $password
            )
        ) {

            header(
                'Location: dashboard.php'
            );

            exit;
        }

        $error =
            'Invalid login credentials.';
    }
}
?>

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

<head>

<meta charset="UTF-8">

<meta
name="viewport"
content="width=device-width, initial-scale=1.0">

<title>
SMI Admin Login
</title>

<link
href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap"
rel="stylesheet">

<style>

*{

    margin:0;
    padding:0;
    box-sizing:border-box;
}

body{

    font-family:'Inter',sans-serif;

    min-height:100vh;

    display:flex;

    align-items:center;

    justify-content:center;

    overflow:hidden;

    background:
    linear-gradient(
        135deg,
        #0f172a,
        #111827,
        #1e293b
    );
}

.background{

    position:absolute;
    inset:0;

    overflow:hidden;
}

.circle{

    position:absolute;

    border-radius:50%;

    filter:blur(80px);

    opacity:0.3;
}

.circle.one{

    width:400px;
    height:400px;

    background:#2563eb;

    top:-100px;
    left:-100px;
}

.circle.two{

    width:350px;
    height:350px;

    background:#7c3aed;

    bottom:-100px;
    right:-100px;
}

.login-card{

    position:relative;

    width:100%;
    max-width:520px;

    background:
    rgba(255,255,255,0.08);

    backdrop-filter:blur(18px);

    border:
    1px solid rgba(255,255,255,0.1);

    border-radius:36px;

    padding:42px;

    box-shadow:
        0 25px 70px rgba(0,0,0,0.45);

    z-index:10;
}

.logo{

    width:95px;
    height:95px;

    border-radius:30px;

    margin:auto;

    display:flex;

    align-items:center;

    justify-content:center;

    font-size:42px;

    font-weight:900;

    color:#fff;

    background:
    linear-gradient(
        135deg,
        #2563eb,
        #7c3aed
    );

    margin-bottom:24px;
}

.title{

    text-align:center;

    margin-bottom:34px;
}

.title h1{

    color:#fff;

    font-size:42px;

    font-weight:900;

    margin-bottom:12px;
}

.title p{

    color:#cbd5e1;

    line-height:1.8;
}

.form-group{

    margin-bottom:22px;
}

.form-group label{

    display:block;

    margin-bottom:10px;

    color:#fff;

    font-weight:700;
}

.form-control{

    width:100%;

    border:none;

    outline:none;

    border-radius:18px;

    padding:18px;

    background:
    rgba(255,255,255,0.08);

    color:#fff;

    font-size:15px;

    border:
    1px solid rgba(255,255,255,0.08);
}

.form-control::placeholder{

    color:#cbd5e1;
}

.login-btn{

    width:100%;

    border:none;

    padding:18px;

    border-radius:20px;

    background:
    linear-gradient(
        135deg,
        #2563eb,
        #1d4ed8
    );

    color:#fff;

    font-size:16px;

    font-weight:900;

    cursor:pointer;

    transition:0.25s;
}

.login-btn:hover{

    transform:translateY(-2px);

    box-shadow:
        0 18px 35px rgba(37,99,235,0.35);
}

.alert{

    padding:16px;

    border-radius:16px;

    margin-bottom:24px;

    font-weight:700;
}

.alert-danger{

    background:#7f1d1d;

    color:#fecaca;
}

.footer{

    margin-top:24px;

    text-align:center;

    color:#94a3b8;

    font-size:13px;
}

</style>

</head>

<body>

<div class="background">

    <div class="circle one"></div>

    <div class="circle two"></div>

</div>

<div class="login-card">

    <div class="logo">

        A

    </div>

    <div class="title">

        <h1>

            Admin Portal

        </h1>

        <p>

            SMI Student Gateway
            Administration System

        </p>

    </div>

    <?php if($error): ?>

        <div class="alert alert-danger">

            <?php echo htmlspecialchars($error); ?>

        </div>

    <?php endif; ?>

    <form method="POST">

        <?php echo csrfField(); ?>

        <div class="form-group">

            <label>

                Admin Email

            </label>

            <input
            type="email"
            name="email"
            class="form-control"
            placeholder="admin@smi.com"
            required>

        </div>

        <div class="form-group">

            <label>

                Password

            </label>

            <input
            type="password"
            name="password"
            class="form-control"
            placeholder="••••••••"
            required>

        </div>

        <button
        type="submit"
        class="login-btn">

            Login To Dashboard

        </button>

    </form>

    <div class="footer">

        © SMI Student Gateway

    </div>

</div>

</body>
</html>