<?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'];

$sql = "SELECT * FROM surveys ORDER BY id ASC";
$result = mysqli_query($conn, $sql);

?>

<div class="container mt-4">

    <div class="card shadow">

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

        <div class="card-body">

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

                <thead>
                    <tr>
                        <th>ID</th>
                        <th>School</th>
                        <th>District</th>
                        <th>Date</th>
                        <th>Entered By</th>
                        <th>Actions</th>
                    </tr>
                </thead>

                <tbody>

                <?php while ($row = mysqli_fetch_assoc($result)) { ?>

                    <tr>

                        <td><?= $row['id'] ?></td>
                        <td><?= htmlspecialchars($row['school_name']) ?></td>
                        <td><?= htmlspecialchars($row['district']) ?></td>
                        <td><?= $row['survey_date'] ?></td>
                        <td><?= htmlspecialchars($row['entered_by']) ?></td>

                        <td>

                            <?php if ($role == 'admin' || $row['entered_by'] == $username) { ?>

                                <!-- EDIT -->
                                <a href="edit_survey.php?id=<?= $row['id'] ?>"
                                   class="btn btn-warning btn-sm">
                                    Edit
                                </a>

                                <!-- DELETE -->
                                <a href="delete_survey.php?id=<?= $row['id'] ?>"
                                   class="btn btn-danger btn-sm"
                                   onclick="return confirm('Are you sure?')">
                                    Delete
                                </a>

                            <?php } ?>

                        </td>

                    </tr>

                <?php } ?>

                </tbody>

            </table>

        </div>

    </div>

</div>

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