<?php

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

allow_roles(['admin']);

$school_id =
    $_SESSION['school_id'];

$page_title =
    'Generate Student ID Card';

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

/*
|--------------------------------------------------------------------------
| CURRENT 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
|--------------------------------------------------------------------------
*/

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

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

$template =
    trim(
        $_GET['template']
        ?? 'template1'
    );

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

$students = [];

if (

    $session_id > 0 &&
    $class_id > 0

) {

    $studentStmt = $pdo->prepare("

        SELECT

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

            s.name,
            s.photo,
            s.phone,
            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,
        $session_id,
        $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>

                        Generate Student ID Card

                    </h1>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- FILTER CARD -->

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

                <div class="card-header">

                    <h3 class="card-title">

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

                                    Session

                                </label>

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

                                    <option value="">

                                        Select Session

                                    </option>

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

                                        <option

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

                                            <?= (
                                                $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>

                                    Class

                                </label>

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

                                    <option value="">

                                        Select Class

                                    </option>

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

                                        <option

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

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

                                        >

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

                                        </option>

                                    <?php } ?>

                                </select>

                            </div>

                            <!-- TEMPLATE -->

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

                                <label>

                                    Template

                                </label>

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

                                    <?php for (
                                        $i = 1;
                                        $i <= 5;
                                        $i++
                                    ) { ?>

                                        <option

                                            value="template<?= $i ?>"

                                            <?= (
                                                $template
                                                === 'template' . $i
                                            )
                                                ? 'selected'
                                                : '' ?>

                                        >

                                            Template <?= $i ?>

                                        </option>

                                    <?php } ?>

                                </select>

                            </div>

                        </div>

                    </div>

                    <div class="card-footer">

                        <button
                            type="submit"

                            class="
                                btn
                                btn-primary
                            "
                        >

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

                            Load Students

                        </button>

                    </div>

                </form>

            </div>
			            <!-- EMPTY STATE -->

            <?php if (

                isset($_GET['session_id']) &&

                $session_id > 0 &&
                $class_id > 0 &&

                empty($students)

            ) { ?>

                <div
                    class="
                        alert
                        alert-warning
                    "
                >

                    No active students found.

                </div>

            <?php } ?>

            <!-- STUDENT LIST -->

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

                <form

                    action="print_id_card.php"

                    method="POST"

                    target="_blank"

                >

                    <!-- HIDDEN FIELDS -->

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

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

                    <input
                        type="hidden"
                        name="template"
                        value="<?= htmlspecialchars($template) ?>"
                    >

                    <!-- CARD -->

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

                        <!-- HEADER -->

                        <div class="card-header">

                            <h3 class="card-title">

                                Select Students

                            </h3>

                        </div>

                        <!-- BODY -->

                        <div class="card-body">

                            <!-- ACTION BUTTONS -->

                            <div
                                class="
                                    d-flex
                                    flex-wrap
                                    gap-2
                                    mb-3
                                "
                            >

                                <button

                                    type="button"

                                    class="
                                        btn
                                        btn-sm
                                        btn-info
                                        mr-2
                                        mb-2
                                    "

                                    id="select_all"

                                >

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

                                    Select All

                                </button>

                                <button

                                    type="button"

                                    class="
                                        btn
                                        btn-sm
                                        btn-secondary
                                        mb-2
                                    "

                                    id="unselect_all"

                                >

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

                                    Unselect All

                                </button>

                            </div>

                            <!-- SELECT COUNT -->

                            <div class="mb-3">

                                Selected Students:

                                <strong
                                    id="selected_count"
                                >

                                    0

                                </strong>

                            </div>

                            <!-- TABLE -->

                            <div class="table-responsive">

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

                                    <thead
                                        class="
                                            thead-dark
                                        "
                                    >

                                        <tr>

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

                                                Select

                                            </th>

                                            <th>

                                                Photo

                                            </th>

                                            <th>

                                                Name

                                            </th>

                                            <th>

                                                Scholar No

                                            </th>

                                            <th>

                                                Roll No

                                            </th>

                                            <th>

                                                Admission No

                                            </th>

                                            <th>

                                                Father Name

                                            </th>

                                            <th>

                                                Class

                                            </th>

                                            <th>

                                                Section

                                            </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'] ?>"

                                                        class="
                                                            student_checkbox
                                                        "

                                                    >

                                                </td>

                                                <!-- PHOTO -->

                                                <td
                                                    class="
                                                        align-middle
                                                    "
                                                >

                                                    <?php if (
                                                        !empty(
                                                            $student['photo']
                                                        )
                                                    ) { ?>

                                                        <img

                                                            src="../uploads/students/<?= htmlspecialchars($student['photo']) ?>"

                                                            width="50"

                                                            height="50"

                                                            style="
                                                                object-fit:cover;
                                                                border-radius:5px;
                                                            "

                                                        >

                                                    <?php } else { ?>

                                                        <span
                                                            class="
                                                                text-muted
                                                            "
                                                        >

                                                            No Photo

                                                        </span>

                                                    <?php } ?>

                                                </td>

                                                <!-- NAME -->

                                                <td
                                                    class="
                                                        align-middle
                                                    "
                                                >

                                                    <strong>

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

                                                    </strong>

                                                </td>

                                                <!-- SCHOLAR -->

                                                <td
                                                    class="
                                                        align-middle
                                                    "
                                                >

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

                                                </td>

                                                <!-- ROLL -->

                                                <td
                                                    class="
                                                        align-middle
                                                    "
                                                >

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

                                                </td>

                                                <!-- ADMISSION -->

                                                <td
                                                    class="
                                                        align-middle
                                                    "
                                                >

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

                                                </td>

                                                <!-- FATHER -->

                                                <td
                                                    class="
                                                        align-middle
                                                    "
                                                >

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

                                                </td>

                                                <!-- CLASS -->

                                                <td
                                                    class="
                                                        align-middle
                                                    "
                                                >

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

                                                </td>

                                                <!-- SECTION -->

                                                <td
                                                    class="
                                                        align-middle
                                                    "
                                                >

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

                                                </td>

                                            </tr>

                                        <?php } ?>

                                    </tbody>

                                </table>

                            </div>

                        </div>

                        <!-- FOOTER -->

                        <div class="card-footer">

                            <button

                                type="submit"

                                class="
                                    btn
                                    btn-success
                                "

                            >

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

                                Generate ID Cards

                            </button>

                        </div>

                    </div>

                </form>

            <?php } ?>

        </div>

    </section>

</div>

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

<!-- =========================================================
CHECKBOX SCRIPT
========================================================= -->

<script>

const checkboxes =
    document.querySelectorAll(
        '.student_checkbox'
    );

function updateSelectedCount() {

    let total =
        document.querySelectorAll(
            '.student_checkbox:checked'
        ).length;

    document.getElementById(
        'selected_count'
    ).innerText = total;

}

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

        checkboxes.forEach(function (checkbox) {

            checkbox.checked = true;

        });

        updateSelectedCount();

    }
);

document.getElementById(
    'unselect_all'
)?.addEventListener(
    'click',
    function () {

        checkboxes.forEach(function (checkbox) {

            checkbox.checked = false;

        });

        updateSelectedCount();

    }
);

checkboxes.forEach(function (checkbox) {

    checkbox.addEventListener(
        'change',
        updateSelectedCount
    );

});

</script>