<?php

declare(strict_types=1);

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

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

$page_title = "Reports Dashboard";

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

$current_session_id = get_current_session_id($pdo);

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

$stmtStudents = $pdo->prepare("

    SELECT COUNT(*) 

    FROM student_sessions

    WHERE school_id = ?
    AND session_id = ?
    AND status = 'Active'

");

$stmtStudents->execute([

    $school_id,
    $current_session_id

]);

$total_students =
    (int) $stmtStudents->fetchColumn();

/*
|--------------------------------------------------------------------------
| TOTAL CLASSES
|--------------------------------------------------------------------------
*/

$stmtClasses = $pdo->prepare("

    SELECT COUNT(*)

    FROM classes

    WHERE school_id = ?

");

$stmtClasses->execute([

    $school_id

]);

$total_classes =
    (int) $stmtClasses->fetchColumn();

/*
|--------------------------------------------------------------------------
| TOTAL TC
|--------------------------------------------------------------------------
*/

$stmtTC = $pdo->prepare("

    SELECT COUNT(*)

    FROM tc_certificates

    WHERE school_id = ?
    AND session_id = ?

");

$stmtTC->execute([

    $school_id,
    $current_session_id

]);

$total_tc =
    (int) $stmtTC->fetchColumn();

/*
|--------------------------------------------------------------------------
| TOTAL ENQUIRIES
|--------------------------------------------------------------------------
*/

$stmtEnquiry = $pdo->prepare("

    SELECT COUNT(*)

    FROM student_enquiries

    WHERE school_id = ?
    AND session_id = ?

");

$stmtEnquiry->execute([

    $school_id,
    $current_session_id

]);

$total_enquiries =
    (int) $stmtEnquiry->fetchColumn();

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

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

?>

<style>

.report-card {

    border: none;

    border-radius: 18px;

    overflow: hidden;

    background: #ffffff;

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

    transition: 0.25s ease;
}

.report-card:hover {

    transform: translateY(-5px);
}

.report-icon {

    width: 70px;

    height: 70px;

    border-radius: 18px;

    display: flex;

    align-items: center;

    justify-content: center;

    font-size: 28px;

    color: #fff;

    margin-bottom: 18px;
}

.bg-blue {

    background: linear-gradient(135deg,#2563eb,#1d4ed8);
}

.bg-green {

    background: linear-gradient(135deg,#10b981,#059669);
}

.bg-orange {

    background: linear-gradient(135deg,#f59e0b,#d97706);
}

.bg-red {

    background: linear-gradient(135deg,#ef4444,#dc2626);
}

.report-link {

    text-decoration: none !important;

    color: inherit;
}

.report-link:hover {

    color: inherit;
}

.stat-card {

    border-radius: 18px;

    padding: 22px;

    color: #fff;

    position: relative;

    overflow: hidden;
}

.stat-card h2 {

    font-size: 32px;

    font-weight: 700;

    margin-bottom: 5px;
}

.stat-card p {

    margin: 0;

    opacity: 0.9;
}

.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;
}

.modern-card .card-title {

    font-weight: 700;

    margin: 0;
}

</style>

<div class="content-wrapper">

    <!-- PAGE HEADER -->

    <section class="content-header">

        <div class="container-fluid">

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

                <div
                    class="card-body"
                    style="
                        background: linear-gradient(135deg,#1e293b,#0f172a);
                        color:#fff;
                        border-radius:18px;
                        padding:35px;
                    "
                >

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

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

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

                                Reports Dashboard 📊

                            </h1>

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

                                Analyze students, classes,
                                documents, categories,
                                religion and transport reports
                                from one centralized dashboard.

                            </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;
                                "
                            >

                                Current Session Active

                            </span>

                        </div>

                    </div>

                </div>

            </div>

        </div>

    </section>

    <!-- MAIN CONTENT -->

    <section class="content">

        <div class="container-fluid">

            <!-- STATS -->

            <div class="row">

                <div class="col-lg-3 col-md-6 mb-4">

                    <div
                        class="stat-card"
                        style="
                            background:
                            linear-gradient(135deg,#2563eb,#1d4ed8);
                        "
                    >

                        <h2>

                            <?= number_format($total_students); ?>

                        </h2>

                        <p>

                            Active Students

                        </p>

                    </div>

                </div>

                <div class="col-lg-3 col-md-6 mb-4">

                    <div
                        class="stat-card"
                        style="
                            background:
                            linear-gradient(135deg,#10b981,#059669);
                        "
                    >

                        <h2>

                            <?= number_format($total_classes); ?>

                        </h2>

                        <p>

                            Total Classes

                        </p>

                    </div>

                </div>

                <div class="col-lg-3 col-md-6 mb-4">

                    <div
                        class="stat-card"
                        style="
                            background:
                            linear-gradient(135deg,#f59e0b,#d97706);
                        "
                    >

                        <h2>

                            <?= number_format($total_tc); ?>

                        </h2>

                        <p>

                            Total TC Issued

                        </p>

                    </div>

                </div>

                <div class="col-lg-3 col-md-6 mb-4">

                    <div
                        class="stat-card"
                        style="
                            background:
                            linear-gradient(135deg,#ef4444,#dc2626);
                        "
                    >

                        <h2>

                            <?= number_format($total_enquiries); ?>

                        </h2>

                        <p>

                            Student Enquiries

                        </p>

                    </div>

                </div>

            </div>

            <!-- REPORT CARDS -->

            <div class="row">

                <!-- CLASS REPORT -->

                <div class="col-lg-4 col-md-6 mb-4">

                    <a
                        href="class_reports.php"
                        class="report-link"
                    >

                        <div class="card report-card h-100">

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

                                <div class="report-icon bg-blue">

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

                                </div>

                                <h4 class="font-weight-bold">

                                    Class Reports

                                </h4>

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

                                    View class-wise student
                                    reports and summaries.

                                </p>

                            </div>

                        </div>

                    </a>

                </div>

                <!-- CATEGORY -->

                <div class="col-lg-4 col-md-6 mb-4">

                    <a
                        href="category_report.php"
                        class="report-link"
                    >

                        <div class="card report-card h-100">

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

                                <div class="report-icon bg-green">

                                    <i class="fas fa-layer-group"></i>

                                </div>

                                <h4 class="font-weight-bold">

                                    Category Reports

                                </h4>

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

                                    Analyze category and caste
                                    distribution of students.

                                </p>

                            </div>

                        </div>

                    </a>

                </div>

                <!-- RELIGION -->

                <div class="col-lg-4 col-md-6 mb-4">

                    <a
                        href="religion_report.php"
                        class="report-link"
                    >

                        <div class="card report-card h-100">

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

                                <div class="report-icon bg-orange">

                                    <i class="fas fa-praying-hands"></i>

                                </div>

                                <h4 class="font-weight-bold">

                                    Religion Reports

                                </h4>

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

                                    Religion and minority
                                    group based reports.

                                </p>

                            </div>

                        </div>

                    </a>

                </div>

                <!-- DOCUMENTS -->

                <div class="col-lg-4 col-md-6 mb-4">

                    <a
                        href="document_missing_report.php"
                        class="report-link"
                    >

                        <div class="card report-card h-100">

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

                                <div class="report-icon bg-red">

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

                                </div>

                                <h4 class="font-weight-bold">

                                    Missing Documents

                                </h4>

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

                                    Find students with
                                    missing Aadhaar,
                                    APAAR or Samagra IDs.

                                </p>

                            </div>

                        </div>

                    </a>

                </div>

                <!-- TRANSPORT -->

                <div class="col-lg-4 col-md-6 mb-4">

                    <a
                        href="transport_report.php"
                        class="report-link"
                    >

                        <div class="card report-card h-100">

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

                                <div class="report-icon bg-blue">

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

                                </div>

                                <h4 class="font-weight-bold">

                                    Transport Reports

                                </h4>

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

                                    Transport routes,
                                    fees and student allocation
                                    reports.

                                </p>

                            </div>

                        </div>

                    </a>

                </div>

                <!-- EXPORT -->

                <div class="col-lg-4 col-md-6 mb-4">

                    <a
                        href="download_report.php"
                        class="report-link"
                    >

                        <div class="card report-card h-100">

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

                                <div class="report-icon bg-green">

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

                                </div>

                                <h4 class="font-weight-bold">

                                    Export Reports

                                </h4>

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

                                    Download reports in
                                    Excel and printable formats.

                                </p>

                            </div>

                        </div>

                    </a>

                </div>

            </div>

        </div>

    </section>

</div>

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