<?php
session_start();

include 'config/koneksi.php';

/*
|--------------------------------------------------------------------------
| Ambil profil sekolah
|--------------------------------------------------------------------------
*/

$profil = [];

$qProfil = $conn->query(
    "SELECT * FROM profil_sekolah LIMIT 1"
);

if ($qProfil && $qProfil->num_rows > 0) {

    $profil = $qProfil->fetch_assoc();
}

/*
|--------------------------------------------------------------------------
| Cegah redirect berulang
|--------------------------------------------------------------------------
*/

if (!empty($_SESSION['login'])) {

    $role = $_SESSION['role'] ?? '';

    if ($role == 'siswa') {

        if (!empty($_SESSION['siswa_id'])) {

            header("Location: siswa/status_lulus.php");
            exit;
        }

        session_unset();
        session_destroy();

        header("Location: index.php");
        exit;
    }

    if (
        $role == 'admin'
        ||
        $role == 'guru'
    ) {

        header("Location: admin/dashboard.php");
        exit;
    }

    session_unset();
    session_destroy();

    header("Location: index.php");
    exit;
}

$error = $_GET['error'] ?? '';

?>
<!doctype html>

<html lang="id">

<head>

    <meta charset="utf-8">

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

    <title>

        <?= e(
            $profil['nama_sekolah']
                ?? 'Kartu Ujian'
        ) ?>

    </title>

    <link
        href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css"
        rel="stylesheet">

    <link
        rel="stylesheet"
        href="assets/css/style.css">

    <style>
        body {

            background: #f5f6fa;

        }

        .login-page {

            display: flex;

            justify-content: center;

            align-items: center;

            min-height: 100vh;

            padding: 20px;

        }

        .login-card {

            width: 100%;

            max-width: 470px;

            background: white;

            padding: 35px;

            border-radius: 20px;

        }

        .logo-sekolah {

            width: 110px;

            height: 110px;

            object-fit: contain;

        }

        .nav-pills .nav-link {

            font-weight: 600;

        }
    </style>

</head>

<body class="login-page">

    <div class="login-card shadow-lg">

        <div class="text-center mb-4">

            <?php
            if (
                !empty($profil['logo'])
            ):
            ?>

                <img

                    src="assets/uploads/<?= e($profil['logo']) ?>"

                    alt="Logo Sekolah"

                    class="logo-sekolah mb-3">

            <?php endif; ?>

            <h3 class="fw-bold">

                <?= strtoupper(
                    e(
                        $profil['nama_sekolah']
                            ?? 'APLIKASI KARTU UJIAN'
                    )
                ) ?>

            </h3>

            <p class="text-muted mb-0">

                Login Admin,
                Guru,
                dan Siswa

            </p>

        </div>

        <?php if ($error == 'pegawai'): ?>

            <div class="alert alert-danger">

                Username atau Password salah

            </div>

        <?php endif; ?>

        <?php if ($error == 'siswa'): ?>

            <div class="alert alert-danger">

                Nomor peserta tidak ditemukan

            </div>

        <?php endif; ?>

        <ul
            class="nav nav-pills nav-fill mb-4">

            <li class="nav-item">

                <button

                    class="nav-link active"

                    data-bs-toggle="pill"

                    data-bs-target="#pegawai"

                    type="button">

                    Admin / Guru

                </button>

            </li>

            <li class="nav-item">

                <button

                    class="nav-link"

                    data-bs-toggle="pill"

                    data-bs-target="#siswa"

                    type="button">

                    Pengumuman

                </button>

            </li>

        </ul>

        <div class="tab-content">

            <div

                class="tab-pane fade show active"

                id="pegawai">

                <form

                    method="post"

                    action="auth/login.php">

                    <input

                        type="hidden"

                        name="jenis_login"

                        value="pegawai">

                    <div class="mb-3">

                        <label>

                            Username

                        </label>

                        <input

                            type="text"

                            name="username"

                            class="form-control"

                            required

                            autofocus>

                    </div>

                    <div class="mb-3">

                        <label>

                            Password

                        </label>

                        <input

                            type="password"

                            name="password"

                            class="form-control"

                            required>

                    </div>

                    <button

                        class="btn btn-primary w-100">

                        Masuk

                    </button>

                </form>

            </div>

            <div

                class="tab-pane fade"

                id="siswa">

                <form

                    method="post"

                    action="auth/login.php">

                    <input

                        type="hidden"

                        name="jenis_login"

                        value="siswa">

                    <div class="mb-3">

                        <label>

                            No Peserta

                        </label>

                        <input

                            type="text"

                            name="no_ujian"

                            class="form-control"

                            placeholder="Contoh : 0103250001"

                            required>

                    </div>

                    <button

                        class="btn btn-success w-100">

                        Cek Kelulusan

                    </button>

                    <p
                        class="
small
text-center
text-muted
mt-3
mb-0
">

                        Memasukkan
                        Nomor Peserta Ujian

                    </p>

                </form>

            </div>

        </div>

    </div>

    <script

        src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js">

    </script>

</body>

</html>