<?php

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

/*
|--------------------------------------------------------------------------
| Access Control
|--------------------------------------------------------------------------
*/

if (
    !isset($_SESSION['role']) ||
    !in_array($_SESSION['role'], ['admin', 'zonal_director'])
)
{
    die("Access Denied");
}

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

<div class="container mt-4">

    <div class="card shadow">

        <div class="card-header bg-success text-white">
            Advanced Export Options
        </div>

        <div class="card-body">

            <form id="exportForm" action="export_excel.php" method="GET">

                <div class="row">

                    <!-- Filter Section -->
                    <div class="col-md-4">

                        <h5>Filter Data</h5>

                        <div class="mb-3">

                            <label class="form-label">District</label>

                            <select name="district" class="form-select">

                                <option value="ALL" selected>
                                    All Districts
                                </option>

                                <?php

                                $districts = mysqli_query(
                                    $conn,
                                    "SELECT DISTINCT district
                                    FROM schools
                                    WHERE district IS NOT NULL
                                    ORDER BY district ASC"
                                );

                                while($row = mysqli_fetch_assoc($districts))
                                {
                                ?>

                                <option value="<?= htmlspecialchars($row['district']) ?>">
                                    <?= htmlspecialchars($row['district']) ?>
                                </option>

                                <?php
                                }
                                ?>

                            </select>

                        </div>

                        <div class="mb-3">
                            <label>Zone</label>

                            <select name="zone" class="form-select">
                                <option value="">-- Select Zone --</option>

                                <?php
                                $zones = mysqli_query(
                                    $conn,
                                    "SELECT DISTINCT zone_name
                                    FROM schools
                                    WHERE zone_name IS NOT NULL
                                    ORDER BY zone_name ASC"
                                );

                                while($row = mysqli_fetch_assoc($zones)){
                                ?>
                                <option value="<?= htmlspecialchars($row['zone_name']) ?>">
                                    <?= htmlspecialchars($row['zone_name']) ?>
                                    </option>
                                    <?php } ?>

                                    </select>
                                    </div>

                                    <div class="mb-3">
                                    <label>Division</label>

                                    <select name="division" class="form-select">
                                    <option value="">-- Select Division --</option>

                                    <?php
                                    $divisions = mysqli_query(
                                        $conn,
                                        "SELECT DISTINCT division_name
                                        FROM schools
                                        WHERE division_name IS NOT NULL
                                        ORDER BY division_name ASC"
                                    );

                                    while($row = mysqli_fetch_assoc($divisions)){
                                    ?>
                                <option value="<?= htmlspecialchars($row['division_name']) ?>">
                                    <?= htmlspecialchars($row['division_name']) ?>
                                </option>
                                <?php } ?>

                            </select>
                        </div>

                    </div>

                    <!-- Additional Details -->
                    <div class="col-md-4">

                        <div class="d-flex justify-content-between align-items-center mb-2">
                            <h5>Additional Details</h5>

                            <div>
                                <button type="button"
                                    class="btn btn-sm btn-secondary"
                                    onclick="selectGroup('details-check')">
                                    All
                                </button>

                                <button type="button"
                                    class="btn btn-sm btn-warning"
                                    onclick="unselectGroup('details-check')">
                                    None
                                </button>
                            </div>
                        </div>

                        <?php
                        $extra_fields = [
                            'survey_date',
                            'entered_by',
                            'census_no',
                            'school_name',
                            'school_category',
                            'school_type',
                            'principal_name',
                            'principal_phone',
                            'school_phone',
                            'it_teacher_name',
                            'it_teacher_phone'
                        ];

                        foreach($extra_fields as $field){
                        ?>

                        <div class="form-check">

                            <input class="form-check-input details-check"
                                type="checkbox"
                                name="fields[]"
                                value="<?= $field ?>">

                            <label class="form-check-label">
                                <?= ucwords(str_replace('_',' ', $field)) ?>
                            </label>

                        </div>

                        <?php } ?>

                    </div>

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

                        <div class="d-flex justify-content-between align-items-center mb-2">
                            <h5>Equipment</h5>

                            <div>
                                <button type="button"
                                    class="btn btn-sm btn-secondary"
                                    onclick="selectGroup('equipment-check')">
                                    All
                                </button>

                                <button type="button"
                                    class="btn btn-sm btn-warning"
                                    onclick="unselectGroup('equipment-check')">
                                    None
                                </button>
                            </div>
                        </div>

                        <?php
                        $equipment = mysqli_query(
                            $conn,
                            "SELECT *
                            FROM equipment_master
                            WHERE status='Active'
                            ORDER BY display_order ASC"
                        );

                        while($row = mysqli_fetch_assoc($equipment)){
                        ?>

                        <div class="form-check">

                            <input class="form-check-input equipment-check"
                            type="checkbox"
                            name="equipment[]"
                            value="<?= $row['id'] ?>">

                            <label class="form-check-label">
                                <?= htmlspecialchars($row['equipment_name']) ?>
                            </label>

                        </div>

                        <?php } ?>

                        <hr>

                        <div class="d-grid gap-2">

                            <button
                                type="submit"
                                formaction="export_excel.php"
                                formmethod="GET"
                                class="btn btn-success">

                                <i class="fa-solid fa-file-excel"></i>
                                Export Excel

                            </button>

                            <button type="submit"
                                    formaction="export_pdf.php"
                                    formmethod="GET"
                                    class="btn btn-danger">

                                    <i class="fa-solid fa-file-pdf"></i>
                                    Export PDF
                            </button>

                            <button
                                type="submit"
                                formaction="print_report.php"
                                formmethod="GET"
                                formtarget="_blank"
                                class="btn btn-primary">

                                <i class="fa-solid fa-print"></i>
                                Print Report

                            </button>

                        </div>

                    </div>

                </div>

            </form>

        </div>

    </div>
    
</div>

<script>

function selectGroup(className)
{
    document.querySelectorAll('.' + className)
    .forEach(cb => cb.checked = true);
}

function unselectGroup(className)
{
    document.querySelectorAll('.' + className)
    .forEach(cb => cb.checked = false);
}

</script>

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