<?php
require_once __DIR__ . "/../../app/config/auth.php";
require_login();
require_once __DIR__ . "/../../app/config/app.php";
require_once __DIR__ . "/../../app/config/session.php";
require_once __DIR__ . "/../../app/config/koneksi.php";

$currentPage = "kritik_saran";
function esc($v){ return htmlspecialchars((string)$v, ENT_QUOTES, 'UTF-8'); }

// Validasi Parameter
if (!isset($_GET['id']) || !is_numeric($_GET['id'])) {
    header("Location: " . base_url('/pages/kritik_saran/kritiksaran_admin.php'));
    exit;
}

$id = intval($_GET['id']);

// Ambil Data
$q = mysqli_query($koneksi, "SELECT * FROM kritik_saran WHERE id=$id LIMIT 1");
$data = mysqli_fetch_assoc($q);

if (!$data) {
    header("Location: " . base_url('/pages/kritik_saran/kritiksaran_admin.php'));
    exit;
}

// Ubah status → dibaca jika belum
if ($data['status'] === 'baru') {
    mysqli_query($koneksi, "UPDATE kritik_saran SET status='dibaca' WHERE id=$id");
    $data['status'] = 'dibaca';
}

// Link Balasan WA
$wa = esc($data['no_hp']);
$msg = urlencode("Assalamualaikum, Kami dari Masjid Al-Firdaus.\nMenanggapi pesan Anda:\n\n" . $data['pesan']);
$waLink = "https://wa.me/$wa?text=$msg";

?>
<!doctype html>
<html lang="id">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Baca Pesan — Kritik & Saran</title>

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.5.0/css/all.min.css">

<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/sidebar.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/layout.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/dashboard.css'); ?>">
<link rel="stylesheet" href="<?= base_url('/assets/css/dashboard/widgets.css'); ?>">

<style>
.badge-baru { background:#198754; }
.badge-baca { background:#6c757d; }
</style>
</head>

<body class="dashboard-page">

<?php include __DIR__ . "/../../assets/component/dashboard_sidebar.php"; ?>

<div id="content-wrapper" class="content-wrapper">
<?php include __DIR__ . "/../../assets/component/dashboard_navbar.php"; ?>

<main class="container-fluid mt-4">

    <h1 class="page-title">Detail Kritik & Saran</h1>
    <p class="text-muted small">Pesan dari jamaah</p>

    <div class="card mt-4 shadow-sm">
        <div class="card-header bg-success text-white fw-bold">
            Detail Pesan Jamaah
        </div>

        <div class="card-body">

            <div class="mb-3">
                <strong>Nama : </strong> <?= esc($data['nama']); ?>
            </div>

            <div class="mb-3">
                <strong>No HP : </strong>
                <a href="<?= $waLink ?>" target="_blank" class="text-decoration-none text-primary">
                    <?= esc($data['no_hp']); ?>
                </a>
            </div>

            <div class="mb-3">
                <strong>Status : </strong>
                <?php if($data['status']=='baru'): ?>
                    <span class="badge badge-baru">Baru</span>
                <?php else: ?>
                    <span class="badge badge-baca">Dibaca</span>
                <?php endif; ?>
            </div>

            <div class="mb-3">
                <strong>Tanggal : </strong> <?= esc($data['tanggal']); ?> <?= esc($data['jam']); ?>
            </div>

            <hr>

            <strong>Isi Pesan:</strong>
            <div class="border rounded p-3 mb-4" style="min-height:120px;">
                <?= nl2br(esc($data['pesan'])); ?>
            </div>

            <div class="d-flex gap-2">
                <a href="<?= $waLink ?>" class="btn btn-success" target="_blank">
                    <i class="fa-brands fa-whatsapp"></i> Balas WA
                </a>

                <a href="<?= base_url('/pages/kritik_saran/kritiksaran_admin.php'); ?>" class="btn btn-secondary">
                    Kembali
                </a>

                <a href="<?= base_url('/pages/kritik_saran/hapus_kritiksaran.php?id='.$id); ?>"
                   onclick="return confirm('Hapus pesan ini?');"
                   class="btn btn-danger">
                    <i class="fa fa-trash"></i> Hapus
                </a>
            </div>

        </div>
    </div>

</main>

</div>

<?php include __DIR__ . "/../../assets/component/dashboard_footer.php"; ?>

</body>
</html>
