<?php

require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../config/db.php';

if (($_SESSION['role'] ?? '') !== 'admin' && ($_SESSION['role'] ?? '') !== 'zonal_director')
{
    die('Access Denied');
}

include '../includes/header.php';
include '../includes/navbar.php';

/*
|--------------------------------------------------------------------------
| Search
|--------------------------------------------------------------------------
*/

$keyword = trim($_GET['keyword'] ?? '');

$sql = "
SELECT *
FROM schools
WHERE (census_no LIKE ?
OR school_name LIKE ?
OR district LIKE ?
OR zone_name LIKE ?
OR division_name LIKE ?)
";

if (($_SESSION['role'] ?? '') === 'zonal_director') {
    $sql .= ' AND zone_name=?';
}

$sql .= ' ORDER BY district, school_name';

$stmt = mysqli_prepare($conn, $sql);

if (!$stmt) {
    die("SQL Error: " . mysqli_error($conn));
}

$search = "%{$keyword}%";

if (($_SESSION['role'] ?? '') === 'zonal_director') {
    $zone = $_SESSION['zone_name'] ?? '';
    mysqli_stmt_bind_param($stmt, "ssssss", $search, $search, $search, $search, $search, $zone);
} else {
    mysqli_stmt_bind_param($stmt, "sssss", $search, $search, $search, $search, $search);
}

mysqli_stmt_execute($stmt);

$result = mysqli_stmt_get_result($stmt);

$totalRows = mysqli_num_rows($result);

?>

<div class="container-fluid mt-4">

    <!-- Search Box -->
    <div class="row mb-3">

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

            <form method="GET">

                <div class="input-group">

                    <input
                        type="text"
                        name="keyword"
                        class="form-control"
                        placeholder="Search Census No, School, District, Zone or Division"
                        value="<?= htmlspecialchars($keyword) ?>">

                    <button
                        type="submit"
                        class="btn btn-primary">
                        Search
                    </button>

                    <a
                        href="manage_schools.php"
                        class="btn btn-secondary">
                        Reset
                    </a>

                </div>

            </form>

        </div>

    </div>

    <!-- School Data Card -->
    <div class="card shadow">

        <div class="card-header bg-primary text-white">

            <h5 class="mb-0">
                School Master Data
            </h5>

        </div>

        <div class="card-body">

            <div class="alert alert-info">

                Total Records Found:
                <strong><?= $totalRows ?></strong>

            </div>

            <div class="table-responsive">

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

                    <thead class="table-dark">

                        <tr>

                            <th>#</th>
                            <th>Census No</th>
                            <th>District</th>
                            <th>Zone</th>
                            <th>Division</th>
                            <th>School</th>
                            <th>Category</th>
                            <th>Type</th>

                        </tr>

                    </thead>

                    <tbody>

                    <?php

                    $no = 1;

                    if ($totalRows > 0)
                    {
                        while ($row = mysqli_fetch_assoc($result))
                        {
                    ?>

                        <tr>

                            <td><?= $no++ ?></td>

                            <td><?= htmlspecialchars($row['census_no']) ?></td>

                            <td><?= htmlspecialchars($row['district']) ?></td>

                            <td><?= htmlspecialchars($row['zone_name']) ?></td>

                            <td><?= htmlspecialchars($row['division_name']) ?></td>

                            <td><?= htmlspecialchars($row['school_name']) ?></td>

                            <td><?= htmlspecialchars($row['school_category']) ?></td>

                            <td><?= htmlspecialchars($row['school_type']) ?></td>

                        </tr>

                    <?php
                        }
                    }
                    else
                    {
                    ?>

                        <tr>

                            <td colspan="8" class="text-center text-danger">

                                No school records found.

                            </td>

                        </tr>

                    <?php
                    }
                    ?>

                    </tbody>

                </table>

            </div>

        </div>

    </div>

</div>

<?php include '../includes/footer.php'; ?>
