<?php

require_once __DIR__ . '/../library/auth.php';

allow_roles(['admin']);

$school_id =
    $_SESSION['school_id'];

$page_title =
    'Import Students';

require_once __DIR__ . '/../include/header.php';
require_once __DIR__ . '/../include/sidebar.php';

/*
|--------------------------------------------------------------------------
| IMPORT ERROR STATUS
|--------------------------------------------------------------------------
*/

$has_import_errors =
    !empty(
        $_SESSION['student_import_errors']
    );

?>

<div class="content-wrapper">

    <!-- PAGE HEADER -->

    <section class="content-header">

        <div class="container-fluid">

            <div
                class="
                    row
                    mb-2
                    align-items-center
                "
            >

                <div class="col-sm-6">

                    <h1>

                        Import Students

                    </h1>

                </div>

                <div
                    class="
                        col-sm-6
                        text-sm-right
                        mt-2
                        mt-sm-0
                    "
                >

                    <?php if (
                        $has_import_errors
                    ) { ?>

                        <a
                            href="student_import_errors.php"

                            class="
                                btn
                                btn-danger
                            "
                        >

                            <i
                                class="
                                    fas
                                    fa-exclamation-circle
                                    mr-1
                                "
                            ></i>

                            View Import Errors

                        </a>

                    <?php } ?>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- SUCCESS MESSAGE -->

            <?php if (
                isset(
                    $_SESSION['success_message']
                )
            ) { ?>

                <div
                    class="
                        alert
                        alert-success
                    "
                >

                    <?= htmlspecialchars(
                        $_SESSION['success_message']
                    ) ?>

                </div>

                <?php

                unset(
                    $_SESSION['success_message']
                );

                ?>

            <?php } ?>

            <!-- ERROR MESSAGE -->

            <?php if (
                isset(
                    $_SESSION['error_message']
                )
            ) { ?>

                <div
                    class="
                        alert
                        alert-danger
                    "
                >

                    <?= htmlspecialchars(
                        $_SESSION['error_message']
                    ) ?>

                </div>

                <?php

                unset(
                    $_SESSION['error_message']
                );

                ?>

            <?php } ?>

            <!-- IMPORT CARD -->

            <div
                class="
                    card
                    card-primary
                    card-outline
                "
            >

                <!-- CARD HEADER -->

                <div class="card-header">

                    <h3 class="card-title">

                        Upload Student Excel File

                    </h3>

                </div>

                <!-- FORM -->

                <form

                    action="process_student_excel.php"

                    method="POST"

                    enctype="multipart/form-data"

                >

                    <div class="card-body">

                        <!-- INSTRUCTIONS -->

                        <div
                            class="
                                alert
                                alert-info
                            "
                        >

                            <h5>

                                <i
                                    class="
                                        fas
                                        fa-info-circle
                                        mr-1
                                    "
                                ></i>

                                Import Instructions

                            </h5>

                            <ul class="mb-0">

                                <li>

                                    Allowed formats:
                                    .xlsx, .xls, .csv

                                </li>

                                <li>

                                    Maximum upload size:
                                    5MB

                                </li>

                                <li>

                                    First row must contain
                                    column headers

                                </li>

                                <li>

                                    Class names must already
                                    exist in ERP

                                </li>

                                <li>

                                    Current active session
                                    will be assigned automatically

                                </li>

                            </ul>

                        </div>

                        <!-- WARNING -->

                        <div
                            class="
                                alert
                                alert-warning
                            "
                        >

                            <strong>

                                Important:

                            </strong>

                            Duplicate roll numbers
                            inside same class
                            will be skipped automatically.

                        </div>

                        <!-- FILE INPUT -->

                        <div class="form-group">

                            <label>

                                Select Excel File

                                <span class="text-danger">

                                    *

                                </span>

                            </label>

                            <div class="custom-file">

                                <input

                                    type="file"

                                    name="student_excel"

                                    id="student_excel"

                                    class="
                                        custom-file-input
                                    "

                                    accept=".xlsx,.xls,.csv"

                                    required
                                >

                                <label
                                    class="
                                        custom-file-label
                                    "

                                    for="student_excel"
                                >

                                    Choose File

                                </label>

                            </div>

                        </div>

                        <!-- ACTION BUTTONS -->

                        <div
                            class="
                                row
                                mt-4
                            "
                        >

                            <!-- SAMPLE -->

                            <div
                                class="
                                    col-lg-6
                                    col-md-6
                                    col-12
                                    mb-2
                                "
                            >

                                <a
                                    href="download_student_sample.php"

                                    class="
                                        btn
                                        btn-success
                                        btn-block
                                    "
                                >

                                    <i
                                        class="
                                            fas
                                            fa-download
                                            mr-1
                                        "
                                    ></i>

                                    Download Sample File

                                </a>

                            </div>

                            <!-- UPLOAD -->

                            <div
                                class="
                                    col-lg-6
                                    col-md-6
                                    col-12
                                    mb-2
                                "
                            >

                                <button

                                    type="submit"

                                    class="
                                        btn
                                        btn-primary
                                        btn-block
                                    "

                                >

                                    <i
                                        class="
                                            fas
                                            fa-upload
                                            mr-1
                                        "
                                    ></i>

                                    Upload & Preview

                                </button>

                            </div>

                        </div>

                    </div>

                </form>

            </div>

        </div>

    </section>

</div>

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

<!-- =========================================================
FILE INPUT LABEL SCRIPT
========================================================= -->

<script>

document
    .getElementById('student_excel')
    ?.addEventListener(
        'change',
        function (e) {

            let fileName =
                e.target.files[0]?.name
                || 'Choose File';

            document.querySelector(
                '.custom-file-label'
            ).innerText = fileName;

        }
    );

</script>