<?php

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

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

$role = $_SESSION['role'];
$username = $_SESSION['fullname'];

$id = isset($_GET['id']) ? (int)$_GET['id'] : 0;

if ($id <= 0) {
    die("Invalid survey ID");
}

/*
|--------------------------------------------------------------------------
| Get Survey
|--------------------------------------------------------------------------
*/
$stmt = mysqli_prepare($conn, "SELECT * FROM surveys WHERE id=?");
mysqli_stmt_bind_param($stmt, "i", $id);
mysqli_stmt_execute($stmt);

$survey = mysqli_fetch_assoc(mysqli_stmt_get_result($stmt));

if (!$survey) {
    die("Survey not found");
}

/*
|--------------------------------------------------------------------------
| Permission Check
|--------------------------------------------------------------------------
*/
if ($role != 'admin' && $survey['entered_by'] != $username) {
    die("Access Denied");
}

/*
|--------------------------------------------------------------------------
| Get Equipment Master
|--------------------------------------------------------------------------
*/
$equipment = mysqli_query(
    $conn,
    "SELECT * FROM equipment_master WHERE status='Active' ORDER BY display_order ASC"
);

/*
|--------------------------------------------------------------------------
| Get Existing Equipment Data
|--------------------------------------------------------------------------
*/
$existing = [];
$res = mysqli_query($conn, "SELECT * FROM survey_equipment WHERE survey_id=$id");

while ($row = mysqli_fetch_assoc($res)) {
    $existing[$row['equipment_id']] = $row;
}

?>

<div class="container mt-4">

    <div class="card shadow">

        <div class="card-header bg-warning">
            Edit Survey
        </div>

        <div class="card-body">

            <form method="POST" action="update_survey.php">

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

                <!-- BASIC INFO -->
                <h5>School Details</h5>

                <div class="row">

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

                        <label>Census No</label>

                        <input type="text"
                            name="census_no"
                            class="form-control"
                            value="<?= htmlspecialchars($survey['census_no']) ?>"
                            readonly>

                    </div>

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

                        <label>District</label>

                        <input type="text"
                            name="district"
                            class="form-control"
                            value="<?= htmlspecialchars($survey['district']) ?>"
                            readonly>

                    </div>

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

                        <label>Zone</label>

                        <input type="text"
                            name="zone_name"
                            class="form-control"
                            value="<?= htmlspecialchars($survey['zone_name']) ?>"
                            readonly>

                    </div>

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

                        <label>Division</label>

                        <input type="text"
                            name="division_name"
                            class="form-control"
                            value="<?= htmlspecialchars($survey['division_name']) ?>"
                            readonly>

                    </div>

                </div>

                <div class="row mt-3">

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

                        <label>School Name</label>

                        <input type="text"
                            name="school_name"
                            class="form-control"
                            value="<?= htmlspecialchars($survey['school_name']) ?>"
                            readonly>

                    </div>

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

                        <label>National / Provincial</label>

                        <input type="text"
                            name="school_category"
                            class="form-control"
                            value="<?= htmlspecialchars($survey['school_category']) ?>"
                            readonly>

                    </div>

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

                        <label>School Type</label>

                        <input type="text"
                            name="school_type"
                            class="form-control"
                            value="<?= htmlspecialchars($survey['school_type']) ?>"
                            readonly>

                    </div>

                </div>

                <hr>

                <!-- PRINCIPAL -->
                <h5>Principal</h5>

                <div class="row">

                    <div class="col-md-4">
                        <input type="text" name="principal_name"
                               class="form-control"
                               value="<?= htmlspecialchars($survey['principal_name']) ?>"
                               placeholder="Principal Name">
                    </div>

                    <div class="col-md-4">
                        <input type="text" name="principal_phone"
                               class="form-control"
                               value="<?= htmlspecialchars($survey['principal_phone']) ?>"
                               placeholder="Phone">
                    </div>

                    <div class="col-md-4">
                        <input type="text" name="school_phone"
                               class="form-control"
                               value="<?= htmlspecialchars($survey['school_phone']) ?>"
                               placeholder="School Phone">
                    </div>

                </div>

                <hr>

                <!-- IT TEACHER -->
                <h5>IT Teacher</h5>

                <div class="row">

                    <div class="col-md-6">
                        <input type="text" name="it_teacher_name"
                               class="form-control"
                               value="<?= htmlspecialchars($survey['it_teacher_name']) ?>">
                    </div>

                    <div class="col-md-6">
                        <input type="text" name="it_teacher_phone"
                               class="form-control"
                               value="<?= htmlspecialchars($survey['it_teacher_phone']) ?>">
                    </div>

                </div>

                <hr>

                <!-- EQUIPMENT -->
                <h5>Equipment</h5>

                <table class="table table-bordered">

                    <thead class="table-dark">
                        <tr>
                            <th>Equipment</th>
                            <th>Working</th>
                            <th>Not Working</th>
                        </tr>
                    </thead>

                    <tbody>

                    <?php while ($eq = mysqli_fetch_assoc($equipment)) {

                        $data = $existing[$eq['id']] ?? null;
                        $working = $data['working'] ?? 0;
                        $not_working = $data['not_working'] ?? 0;

                    ?>

                        <tr>

                            <td><?= htmlspecialchars($eq['equipment_name']) ?></td>

                            <td>
                                <input type="number"
                                       name="working[<?= $eq['id'] ?>]"
                                       class="form-control"
                                       value="<?= $working ?>">
                            </td>

                            <td>
                                <input type="number"
                                       name="not_working[<?= $eq['id'] ?>]"
                                       class="form-control"
                                       value="<?= $not_working ?>">
                            </td>

                        </tr>

                    <?php } ?>

                    </tbody>

                </table>

                <button class="btn btn-success">
                    Update Survey
                </button>

            </form>

        </div>

    </div>

</div>

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