<?php 
session_start();

if (!isset($_SESSION['is_logged_in']) || $_SESSION['is_logged_in'] !== true) {
    header("Location: index.php");
    exit();
}

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

if (!isset($_GET['id'])) {
    header("Location: dashboard.php");
    exit();
}

$id = $_GET['id'];

try {
    $stmt = $pdo->prepare("SELECT * FROM laporan WHERE id = ?");
    $stmt->execute([$id]);
    $row = $stmt->fetch();

    if (!$row) {
        die("<div class='container my-5 alert alert-danger'>Data laporan tidak ditemukan.</div>");
    }
} catch (PDOException $e) {
    die("Error Database: " . $e->getMessage());
}
?>

<style>
    body { background-color: #f4f7f6; }
    .card { border: none; border-radius: 15px; box-shadow: 0 5px 15px rgba(0,0,0,0.05); }
    .card-header { background-color: #fff !important; border-bottom: 1px solid #edf2f9; padding: 1.25rem; }
    .info-label { color: #6c757d; font-size: 0.8rem; font-weight: 600; text-transform: uppercase; margin-bottom: 5px; }
    .info-value { color: #2d3748; font-weight: 500; margin-bottom: 20px; }
    .preview-img { width: 100%; max-height: 400px; object-fit: contain; border-radius: 12px; background: #eee; cursor: pointer; }
    
    /* CSS Khusus Cetak */
    @media print {
        .d-print-none, .btn, .breadcrumb, footer, nav { display: none !important; }
        .card { border: 1px solid #dee2e6 !important; box-shadow: none !important; }
        body { background-color: #fff; }
        .container { width: 100%; max-width: 100%; margin: 0; padding: 0; }
    }
</style>

<div class="container my-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="fw-bold mb-0">Detail Laporan</h2>
            <nav aria-label="breadcrumb" class="d-print-none">
                <ol class="breadcrumb mt-2">
                    <li class="breadcrumb-item"><a href="dashboard.php" class="text-decoration-none">Dashboard</a></li>
                    <li class="breadcrumb-item active">#<?= $row['kode_tracking'] ?></li>
                </ol>
            </nav>
        </div>
        <div class="d-flex gap-2 d-print-none">
            <button onclick="window.print()" class="btn btn-outline-secondary rounded-pill px-4 shadow-sm">
                <i class="bi bi-printer me-2"></i>Cetak
            </button>
            <a href="dashboard.php" class="btn btn-light border rounded-pill px-4 shadow-sm">
                <i class="bi bi-arrow-left me-2"></i>Kembali
            </a>
        </div>
    </div>

    <div class="row g-4">
        <div class="col-lg-8">
            <div class="card mb-4">
                <div class="card-header d-flex justify-content-between align-items-center">
                    <h5 class="mb-0 fw-bold text-primary">Informasi Kejadian</h5>
                    <span class="badge bg-light text-dark border px-3 py-2 rounded-pill">ID: <?= $row['id'] ?></span>
                </div>
                <div class="card-body p-4">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="info-label">Kode Tracking</div>
                            <div class="info-value text-primary fw-bold fs-5">#<?= $row['kode_tracking'] ?></div>
                            
                            <div class="info-label">tanggal_kejadian</div>
                            <div class="info-value"><?= date('d F Y, H:i', strtotime($row['created_at'])) ?> WIB</div>
                        </div>
                        <div class="col-md-6 text-md-end">
                            <div class="info-label">Status Saat Ini</div>
                            <div>
                                <?php 
                                    $status_map = [
                                        'Baru' => 'bg-danger',
                                        'Diproses' => 'bg-warning text-dark',
                                        'Selesai' => 'bg-success',
                                        'Ditolak' => 'bg-dark'
                                    ];
                                    $badge = $status_map[$row['status']] ?? 'bg-secondary';
                                ?>
                                <span class="badge <?= $badge ?> px-4 py-2 rounded-pill fs-6"><?= $row['status'] ?></span>
                            </div>
                        </div>
                    </div>

                    <hr class="my-4 opacity-50">

                    <div class="row">
                        <div class="col-md-6">
                            <div class="info-label">Jenis Dugaan Pelanggaran</div>
                            <div class="info-value text-danger fw-bold"><?= htmlspecialchars($row['jenis_dugaan']) ?></div>
                        </div>
                        <div class="col-md-6">
                            <div class="info-label">waktu kejadian</div>
                            <div class="info-value"><?= htmlspecialchars($row['tanggal_kejadian']) ?></div>
                        </div>
                        <div class="col-12">
                            <div class="info-label">Lokasi Kejadian</div>
                            <div class="info-value"><?= htmlspecialchars($row['lokasi']) ?></div>
                        </div>
                    </div>

                    <div class="info-label">Deskripsi / Kronologi Lengkap</div>
                    <div class="p-3 rounded bg-light border" style="white-space: pre-wrap; line-height: 1.6;"><?= htmlspecialchars($row['deskripsi']) ?></div>
                </div>
            </div>

            <div class="card">
                <div class="card-header"><h5 class="mb-0 fw-bold text-primary">Pihak Terlapor</h5></div>
                <div class="card-body p-4">
                    <div class="d-flex align-items-center">
                        <div class="bg-primary text-white rounded-circle d-flex align-items-center justify-content-center" style="width: 50px; height: 50px;">
                            <i class="bi bi-person fs-3"></i>
                        </div>
                        <div class="ms-3">
                            <h5 class="mb-0 fw-bold"><?= htmlspecialchars($row['terlapor']) ?></h5>
                            <p class="text-muted mb-0 small">Pihak yang dilaporkan</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <div class="col-lg-4">
            <div class="card mb-4 border-start border-4 <?= ($row['status'] == 'Ditolak') ? 'border-danger' : 'border-primary' ?>">
                <div class="card-body">
                    <h6 class="fw-bold mb-3"><i class="bi bi-chat-left-dots me-2"></i>Catatan Petugas</h6>
                    <p class="small text-dark fst-italic mb-3 border-start ps-2">
                        "<?= !empty($row['catatan_admin']) ? htmlspecialchars($row['catatan_admin']) : 'Belum ada catatan progres.' ?>"
                    </p>
                    <div class="d-grid d-print-none">
                        <a href="update_status.php?id=<?= $row['id'] ?>" class="btn btn-primary rounded-pill btn-sm">
                            <i class="bi bi-pencil-square me-2"></i>Update Progres / Status
                        </a>
                    </div>
                </div>
            </div>

            <div class="card shadow-sm">
                <div class="card-header text-center"><h6 class="mb-0 fw-bold">Bukti Lampiran</h6></div>
                <div class="card-body p-4 text-center">
                    <?php 
                    $file_path = "../assets/uploads/" . $row['bukti'];
                    if (!empty($row['bukti']) && file_exists($file_path)): 
                        $ext = strtolower(pathinfo($row['bukti'], PATHINFO_EXTENSION));
                        $is_image = in_array($ext, ['jpg', 'jpeg', 'png', 'gif', 'webp']);
                    ?>
                        <?php if ($is_image): ?>
                            <a href="<?= $file_path ?>" target="_blank">
                                <img src="<?= $file_path ?>" class="preview-img mb-3 shadow-sm" alt="Bukti">
                            </a>
                        <?php else: ?>
                            <div class="py-4 bg-light rounded-3 mb-3 border border-dashed text-center">
                                <i class="bi bi-file-earmark-text text-primary" style="font-size: 3rem;"></i>
                                <p class="small text-muted mt-2 mb-0 text-truncate px-2"><?= htmlspecialchars($row['bukti']) ?></p>
                            </div>
                        <?php endif; ?>

                        <div class="d-grid d-print-none">
                            <a href="<?= $file_path ?>" download class="btn btn-outline-primary btn-sm rounded-pill">
                                <i class="bi bi-download me-2"></i>Unduh Lampiran
                            </a>
                        </div>
                    <?php else: ?>
                        <div class="text-muted py-4 fst-italic">
                            <i class="bi bi-file-earmark-x fs-2"></i>
                            <p class="small mt-2 mb-0">Tidak ada bukti fisik ditemukan.</p>
                        </div>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
</div>

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