<?php 
session_start();

// Proteksi halaman: Wajib login
if (!isset($_SESSION['pelapor_id'])) {
    header("Location: login_pelapor.php");
    exit;
}

// Memanggil koneksi database
require_once '../includes/config.php';

$pelapor_id = $_SESSION['pelapor_id'];
$nama_user  = $_SESSION['pelapor_nama'];

try {
    // Query mengambil data sesuai struktur tabel laporan Anda
    $query = "SELECT * FROM laporan WHERE pelapor_id = ? ORDER BY created_at DESC";
    $stmt = $pdo->prepare($query);
    $stmt->execute([$pelapor_id]);
    $daftar_laporan = $stmt->fetchAll();
} catch (PDOException $e) {
    die("Gagal mengambil data: " . $e->getMessage());
}
?>

<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Riwayat Laporan - Tangkis BPS</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.0/font/bootstrap-icons.css">
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap" rel="stylesheet">
    <style>
        body { background-color: #f4f7f9; font-family: 'Poppins', sans-serif; }
        .navbar { background: white !important; box-shadow: 0 2px 15px rgba(0,0,0,0.05); }
        .card-custom { border: none; border-radius: 15px; box-shadow: 0 4px 12px rgba(0,0,0,0.05); }
        .badge-status { padding: 8px 12px; border-radius: 8px; font-weight: 600; }
        .btn-logout { border-radius: 50px; font-weight: bold; }
        .evidence-preview { max-width: 100%; border-radius: 10px; border: 1px solid #dee2e6; }
    </style>
</head>
<body>

<nav class="navbar navbar-expand-lg sticky-top mb-4">
    <div class="container">
        <a class="navbar-brand fw-bold text-primary" href="index.php">Tangkis BPS LAMTIM</a>
        <div class="collapse navbar-collapse" id="navbarNav">
            <ul class="navbar-nav ms-auto align-items-center">
                <li class="nav-item"><a class="nav-link px-3" href="index.php">Beranda</a></li>
                <li class="nav-item"><a class="nav-link px-3 active fw-bold text-primary" href="riwayat_laporan.php">Riwayat Laporan</a></li>
                <li class="nav-item ms-lg-3">
                    <span class="me-2 fw-bold text-primary"><i class="bi bi-person-circle"></i> <?= htmlspecialchars($nama_user) ?></span>
                    <a href="logout_pelapor.php" class="btn btn-outline-danger btn-sm btn-logout">Keluar</a>
                </li>
            </ul>
        </div>
    </div>
</nav>

<div class="container pb-5">
    <div class="row">
        <div class="col-md-12">
            <div class="d-flex justify-content-between align-items-center mb-4">
                <h3 class="fw-bold text-dark">Riwayat Aduan Anda</h3>
                <a href="lapor.php" class="btn btn-primary rounded-pill px-4 shadow-sm">
                    <i class="bi bi-plus-circle me-2"></i>Buat Laporan Baru
                </a>
            </div>

            <?php if (empty($daftar_laporan)): ?>
                <div class="card card-custom p-5 text-center bg-white">
                    <i class="bi bi-folder-x text-muted" style="font-size: 4rem;"></i>
                    <p class="mt-3 text-muted">Anda belum pernah mengirimkan laporan.</p>
                </div>
            <?php else: ?>
                <div class="card card-custom overflow-hidden bg-white">
                    <div class="table-responsive">
                        <table class="table table-hover align-middle mb-0">
                            <thead class="table-light">
                                <tr>
                                    <th class="ps-4">Kode Tracking</th>
                                    <th>Jenis Dugaan</th>
                                    <th>Terlapor</th>
                                    <th>Status</th>
                                    <th>Update Terakhir</th>
                                    <th class="text-center pe-4">Aksi</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php foreach ($daftar_laporan as $row): ?>
                                    <tr>
                                        <td class="ps-4 fw-bold text-primary">#<?= $row['kode_tracking'] ?></td>
                                        <td><?= htmlspecialchars($row['jenis_dugaan']) ?></td>
                                        <td><?= htmlspecialchars($row['terlapor']) ?></td>
                                        <td>
                                            <?php 
                                            $color = 'secondary';
                                            if ($row['status'] == 'Baru') $color = 'danger';
                                            if ($row['status'] == 'Diproses') $color = 'warning text-dark';
                                            if ($row['status'] == 'Selesai') $color = 'success';
                                            if ($row['status'] == 'Ditolak') $color = 'dark';
                                            ?>
                                            <span class="badge badge-status bg-<?= $color ?>"><?= $row['status'] ?></span>
                                        </td>
                                        <td><small class="text-muted"><?= date('d M Y', strtotime($row['updated_at'])) ?></small></td>
                                        <td class="text-center pe-4">
                                            <button class="btn btn-sm btn-outline-primary rounded-pill" data-bs-toggle="modal" data-bs-target="#modalDetail<?= $row['id'] ?>">
                                                <i class="bi bi-eye"></i> Detail
                                            </button>
                                        </td>
                                    </tr>

                                    <div class="modal fade" id="modalDetail<?= $row['id'] ?>" tabindex="-1" aria-hidden="true">
                                        <div class="modal-dialog modal-lg modal-dialog-centered">
                                            <div class="modal-content border-0 shadow-lg" style="border-radius: 20px;">
                                                <div class="modal-header border-0 pb-0">
                                                    <h5 class="modal-title fw-bold text-primary">Detail Laporan #<?= $row['kode_tracking'] ?></h5>
                                                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                                                </div>
                                                <div class="modal-body p-4">
                                                    <div class="row g-4">
                                                        <div class="col-md-6">
                                                            <label class="text-muted small d-block">Jenis Dugaan</label>
                                                            <span class="fw-bold"><?= htmlspecialchars($row['jenis_dugaan']) ?></span>
                                                        </div>
                                                        <div class="col-md-6">
                                                            <label class="text-muted small d-block">Tanggal Kejadian</label>
                                                            <span class="fw-bold"><?= date('d F Y', strtotime($row['tanggal_kejadian'])) ?></span>
                                                        </div>
                                                        <div class="col-12">
                                                            <label class="text-muted small d-block">Lokasi</label>
                                                            <span class="fw-bold"><?= htmlspecialchars($row['lokasi']) ?></span>
                                                        </div>
                                                        <div class="col-12">
                                                            <label class="text-muted small d-block mb-1">Deskripsi Aduan</label>
                                                            <div class="p-3 bg-light rounded shadow-sm border-0"><?= nl2br(htmlspecialchars($row['deskripsi'])) ?></div>
                                                        </div>

                                                        <div class="col-12">
                                                            <label class="text-muted small d-block mb-2">Bukti Pendukung</label>
                                                            <?php if ($row['bukti']): 
                                                                $file_path = "../assets/uploads/" . $row['bukti'];
                                                                $ext = strtolower(pathinfo($row['bukti'], PATHINFO_EXTENSION));
                                                            ?>
                                                                <?php if (in_array($ext, ['jpg', 'jpeg', 'png'])): ?>
                                                                    <img src="<?= $file_path ?>" class="evidence-preview shadow-sm" alt="Bukti Foto">
                                                                <?php elseif (in_array($ext, ['mp4', 'mov'])): ?>
                                                                    <video controls class="evidence-preview shadow-sm">
                                                                        <source src="<?= $file_path ?>" type="video/<?= $ext ?>">
                                                                        Browser Anda tidak mendukung video.
                                                                    </video>
                                                                <?php else: ?>
                                                                    <div class="alert alert-info d-flex align-items-center">
                                                                        <i class="bi bi-file-earmark-text fs-4 me-3"></i>
                                                                        <div>
                                                                            <span class="d-block fw-bold">Dokumen: <?= $row['bukti'] ?></span>
                                                                            <a href="<?= $file_path ?>" class="btn btn-sm btn-primary mt-2" download>Unduh File</a>
                                                                        </div>
                                                                    </div>
                                                                <?php endif; ?>
                                                            <?php else: ?>
                                                                <span class="text-muted italic small">Tidak ada bukti yang diunggah.</span>
                                                            <?php endif; ?>
                                                        </div>

                                                        <div class="col-12 mt-2">
                                                            <label class="text-primary fw-bold small d-block mb-1">Catatan dari Admin</label>
                                                            <div class="p-3 bg-primary bg-opacity-10 border border-primary border-opacity-25 rounded">
                                                                <?= $row['catatan_admin'] ? nl2br(htmlspecialchars($row['catatan_admin'])) : "<em>Belum ada catatan dari admin.</em>" ?>
                                                            </div>
                                                        </div>
                                                    </div>
                                                </div>
                                                <div class="modal-footer border-0">
                                                    <button type="button" class="btn btn-light rounded-pill px-4" data-bs-dismiss="modal">Tutup</button>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    </div>
                </div>
            <?php endif; ?>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>