
<?php
session_start();
include 'config.php';
include 'header.php';

if (!isset($_SESSION['username']) || ($_SESSION['role'] != 'Admin' && $_SESSION['role'] != 'Super Admin')) {
    header("Location: index.php");
    exit();
}

// Fetch grievances from the database
$sql = "SELECT g.id, g.title, g.description, g.status, u.username 
        FROM grievances g
        JOIN users u ON g.user_id = u.id
        ORDER BY g.status DESC";
$result = $conn->query($sql);
?>

<div class="container mt-4">
    <h1 class="text-primary text-center">Grievances Submitted by Employees</h1>
    <table class="table table-bordered table-hover">
        <thead class="thead-dark">
            <tr>
                <th>Grievance Title</th>
                <th>Description</th>
                <th>Status</th>
                <th>Employee Username</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php while ($row = $result->fetch_assoc()) { ?>
                <tr>
                    <td><?php echo htmlspecialchars($row['title'], ENT_QUOTES, 'UTF-8'); ?></td>
                    <td><?php echo htmlspecialchars($row['description'], ENT_QUOTES, 'UTF-8'); ?></td>
                    <td><?php echo htmlspecialchars($row['status'], ENT_QUOTES, 'UTF-8'); ?></td>
                    <td><?php echo htmlspecialchars($row['username'], ENT_QUOTES, 'UTF-8'); ?></td>
                    <td>
                        <?php if ($row['status'] == 'Pending') { ?>
                            <a href="respond_grievance.php?id=<?php echo $row['id']; ?>" class="btn btn-info btn-sm">Respond</a>
                        <?php } else { ?>
                            <span class="badge bg-success">Resolved</span>
                        <?php } ?>
                    </td>
                </tr>
            <?php } ?>
        </tbody>
    </table>
</div>
