<?php

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

allow_roles(['admin']);

$school_id =
    $_SESSION['school_id'];

$page_title =
    'Promotion History';

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

/*
|--------------------------------------------------------------------------
| FETCH PROMOTION HISTORY
|--------------------------------------------------------------------------
*/

$stmt = $pdo->prepare("

    SELECT

        ss.id,
        ss.student_id,

        ss.session_id,
        ss.class_id,

        ss.section,
        ss.roll_no,

        ss.status,
        ss.created_at,

        s.name,
        s.enrollment_no,

        sp.father_name,

        c.class_name,

        ac.session_name

    FROM student_sessions ss

    INNER JOIN students s
        ON s.id = ss.student_id

    LEFT JOIN student_parents sp
        ON sp.student_id = s.id

    INNER JOIN classes c
        ON c.id = ss.class_id

    INNER JOIN academic_sessions ac
        ON ac.id = ss.session_id

    WHERE ss.school_id = ?

    ORDER BY ss.id DESC

");

$stmt->execute([
    $school_id
]);

$history =
    $stmt->fetchAll(PDO::FETCH_ASSOC);

?>

<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>

                        Promotion History

                    </h1>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

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

                <!-- CARD HEADER -->

                <div class="card-header">

                    <h3 class="card-title">

                        Student Session History

                    </h3>

                </div>

                <!-- CARD BODY -->

                <div class="card-body">

                    <div class="table-responsive">

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

                            <thead
                                class="
                                    thead-dark
                                "
                            >

                                <tr>

                                    <th>

                                        ID

                                    </th>

                                    <th>

                                        Student

                                    </th>

                                    <th>

                                        Scholar No

                                    </th>

                                    <th>

                                        Father Name

                                    </th>

                                    <th>

                                        Roll No

                                    </th>

                                    <th>

                                        Class

                                    </th>

                                    <th>

                                        Section

                                    </th>

                                    <th>

                                        Session

                                    </th>

                                    <th>

                                        Status

                                    </th>

                                    <th>

                                        Date

                                    </th>

                                </tr>

                            </thead>

                            <tbody>

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

                                    <?php foreach (
                                        $history as $row
                                    ) { ?>

                                        <?php

                                        $badge =
                                            'secondary';

                                        if (
                                            $row['status']
                                            === 'Active'
                                        ) {

                                            $badge =
                                                'success';

                                        } elseif (
                                            $row['status']
                                            === 'Promoted'
                                        ) {

                                            $badge =
                                                'info';

                                        } elseif (
                                            $row['status']
                                            === 'Failed'
                                        ) {

                                            $badge =
                                                'warning';

                                        } elseif (
                                            $row['status']
                                            === 'TC'
                                        ) {

                                            $badge =
                                                'danger';

                                        }

                                        ?>

                                        <tr>
										                                            <!-- ID -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <?= $row['id'] ?>

                                            </td>

                                            <!-- STUDENT -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <strong>

                                                    <?= htmlspecialchars(
                                                        $row['name']
                                                    ) ?>

                                                </strong>

                                            </td>

                                            <!-- SCHOLAR -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <?= htmlspecialchars(
                                                    $row['enrollment_no']
                                                    ?? '-'
                                                ) ?>

                                            </td>

                                            <!-- FATHER -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <?= htmlspecialchars(
                                                    $row['father_name']
                                                    ?? '-'
                                                ) ?>

                                            </td>

                                            <!-- ROLL -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <?= htmlspecialchars(
                                                    $row['roll_no']
                                                    ?? '-'
                                                ) ?>

                                            </td>

                                            <!-- CLASS -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <?= htmlspecialchars(
                                                    $row['class_name']
                                                ) ?>

                                            </td>

                                            <!-- SECTION -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <?= htmlspecialchars(
                                                    $row['section']
                                                    ?? '-'
                                                ) ?>

                                            </td>

                                            <!-- SESSION -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <?= htmlspecialchars(
                                                    $row['session_name']
                                                ) ?>

                                            </td>

                                            <!-- STATUS -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

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

                                                    <?= htmlspecialchars(
                                                        $row['status']
                                                    ) ?>

                                                </span>

                                            </td>

                                            <!-- DATE -->

                                            <td
                                                class="
                                                    align-middle
                                                "
                                            >

                                                <?= date(

                                                    'd-m-Y',

                                                    strtotime(
                                                        $row['created_at']
                                                    )

                                                ) ?>

                                            </td>

                                        </tr>

                                    <?php } ?>

                                <?php } else { ?>

                                    <!-- EMPTY STATE -->

                                    <tr>

                                        <td
                                            colspan="10"

                                            class="
                                                text-center
                                                py-5
                                            "
                                        >

                                            <div
                                                class="
                                                    text-muted
                                                "
                                            >

                                                <i
                                                    class="
                                                        fas
                                                        fa-history
                                                        fa-4x
                                                        mb-3
                                                    "
                                                ></i>

                                                <h4>

                                                    No Promotion History Found

                                                </h4>

                                                <p class="mb-0">

                                                    No student session
                                                    history available.

                                                </p>

                                            </div>

                                        </td>

                                    </tr>

                                <?php } ?>

                            </tbody>

                        </table>

                    </div>

                </div>

                <!-- CARD FOOTER -->

                <div class="card-footer">

                    <small class="text-muted">

                        Student promotion, fail,
                        TC and academic history
                        records are displayed here.

                    </small>

                </div>

            </div>

        </div>

    </section>

</div>

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