<?php

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

allow_roles(['admin']);

$page_title =
    'Student Import Errors';

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

/*
|--------------------------------------------------------------------------
| FETCH ERRORS
|--------------------------------------------------------------------------
*/

$errors =
    $_SESSION['student_import_errors']
    ?? [];

/*
|--------------------------------------------------------------------------
| TOTAL ERRORS
|--------------------------------------------------------------------------
*/

$total_errors =
    count($errors);

/*
|--------------------------------------------------------------------------
| CLEAR SESSION
|--------------------------------------------------------------------------
*/

unset(
    $_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>

                        Student Import Errors

                    </h1>

                </div>

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

                    <a
                        href="import_students.php"

                        class="
                            btn
                            btn-primary
                        "
                    >

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

                        Back To Import

                    </a>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

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

                <!-- CARD HEADER -->

                <div class="card-header">

                    <h3 class="card-title">

                        Import Validation Errors

                    </h3>

                    <div class="card-tools">

                        <span
                            class="
                                badge
                                badge-light
                                px-3
                                py-2
                            "
                        >

                            <?= $total_errors ?>

                            Errors

                        </span>

                    </div>

                </div>

                <!-- CARD BODY -->

                <div class="card-body">

                    <?php if (
                        empty($errors)
                    ) { ?>

                        <!-- NO ERRORS -->

                        <div
                            class="
                                text-center
                                py-5
                            "
                        >

                            <i
                                class="
                                    fas
                                    fa-check-circle
                                    fa-4x
                                    text-success
                                    mb-3
                                "
                            ></i>

                            <h4>

                                No Errors Found

                            </h4>

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

                                Student import completed successfully.

                            </p>

                        </div>

                    <?php } else { ?>

                        <!-- ERROR ALERT -->

                        <div
                            class="
                                alert
                                alert-danger
                            "
                        >

                            <strong>

                                Import Failed Rows:

                            </strong>

                            <?= $total_errors ?>

                        </div>

                        <!-- ERROR TABLE -->

                        <div
                            class="
                                table-responsive
                            "

                            style="
                                max-height:500px;
                                overflow:auto;
                            "
                        >

                            <table
                                class="
                                    table
                                    table-bordered
                                    table-striped
                                    table-hover
                                "
                            >

                                <thead
                                    class="
                                        thead-dark
                                    "
                                >

                                    <tr>

                                        <th
                                            style="
                                                width:80px;
                                            "
                                        >

                                            #

                                        </th>

                                        <th>

                                            Error Message

                                        </th>

                                    </tr>

                                </thead>

                                <tbody>

                                    <?php foreach (

                                        $errors
                                        as $index => $error

                                    ) { ?>

                                        <tr>

                                            <!-- SERIAL -->

                                            <td
                                                class="
                                                    text-center
                                                    align-middle
                                                "
                                            >

                                                <?= $index + 1 ?>

                                            </td>

                                            <!-- MESSAGE -->

                                            <td
                                                class="
                                                    align-middle
                                                    text-danger
                                                "
                                            >

                                                <i
                                                    class="
                                                        fas
                                                        fa-times-circle
                                                        mr-2
                                                    "
                                                ></i>

                                                <?= htmlspecialchars(
                                                    $error
                                                ) ?>

                                            </td>

                                        </tr>

                                    <?php } ?>

                                </tbody>

                            </table>

                        </div>

                    <?php } ?>

                </div>

                <!-- CARD FOOTER -->

                <div class="card-footer">

                    <div
                        class="
                            d-flex
                            justify-content-between
                            flex-wrap
                        "
                    >

                        <!-- BACK -->

                        <a
                            href="import_students.php"

                            class="
                                btn
                                btn-primary
                                mb-2
                            "
                        >

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

                            Back To Import

                        </a>

                        <!-- PRINT -->

                        <?php if (
                            !empty($errors)
                        ) { ?>

                            <button
                                type="button"

                                class="
                                    btn
                                    btn-danger
                                    mb-2
                                "

                                onclick="window.print()"
                            >

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

                                Print Errors

                            </button>

                        <?php } ?>

                    </div>

                </div>

            </div>

        </div>

    </section>

</div>

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