<?php
require 'config.php';

// Fitur Hapus Aspirasi
if (isset($_GET['hapus'])) {
    $id = $_GET['hapus'];
    mysqli_query($conn, "DELETE FROM aspirasi WHERE id='$id'");
    echo "<script>alert('Aspirasi Dihapus!'); window.location='list-aspirasi.php';</script>";
}

$query = mysqli_query($conn, "SELECT * FROM aspirasi ORDER BY created_at DESC");
?>

<!DOCTYPE html>
<html>
<head>
    <title>Daftar Aspirasi</title>
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css">
</head>
<body class="bg-light p-4">
    <div class="card shadow">
        <div class="card-header bg-dark text-white text-center"><h5>ASPIRASI MASYARAKAT MASUK</h5></div>
        <div class="card-body p-0">
            <table class="table table-hover mb-0">
                <thead class="table-danger">
                    <tr>
                        <th>Tanggal</th>
                        <th>Nama Pengirim</th>
                        <th>Email</th>
                        <th>Pesan Aspirasi</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php while($row = mysqli_fetch_assoc($query)) : ?>
                    <tr>
                        <td><small><?= date('d/m/Y H:i', strtotime($row['created_at'])) ?></small></td>
                        <td class="fw-bold"><?= $row['nama'] ?></td>
                        <td><?= $row['email'] ?></td>
                        <td><p class="small"><?= $row['pesan'] ?></p></td>
                        <td>
                            <a href="?hapus=<?= $row['id'] ?>" class="btn btn-sm btn-outline-danger" onclick="return confirm('Hapus pesan ini?')">Hapus</a>
                        </td>
                    </tr>
                    <?php endwhile; ?>
                </tbody>
            </table>
        </div>
    </div>
    <div class="mt-3"><a href="index.php" class="text-danger"> Kembali ke Dashboard</a></div>
</body>
</html>