<?php

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

allow_roles(['admin']);

$school_id =
    $_SESSION['school_id'];

$page_title =
    'Student Promotion';

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

/*
|--------------------------------------------------------------------------
| CURRENT ACTIVE SESSION
|--------------------------------------------------------------------------
*/

$current_session_id =
    get_current_session_id($pdo);

/*
|--------------------------------------------------------------------------
| FETCH SESSIONS
|--------------------------------------------------------------------------
*/

$sessionStmt = $pdo->prepare("

    SELECT
        id,
        session_name

    FROM academic_sessions

    WHERE school_id = ?

    ORDER BY id DESC

");

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

$sessions =
    $sessionStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FETCH CLASSES
|--------------------------------------------------------------------------
*/

$classStmt = $pdo->prepare("

    SELECT
        id,
        class_name

    FROM classes

    WHERE school_id = ?

    ORDER BY class_name ASC

");

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

$classes =
    $classStmt->fetchAll(PDO::FETCH_ASSOC);

/*
|--------------------------------------------------------------------------
| FILTERS
|--------------------------------------------------------------------------
*/

$current_session_id =
    (int) (
        $_GET['current_session_id']
        ?? $current_session_id
    );

$current_class_id =
    (int) (
        $_GET['current_class_id']
        ?? 0
    );

/*
|--------------------------------------------------------------------------
| FETCH STUDENTS
|--------------------------------------------------------------------------
*/

$students = [];

if (
    $current_session_id > 0 &&
    $current_class_id > 0
) {

    $studentStmt = $pdo->prepare("

        SELECT

            ss.id AS student_session_id,

            ss.student_id,
            ss.roll_no,
            ss.section,

            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 = ?
        AND ss.session_id = ?
        AND ss.class_id = ?
        AND ss.status = 'Active'

        ORDER BY
            CAST(ss.roll_no AS UNSIGNED) ASC

    ");

    $studentStmt->execute([

        $school_id,
        $current_session_id,
        $current_class_id

    ]);

    $students =
        $studentStmt->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>

                        Student Promotion

                    </h1>

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

    <a
        href="promotion_history.php"

        class="
            btn
            btn-info
        "
    >

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

        Promotion History

    </a>

</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 } ?>

            <!-- PROMOTION ERRORS -->

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

                <a
                    href="promotion_errors.php"
                    class="
                        btn
                        btn-danger
                        mb-3
                    "
                >

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

                    View Promotion Errors

                </a>

            <?php } ?>

            <!-- FILTER CARD -->

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

                <div class="card-header">

                    <h3 class="card-title">

                        Select Students

                    </h3>

                </div>

                <form method="GET">

                    <div class="card-body">

                        <div class="row">

                            <!-- SESSION -->

                            <div
                                class="
                                    col-lg-4
                                    col-md-6
                                    col-12
                                    form-group
                                "
                            >

                                <label>

                                    Current Session

                                </label>

                                <select
                                    name="current_session_id"
                                    class="form-control"
                                    required
                                >

                                    <option value="">

                                        Select Session

                                    </option>

                                    <?php foreach (
                                        $sessions
                                        as $session
                                    ) { ?>

                                        <option
                                            value="<?= $session['id'] ?>"

                                            <?= (
                                                $current_session_id
                                                == $session['id']
                                            )
                                                ? 'selected'
                                                : '' ?>
                                        >

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

                                        </option>

                                    <?php } ?>

                                </select>

                            </div>

                            <!-- CLASS -->

                            <div
                                class="
                                    col-lg-4
                                    col-md-6
                                    col-12
                                    form-group
                                "
                            >

                                <label>

                                    Current Class

                                </label>

                                <select
                                    name="current_class_id"
                                    class="form-control"
                                    required
                                >

                                    <option value="">

                                        Select Class

                                    </option>

                                    <?php foreach (
                                        $classes
                                        as $class
                                    ) { ?>

                                        <option
                                            value="<?= $class['id'] ?>"

                                            <?= (
                                                $current_class_id
                                                == $class['id']
                                            )
                                                ? 'selected'
                                                : '' ?>
                                        >

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

                                        </option>

                                    <?php } ?>

                                </select>

                            </div>

                            <!-- BUTTON -->

                            <div
                                class="
                                    col-lg-4
                                    col-md-12
                                    col-12
                                    form-group
                                    d-flex
                                    align-items-end
                                "
                            >

                                <button
                                    type="submit"
                                    class="
                                        btn
                                        btn-primary
                                        btn-block
                                    "
                                >

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

                                    Load Students

                                </button>

                            </div>

                        </div>

                    </div>

                </form>

            </div>
			            <?php if (!empty($students)) { ?>

                <form
                    action="process_student_promotion.php"
                    method="POST"
                >

                    <!-- HIDDEN FIELDS -->

                    <input
                        type="hidden"
                        name="current_session_id"
                        value="<?= $current_session_id ?>"
                    >

                    <input
                        type="hidden"
                        name="current_class_id"
                        value="<?= $current_class_id ?>"
                    >

                    <!-- PROMOTION CARD -->

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

                        <div class="card-header">

                            <h3 class="card-title">

                                Promote Students

                            </h3>

                        </div>

                        <div class="card-body">

                            <!-- PROMOTION SETTINGS -->

                            <div class="row">

                                <!-- NEW SESSION -->

                                <div
                                    class="
                                        col-lg-3
                                        col-md-6
                                        col-12
                                        form-group
                                    "
                                >

                                    <label>

                                        Promote To Session

                                    </label>

                                    <select
                                        name="new_session_id"
                                        class="form-control"
                                        required
                                    >

                                        <option value="">

                                            Select Session

                                        </option>

                                        <?php foreach (
                                            $sessions
                                            as $session
                                        ) { ?>

                                            <option
                                                value="<?= $session['id'] ?>"
                                            >

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

                                            </option>

                                        <?php } ?>

                                    </select>

                                </div>

                                <!-- NEW CLASS -->

                                <div
                                    class="
                                        col-lg-3
                                        col-md-6
                                        col-12
                                        form-group
                                    "
                                >

                                    <label>

                                        Promote To Class

                                    </label>

                                    <select
                                        name="new_class_id"
                                        class="form-control"
                                        required
                                    >

                                        <option value="">

                                            Select Class

                                        </option>

                                        <?php foreach (
                                            $classes
                                            as $class
                                        ) { ?>

                                            <option
                                                value="<?= $class['id'] ?>"
                                            >

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

                                            </option>

                                        <?php } ?>

                                    </select>

                                </div>

                                <!-- NEW SECTION -->

                                <div
                                    class="
                                        col-lg-3
                                        col-md-6
                                        col-12
                                        form-group
                                    "
                                >

                                    <label>

                                        New Section

                                    </label>

                                    <input
                                        type="text"
                                        name="new_section"
                                        class="form-control"
                                        placeholder="A / B / C"
                                    >

                                </div>

                                <!-- PROMOTION TYPE -->

                                <div
                                    class="
                                        col-lg-3
                                        col-md-6
                                        col-12
                                        form-group
                                    "
                                >

                                    <label>

                                        Promotion Type

                                    </label>

                                    <select
                                        name="promotion_type"
                                        class="form-control"
                                        required
                                    >

                                        <option value="pass">

                                            Pass / Promote

                                        </option>

                                        <option value="fail">

                                            Fail / Same Class

                                        </option>

                                    </select>

                                </div>

                            </div>

                            <!-- STUDENT TABLE -->

                            <div class="table-responsive">

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

                                    <thead
                                        class="
                                            thead-dark
                                        "
                                    >

                                        <tr>

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

                                                <input
                                                    type="checkbox"
                                                    id="select_all"
                                                >

                                            </th>

                                            <th>

                                                Roll No

                                            </th>

                                            <th>

                                                Student Name

                                            </th>

                                            <th>

                                                Scholar No

                                            </th>

                                            <th>

                                                Father Name

                                            </th>

                                            <th>

                                                Current Class

                                            </th>

                                            <th>

                                                Current Session

                                            </th>

                                        </tr>

                                    </thead>

                                    <tbody>

                                        <?php foreach (
                                            $students
                                            as $student
                                        ) { ?>

                                            <tr>

                                                <!-- CHECKBOX -->

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

                                                    <input
                                                        type="checkbox"
                                                        name="student_ids[]"

                                                        value="<?= $student['student_id'] ?>"
                                                    >

                                                </td>

                                                <!-- ROLL -->

                                                <td>

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

                                                </td>

                                                <!-- NAME -->

                                                <td>

                                                    <strong>

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

                                                    </strong>

                                                </td>

                                                <!-- SCHOLAR -->

                                                <td>

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

                                                </td>

                                                <!-- FATHER -->

                                                <td>

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

                                                </td>

                                                <!-- CLASS -->

                                                <td>

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

                                                </td>

                                                <!-- SESSION -->

                                                <td>

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

                                                </td>

                                            </tr>

                                        <?php } ?>

                                    </tbody>

                                </table>

                            </div>
							                        </div>

                        <!-- CARD FOOTER -->

                        <div class="card-footer">

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

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

                                    <small class="text-muted">

                                        Selected students will
                                        move into new academic
                                        session.

                                    </small>

                                </div>

                                <div
                                    class="
                                        col-lg-6
                                        col-md-12
                                        text-lg-right
                                    "
                                >

                                    <button
                                        type="submit"

                                        class="
                                            btn
                                            btn-success
                                            btn-block
                                            btn-lg
                                        "

                                        onclick="
                                            return confirm(
                                                'Are you sure you want to process promotion?'
                                            );
                                        "
                                    >

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

                                        Process Promotion

                                    </button>

                                </div>

                            </div>

                        </div>

                    </div>

                </form>

            <?php } else { ?>

                <!-- EMPTY STATE -->

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

                    <div class="card-body">

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

                            <i
                                class="
                                    fas
                                    fa-user-graduate
                                    fa-4x
                                    text-muted
                                    mb-3
                                "
                            ></i>

                            <h4>

                                No Students Loaded

                            </h4>

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

                                Please select session and class
                                to load active students.

                            </p>

                        </div>

                    </div>

                </div>

            <?php } ?>

        </div>

    </section>

</div>

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

<!-- =========================================================
MOBILE FRIENDLY CHECKBOX SCRIPT
========================================================= -->

<script>

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

            let checkboxes =
                document.querySelectorAll(
                    'input[name="student_ids[]"]'
                );

            checkboxes.forEach(function (checkbox) {

                checkbox.checked =
                    document.getElementById(
                        'select_all'
                    ).checked;

            });

        }
    );

</script>