<?php

declare(strict_types=1);

require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/csrf.php';

if (($_SESSION['role'] ?? '') !== 'admin') {
    http_response_code(403);
    exit('Access denied.');
}

$successMessage = $_SESSION['success_message'] ?? '';
$errorMessage = $_SESSION['error_message'] ?? '';

unset(
    $_SESSION['success_message'],
    $_SESSION['error_message']
);

include __DIR__ . '/../includes/header.php';
include __DIR__ . '/../includes/navbar.php';
?>

<main class="container mt-4 mb-5">

    <div class="card shadow">

        <div class="card-header bg-primary text-white">
            <strong>
                <i class="fa-solid fa-file-excel me-1"></i>
                Import School Master Data
            </strong>
        </div>

        <div class="card-body">

            <?php if ($successMessage !== ''): ?>
                <div
                    class="alert alert-success alert-dismissible fade show"
                    role="alert"
                >
                    <i class="fa-solid fa-circle-check me-1"></i>

                    <?= htmlspecialchars(
                        $successMessage,
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>

                    <button
                        type="button"
                        class="btn-close"
                        data-bs-dismiss="alert"
                        aria-label="Close"
                    ></button>
                </div>
            <?php endif; ?>

            <?php if ($errorMessage !== ''): ?>
                <div
                    class="alert alert-danger alert-dismissible fade show"
                    role="alert"
                >
                    <i class="fa-solid fa-circle-exclamation me-1"></i>

                    <?= htmlspecialchars(
                        $errorMessage,
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>

                    <button
                        type="button"
                        class="btn-close"
                        data-bs-dismiss="alert"
                        aria-label="Close"
                    ></button>
                </div>
            <?php endif; ?>

            <div class="alert alert-info">

                <strong>Required column order:</strong>

                <ol class="mb-0 mt-2">
                    <li>Census No.</li>
                    <li>District</li>
                    <li>Zone</li>
                    <li>Division</li>
                    <li>School Name</li>
                    <li>School Category</li>
                    <li>School Type</li>
                </ol>

                <div class="small mt-2">
                    The first worksheet is imported. The first row is treated
                    as the header row. Existing schools are updated using
                    Census No.
                </div>

            </div>

            <form
                action="import_school_data.php"
                method="post"
                enctype="multipart/form-data"
            >

                <input
                    type="hidden"
                    name="csrf_token"
                    value="<?= htmlspecialchars(
                        $_SESSION['csrf_token'],
                        ENT_QUOTES,
                        'UTF-8'
                    ) ?>"
                >

                <div class="mb-3">

                    <label
                        for="excel_file"
                        class="form-label"
                    >
                        Select Excel File
                    </label>

                    <input
                        type="file"
                        name="excel_file"
                        id="excel_file"
                        accept=".xlsx,.xls"
                        class="form-control"
                        required
                    >

                    <div class="form-text">
                        Accepted formats: .xlsx and .xls. Maximum file size:
                        10 MB.
                    </div>

                </div>

                <button
                    type="submit"
                    class="btn btn-success"
                >
                    <i class="fa-solid fa-file-import me-1"></i>
                    Import Schools
                </button>

            </form>

        </div>

    </div>

</main>

<?php include __DIR__ . '/../includes/footer.php'; ?>