<?php

declare(strict_types=1);

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

allow_roles(['admin', 'accountant', 'teacher']);

$page_title = "Class Reports";

$school_id = (int) $_SESSION['school_id'];

$current_session_id = get_current_session_id($pdo);

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

$students = [];

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

$classStmt = $pdo->prepare("

    SELECT

        id,
        class_name,
        section

    FROM classes

    WHERE school_id = ?

    ORDER BY sort_order ASC,
    class_name ASC

");

$classStmt->execute([

    $school_id

]);

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

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

if ($class_id > 0) {

    $studentStmt = $pdo->prepare("

        SELECT

            ss.id AS student_session_id,
            ss.roll_no,
            ss.status,

            s.id AS student_id,
            s.name,
            s.gender,
            s.phone,

            sp.father_name,
            sp.mother_name,

            sd.aadhaar_no,
            sd.apaar_id,
            sd.samagra_id,

            sci.category,
            sci.caste,
            sci.religion,

            st.transport_route,
            st.transport_fees,

            c.class_name,
            c.section

        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

        LEFT JOIN student_documents sd
            ON sd.student_id = s.id

        LEFT JOIN student_category_info sci
            ON sci.student_id = s.id

        LEFT JOIN student_transport st
            ON st.student_id = s.id

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

        WHERE ss.school_id = ?
        AND ss.session_id = ?
        AND ss.class_id = ?

        ORDER BY
        ss.roll_no ASC,
        s.name ASC

    ");

    $studentStmt->execute([

        $school_id,
        $current_session_id,
        $class_id

    ]);

    $students =
        $studentStmt->fetchAll(PDO::FETCH_ASSOC);
}

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

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

?>

<style>

.modern-card {

    border: none;

    border-radius: 18px;

    overflow: hidden;

    box-shadow: 0 4px 18px rgba(0,0,0,0.06);
}

.modern-card .card-header {

    background: #fff;

    border-bottom: 1px solid #eef2f7;

    padding: 18px 22px;
}

.table-modern thead {

    background: #111827;

    color: #fff;
}

.table-modern th {

    border: none !important;

    font-size: 14px;

    font-weight: 600;
}

.table-modern td {

    vertical-align: middle !important;
}

.filter-card {

    background: linear-gradient(135deg,#1e293b,#0f172a);

    border-radius: 18px;

    padding: 25px;

    color: #fff;

    margin-bottom: 25px;
}

.empty-state {

    text-align: center;

    padding: 70px 20px;
}

.empty-state i {

    font-size: 70px;

    color: #cbd5e1;

    margin-bottom: 20px;
}

.export-btn {

    border-radius: 10px;

    font-weight: 600;
}

</style>

<div class="content-wrapper">

    <!-- HEADER -->

    <section class="content-header">

        <div class="container-fluid">

            <div class="filter-card">

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

                    <div class="col-lg-8">

                        <h1 class="font-weight-bold mb-2">

                            Class Reports 📚

                        </h1>

                        <p
                            class="mb-0"
                            style="
                                color: rgba(255,255,255,0.75);
                            "
                        >

                            View detailed student information
                            class-wise with export support.

                        </p>

                    </div>

                    <div
                        class="col-lg-4 text-lg-right mt-4 mt-lg-0"
                    >

                        <span
                            class="badge badge-light p-3"
                            style="
                                font-size:14px;
                                border-radius:12px;
                            "
                        >

                            Session Active

                        </span>

                    </div>

                </div>

            </div>

        </div>

    </section>

    <!-- CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- FILTER -->

            <div class="card modern-card mb-4">

                <div class="card-header">

                    <h3 class="card-title">

                        Filter Class Report

                    </h3>

                </div>

                <div class="card-body">

                    <form method="GET">

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

                            <div class="col-md-4">

                                <label>

                                    Select Class

                                </label>

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

                                    <option value="">

                                        Choose Class

                                    </option>

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

                                        <option
                                            value="<?= (int) $class['id']; ?>"

                                            <?php
                                            if (
                                                $class_id ===
                                                (int) $class['id']
                                            ) {
                                                echo 'selected';
                                            }
                                            ?>
                                        >

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

                                            <?php if (!empty($class['section'])): ?>

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

                                            <?php endif; ?>

                                        </option>

                                    <?php endforeach; ?>

                                </select>

                            </div>

                            <div class="col-md-3">

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

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

                                    Generate Report

                                </button>

                            </div>

                            <?php if ($class_id > 0): ?>

                                <div class="col-md-2">

                                    <a
                                        href="download_report.php?type=class&class_id=<?= $class_id; ?>"
                                        class="btn btn-success btn-block export-btn"
                                    >

                                        <i class="fas fa-file-excel"></i>

                                        Excel

                                    </a>

                                </div>

                                <div class="col-md-2">

                                    <button
                                        type="button"
                                        onclick="window.print()"
                                        class="btn btn-dark btn-block export-btn"
                                    >

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

                                        Print

                                    </button>

                                </div>

                            <?php endif; ?>

                        </div>

                    </form>

                </div>

            </div>

            <!-- REPORT -->

            <div class="card modern-card">

                <div class="card-header">

                    <h3 class="card-title">

                        Student Records

                    </h3>

                </div>

                <div class="card-body p-0">

                    <?php if (count($students) > 0): ?>

                        <div class="table-responsive">

                            <table
                                class="table table-bordered table-hover table-modern mb-0"
                            >

                                <thead>

                                    <tr>

                                        <th>#</th>

                                        <th>Roll No</th>

                                        <th>Student</th>

                                        <th>Gender</th>

                                        <th>Father</th>

                                        <th>Phone</th>

                                        <th>Category</th>

                                        <th>Religion</th>

                                        <th>Aadhaar</th>

                                        <th>APAAR</th>

                                        <th>Samagra</th>

                                        <th>Transport</th>

                                        <th>Status</th>

                                    </tr>

                                </thead>

                                <tbody>

                                    <?php foreach ($students as $index => $row): ?>

                                        <tr>

                                            <td>

                                                <?= $index + 1; ?>

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

                                                <strong>

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

                                                </strong>

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

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

                                            </td>

                                            <td>

                                                <?php
                                                $badge =
                                                    $row['status'] === 'Active'
                                                    ? 'success'
                                                    : 'secondary';
                                                ?>

                                                <span
                                                    class="badge badge-<?= $badge; ?> p-2"
                                                >

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

                                                </span>

                                            </td>

                                        </tr>

                                    <?php endforeach; ?>

                                </tbody>

                            </table>

                        </div>

                    <?php else: ?>

                        <div class="empty-state">

                            <i class="fas fa-folder-open"></i>

                            <h4>

                                No Records Found

                            </h4>

                            <p class="text-muted">

                                Select a class to generate report.

                            </p>

                        </div>

                    <?php endif; ?>

                </div>

            </div>

        </div>

    </section>

</div>

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