<?php
require_once 'db.php';

session_start();
if(!isset($_SESSION['user_id'])) {
    header("Location: login.php");
    exit;
}
$instansi_id = $_SESSION['instansi_id'] ?? 1;

// Ambil data instansi
$stmt_inst = $pdo->prepare("SELECT nama_instansi, logo_path FROM instansi WHERE id = ?");
$stmt_inst->execute([$instansi_id]);
$inst_data = $stmt_inst->fetch(PDO::FETCH_ASSOC);
$nama_instansi = $inst_data['nama_instansi'] ?? "Instansi ID $instansi_id";
$logo_path = !empty($inst_data['logo_path']) ? $inst_data['logo_path'] : '';

$msg = '';
$err = '';
if (isset($_GET['msg'])) {
    $msg = "<div class='alert alert-success alert-dismissible fade show' role='alert'>" . htmlspecialchars($_GET['msg']) . "<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
}
if (isset($_GET['err'])) {
    $err = "<div class='alert alert-danger alert-dismissible fade show' role='alert'>" . htmlspecialchars($_GET['err']) . "<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
}

// Handle Manual Form Add/Edit/Delete
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $action = $_POST['action'] ?? '';

    if ($action === 'add_manual') {
        $tracking_id = trim($_POST['tracking_id']);
        if (empty($tracking_id)) {
            $tracking_id = 'LPR-' . date('Ymd') . '-' . rand(100, 999);
        }
        $tanggal_masuk = !empty($_POST['tanggal_masuk']) ? $_POST['tanggal_masuk'] : date('Y-m-d H:i:s');
        $nama_pelapor = trim($_POST['nama_pelapor']) ?: 'Anonim';
        $klasifikasi = $_POST['klasifikasi'] ?? 'Pengaduan';
        $kategori = trim($_POST['kategori']) ?: 'Umum';
        $judul = trim($_POST['judul']);
        $isi_laporan = trim($_POST['isi_laporan']);
        $skpd_id = !empty($_POST['skpd_id']) ? (int)$_POST['skpd_id'] : null;
        $status_laporan = $_POST['status_laporan'] ?? 'Proses';
        $tindak_lanjut = trim($_POST['tindak_lanjut'] ?? '');

        $instansi_terdisposisi = '';
        if ($skpd_id) {
            $stmt_sk = $pdo->prepare("SELECT nama_skpd FROM skpd WHERE id = ?");
            $stmt_sk->execute([$skpd_id]);
            $instansi_terdisposisi = $stmt_sk->fetchColumn() ?: '';
        }

        $stmt_ins = $pdo->prepare("INSERT INTO lapor (instansi_id, skpd_id, tracking_id, tanggal_masuk, nama_pelapor, klasifikasi, kategori, judul, isi_laporan, instansi_terdisposisi, status_laporan, tindak_lanjut) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
        if ($stmt_ins->execute([$instansi_id, $skpd_id, $tracking_id, $tanggal_masuk, $nama_pelapor, $klasifikasi, $kategori, $judul, $isi_laporan, $instansi_terdisposisi, $status_laporan, $tindak_lanjut])) {
            $msg = "<div class='alert alert-success alert-dismissible fade show'>Laporan manual berhasil ditambahkan.<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
        }
    } elseif ($action === 'delete') {
        $id = (int)$_POST['id'];
        if ($id > 0) {
            $stmt_del = $pdo->prepare("DELETE FROM lapor WHERE id = ? AND instansi_id = ?");
            if ($stmt_del->execute([$id, $instansi_id])) {
                $msg = "<div class='alert alert-success alert-dismissible fade show'>Laporan berhasil dihapus.<button type='button' class='btn-close' data-bs-dismiss='alert'></button></div>";
            }
        }
    }
}

// Default filter bulan & tahun dari tanggal laporan terbaru di DB
if (!isset($_GET['bulan']) && !isset($_GET['tahun'])) {
    $stmt_max = $pdo->prepare("SELECT MAX(tanggal_masuk) FROM lapor WHERE instansi_id = ?");
    $stmt_max->execute([$instansi_id]);
    $max_date = $stmt_max->fetchColumn();
    if ($max_date) {
        $filter_bulan = date('m', strtotime($max_date));
        $filter_tahun = date('Y', strtotime($max_date));
    } else {
        $filter_bulan = date('m');
        $filter_tahun = date('Y');
    }
} else {
    $filter_bulan = isset($_GET['bulan']) ? $_GET['bulan'] : date('m');
    $filter_tahun = isset($_GET['tahun']) ? $_GET['tahun'] : date('Y');
}

$filter_skpd = isset($_GET['skpd_id']) ? $_GET['skpd_id'] : '';

// Fetch all SKPDs for dropdowns
$stmt_skpd_all = $pdo->prepare("SELECT id, nama_skpd, singkatan FROM skpd WHERE instansi_id = ? ORDER BY nama_skpd ASC");
$stmt_skpd_all->execute([$instansi_id]);
$skpd_options = $stmt_skpd_all->fetchAll(PDO::FETCH_ASSOC);

// Base Query
$where_sql = "l.instansi_id = :instansi_id AND MONTH(l.tanggal_masuk) = :bulan AND YEAR(l.tanggal_masuk) = :tahun";
$params_sql = [
    ':instansi_id' => $instansi_id,
    ':bulan' => $filter_bulan,
    ':tahun' => $filter_tahun
];

if ($filter_skpd !== '') {
    $where_sql .= " AND l.skpd_id = :skpd_id";
    $params_sql[':skpd_id'] = $filter_skpd;
}

$stmt_lapor = $pdo->prepare("
    SELECT l.*, s.nama_skpd, s.singkatan 
    FROM lapor l 
    LEFT JOIN skpd s ON l.skpd_id = s.id 
    WHERE $where_sql 
    ORDER BY l.tanggal_masuk DESC
");
$stmt_lapor->execute($params_sql);
$lapor_list = $stmt_lapor->fetchAll(PDO::FETCH_ASSOC);

// Hitung Ringkasan Bulanan
$total_isu = count($lapor_list);
$skpd_terdampak = count(array_unique(array_filter(array_column($lapor_list, 'skpd_id'))));

require_once 'layout_header.php';
?>

<div class="main-panel">
    <div class="content-wrapper">
        <div class="row">
            <div class="col-md-12 grid-margin stretch-card">
                <div class="card shadow-sm border-0 rounded-4">
                    <div class="card-body">
                        <div class="d-flex justify-content-between align-items-center mb-4 flex-wrap gap-2">
                            <div>
                                <h4 class="card-title fw-bold text-dark mb-1">
                                    <i class="fa-solid fa-bullhorn text-danger me-2"></i>Isu Publik & Keluhan Masyarakat (SP4N LAPOR!)
                                </h4>
                                <p class="card-description text-muted mb-0">
                                    Import file Excel (.xlsx) keluhan SP4N LAPOR! per bulan untuk memantau isu publik pada SKPD bersangkutan.
                                </p>
                            </div>
                            <div class="d-flex gap-2 flex-wrap">
                                <button class="btn btn-success fw-bold text-white rounded-3 px-3 shadow-sm" data-bs-toggle="modal" data-bs-target="#modalImportExcel">
                                    <i class="fa-solid fa-file-excel me-1"></i> Import File Excel LAPOR
                                </button>
                                <button class="btn btn-primary fw-bold rounded-3 px-3 shadow-sm" data-bs-toggle="modal" data-bs-target="#modalAddManual">
                                    <i class="fa-solid fa-plus me-1"></i> Input Manual
                                </button>
                            </div>
                        </div>

                        <?= $msg ?>
                        <?= $err ?>

                        <!-- Ringkasan Bulanan Cards -->
                        <div class="row mb-4">
                            <div class="col-md-6 col-lg-3 mb-2">
                                <div class="card border-0 bg-danger bg-opacity-10 rounded-3 p-3">
                                    <small class="text-danger fw-bold text-uppercase d-block mb-1">Total Isu Publik (Bulan Ini)</small>
                                    <h3 class="fw-bold text-danger mb-0"><?= number_format($total_isu) ?> <small class="fs-6 text-muted">Laporan</small></h3>
                                </div>
                            </div>
                            <div class="col-md-6 col-lg-3 mb-2">
                                <div class="card border-0 bg-info bg-opacity-10 rounded-3 p-3">
                                    <small class="text-info fw-bold text-uppercase d-block mb-1">SKPD Terdampak</small>
                                    <h3 class="fw-bold text-info mb-0"><?= number_format($skpd_terdampak) ?> <small class="fs-6 text-muted">Dinas / Instansi</small></h3>
                                </div>
                            </div>
                        </div>

                        <!-- Filter Per Bulan, Tahun, dan SKPD -->
                        <div class="card bg-light border-0 rounded-3 p-3 mb-4">
                            <form method="GET" class="row g-3 align-items-end">
                                <div class="col-md-3">
                                    <label class="form-label fw-bold text-muted small mb-1"><i class="fa-solid fa-calendar me-1"></i>Bulan Laporan</label>
                                    <select name="bulan" class="form-select border-0 shadow-sm rounded-3">
                                        <?php 
                                        $months = ['01'=>'Januari', '02'=>'Februari', '03'=>'Maret', '04'=>'April', '05'=>'Mei', '06'=>'Juni', '07'=>'Juli', '08'=>'Agustus', '09'=>'September', '10'=>'Oktober', '11'=>'November', '12'=>'Desember'];
                                        foreach($months as $num => $name) {
                                            echo "<option value=\"$num\" ".(($filter_bulan == $num) ? 'selected' : '').">$name</option>";
                                        }
                                        ?>
                                    </select>
                                </div>
                                <div class="col-md-2">
                                    <label class="form-label fw-bold text-muted small mb-1">Tahun</label>
                                    <select name="tahun" class="form-select border-0 shadow-sm rounded-3">
                                        <?php for($y = date('Y'); $y >= 2023; $y--) {
                                            echo "<option value=\"$y\" ".(($filter_tahun == $y) ? 'selected' : '').">$y</option>";
                                        } ?>
                                    </select>
                                </div>
                                <div class="col-md-4">
                                    <label class="form-label fw-bold text-muted small mb-1">SKPD Terdisposisi</label>
                                    <select name="skpd_id" class="form-select border-0 shadow-sm rounded-3">
                                        <option value="">-- Semua SKPD --</option>
                                        <?php foreach($skpd_options as $sk): ?>
                                            <option value="<?= $sk['id'] ?>" <?= ($filter_skpd == $sk['id']) ? 'selected' : '' ?>>
                                                <?= htmlspecialchars($sk['nama_skpd']) ?> <?= !empty($sk['singkatan']) ? "({$sk['singkatan']})" : '' ?>
                                            </option>
                                        <?php endforeach; ?>
                                    </select>
                                </div>
                                <div class="col-md-3 d-flex gap-2">
                                    <button type="submit" class="btn btn-primary w-100 fw-bold rounded-3 shadow-sm">
                                        <i class="fa-solid fa-filter me-1"></i> Terapkan Filter
                                    </button>
                                    <a href="lapor.php" class="btn btn-light rounded-3 text-muted">Reset</a>
                                </div>
                            </form>
                        </div>

                        <!-- Data Table -->
                        <div class="table-responsive">
                            <table id="tableLapor" class="table table-hover align-middle">
                                <thead class="table-light">
                                    <tr>
                                        <th style="width: 50px;">No</th>
                                        <th>Tracking ID & Waktu</th>
                                        <th>Pelapor</th>
                                        <th>Isu Publik / Judul Aduan</th>
                                        <th>SKPD Terdisposisi</th>
                                        <th>Status</th>
                                        <th style="width: 100px;" class="text-center">Aksi</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <?php if(empty($lapor_list)): ?>
                                        <tr>
                                            <td colspan="7" class="text-center py-5 text-muted">
                                                <i class="fa-solid fa-folder-open display-6 d-block mb-2 text-secondary"></i>
                                                Tidak ada isu publik LAPOR pada periode bulan terpilih. Klik <strong>"Import File Excel LAPOR"</strong> untuk mengunggah berkas Excel per bulan.
                                            </td>
                                        </tr>
                                    <?php else: ?>
                                        <?php $no = 1; foreach($lapor_list as $l): ?>
                                            <tr>
                                                <td><?= $no++ ?></td>
                                                <td>
                                                    <span class="badge bg-primary bg-opacity-10 text-primary font-monospace px-2 py-1 mb-1 d-inline-block">
                                                        #<?= htmlspecialchars($l['tracking_id']) ?>
                                                    </span>
                                                    <small class="d-block text-muted">
                                                        <i class="fa-regular fa-clock me-1"></i><?= date('d M Y H:i', strtotime($l['tanggal_masuk'])) ?>
                                                    </small>
                                                </td>
                                                <td>
                                                    <span class="fw-bold text-dark small d-block"><?= htmlspecialchars($l['nama_pelapor'] ?: 'Anonim') ?></span>
                                                    <span class="badge bg-light text-muted border rounded-pill small" style="font-size:0.7rem;"><?= htmlspecialchars($l['klasifikasi'] ?: 'Pengaduan') ?></span>
                                                </td>
                                                <td>
                                                    <span class="fw-bold text-dark d-block mb-1" style="font-size: 0.95rem;">
                                                        <?= htmlspecialchars($l['judul']) ?>
                                                    </span>
                                                    <small class="text-muted d-block text-truncate" style="max-width: 380px;">
                                                        <?= htmlspecialchars($l['isi_laporan']) ?>
                                                    </small>
                                                </td>
                                                <td>
                                                    <?php if(!empty($l['nama_skpd'])): ?>
                                                        <span class="badge bg-info bg-opacity-10 text-dark border border-info fw-bold rounded-pill px-3">
                                                            <i class="fa-solid fa-building me-1 text-info"></i><?= htmlspecialchars($l['singkatan'] ?: $l['nama_skpd']) ?>
                                                        </span>
                                                    <?php else: ?>
                                                        <span class="text-muted small"><?= htmlspecialchars($l['instansi_terdisposisi'] ?: 'Belum Terpetakan') ?></span>
                                                    <?php endif; ?>
                                                </td>
                                                <td>
                                                    <?php 
                                                        $st = strtolower($l['status_laporan'] ?? '');
                                                        if(str_contains($st, 'selesai')) {
                                                            echo '<span class="badge bg-success bg-opacity-10 text-success fw-bold rounded-pill px-3 py-1">Selesai</span>';
                                                        } elseif(str_contains($st, 'proses') || str_contains($st, 'tindak')) {
                                                            echo '<span class="badge bg-warning bg-opacity-10 text-warning fw-bold rounded-pill px-3 py-1">Ditindaklanjuti</span>';
                                                        } else {
                                                            echo '<span class="badge bg-secondary bg-opacity-10 text-secondary fw-bold rounded-pill px-3 py-1">' . htmlspecialchars($l['status_laporan'] ?: 'Proses') . '</span>';
                                                        }
                                                    ?>
                                                </td>
                                                <td class="text-center">
                                                    <button class="btn btn-sm btn-outline-info rounded-circle p-2 me-1 btn-detail"
                                                            data-tracking="#<?= htmlspecialchars($l['tracking_id']) ?>"
                                                            data-tanggal="<?= date('d F Y H:i', strtotime($l['tanggal_masuk'])) ?>"
                                                            data-pelapor="<?= htmlspecialchars($l['nama_pelapor'] ?: 'Anonim') ?>"
                                                            data-klasifikasi="<?= htmlspecialchars($l['klasifikasi'] ?: 'Pengaduan') ?>"
                                                            data-kategori="<?= htmlspecialchars($l['kategori'] ?: '-') ?>"
                                                            data-judul="<?= htmlspecialchars($l['judul']) ?>"
                                                            data-isi="<?= htmlspecialchars($l['isi_laporan']) ?>"
                                                            data-instansi="<?= htmlspecialchars($l['nama_skpd'] ?: $l['instansi_terdisposisi']) ?>"
                                                            data-status="<?= htmlspecialchars($l['status_laporan'] ?: 'Proses') ?>"
                                                            data-tindaklanjut="<?= htmlspecialchars($l['tindak_lanjut'] ?: 'Belum ada tanggapan tertulis.') ?>"
                                                            title="Detail">
                                                        <i class="fa-solid fa-eye"></i>
                                                    </button>
                                                    <form method="POST" class="d-inline form-delete">
                                                        <input type="hidden" name="action" value="delete">
                                                        <input type="hidden" name="id" value="<?= $l['id'] ?>">
                                                        <button type="button" class="btn btn-sm btn-outline-danger rounded-circle p-2 btn-del-trigger" title="Hapus">
                                                            <i class="fa-solid fa-trash"></i>
                                                        </button>
                                                    </form>
                                                </td>
                                            </tr>
                                        <?php endforeach; ?>
                                    <?php endif; ?>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Modal Import Excel -->
    <div class="modal fade" id="modalImportExcel" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered">
            <div class="modal-content rounded-4 border-0">
                <form method="POST" action="import_lapor.php" enctype="multipart/form-data">
                    <input type="hidden" name="action" value="upload_excel">
                    <div class="modal-header">
                        <h5 class="modal-title fw-bold"><i class="fa-solid fa-file-excel me-2 text-success"></i>Import File Excel SP4N LAPOR!</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="mb-3">
                            <label class="form-label fw-bold">Pilih File Excel (.xlsx)</label>
                            <input type="file" name="file_lapor" class="form-control rounded-3" accept=".xlsx,.xls" required>
                            <small class="text-muted mt-1 d-block">Unggah berkas ekspor Excel bulanan resmi dari aplikasi SP4N LAPOR!.</small>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-light rounded-3" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-success fw-bold text-white rounded-3">Mulai Impor</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Modal Add Manual LAPOR -->
    <div class="modal fade" id="modalAddManual" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content rounded-4 border-0">
                <form method="POST">
                    <input type="hidden" name="action" value="add_manual">
                    <div class="modal-header">
                        <h5 class="modal-title fw-bold"><i class="fa-solid fa-pen-to-square me-2 text-primary"></i>Input Manual Laporan LAPOR</h5>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <div class="modal-body">
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Tracking ID (Opsional)</label>
                                <input type="text" name="tracking_id" class="form-control rounded-3" placeholder="Otomatis jika dikosongkan">
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Nama Pelapor</label>
                                <input type="text" name="nama_pelapor" class="form-control rounded-3" placeholder="Anonim">
                            </div>
                        </div>
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Klasifikasi Laporan</label>
                                <select name="klasifikasi" class="form-select rounded-3">
                                    <option value="Pengaduan">Pengaduan</option>
                                    <option value="Aspirasi">Aspirasi</option>
                                    <option value="Pengaduan Berkadar Pengawasan">Pengaduan Berkadar Pengawasan</option>
                                </select>
                            </div>
                            <div class="col-md-6 mb-3">
                                <label class="form-label fw-bold">Pilih SKPD Terdisposisi</label>
                                <select name="skpd_id" class="form-select rounded-3">
                                    <option value="">-- Pilih SKPD --</option>
                                    <?php foreach($skpd_options as $sk): ?>
                                        <option value="<?= $sk['id'] ?>"><?= htmlspecialchars($sk['nama_skpd']) ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold">Judul Isu / Laporan</label>
                            <input type="text" name="judul" class="form-control rounded-3" placeholder="Contoh: Jalan Rusak di Kelurahan X" required>
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold">Isi Aduan / Keluhan Masyarakat</label>
                            <textarea name="isi_laporan" class="form-control rounded-3" rows="4" required></textarea>
                        </div>
                        <div class="mb-3">
                            <label class="form-label fw-bold">Tindak Lanjut / Respon SKPD (Jika ada)</label>
                            <textarea name="tindak_lanjut" class="form-control rounded-3" rows="2" placeholder="Jawaban resmi dari pihak dinas..."></textarea>
                        </div>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-light rounded-3" data-bs-dismiss="modal">Batal</button>
                        <button type="submit" class="btn btn-primary fw-bold rounded-3">Simpan Laporan</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <!-- Modal Detail LAPOR -->
    <div class="modal fade" id="modalDetailLapor" tabindex="-1" aria-hidden="true">
        <div class="modal-dialog modal-dialog-centered modal-lg">
            <div class="modal-content rounded-4 border-0">
                <div class="modal-header bg-light">
                    <h5 class="modal-title fw-bold" id="detail_tracking">#Tracking ID</h5>
                    <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                </div>
                <div class="modal-body p-4">
                    <div class="d-flex justify-content-between align-items-center mb-3">
                        <span class="badge bg-primary px-3 py-2 rounded-pill" id="detail_klasifikasi">Pengaduan</span>
                        <span class="text-muted small" id="detail_tanggal"><i class="fa-regular fa-clock me-1"></i>-</span>
                    </div>

                    <h5 class="fw-bold text-dark mb-3" id="detail_judul">Judul Laporan</h5>

                    <div class="card bg-light border-0 rounded-3 p-3 mb-4">
                        <p class="text-muted small mb-1 fw-bold">ISI KELUHAN MASYARAKAT:</p>
                        <p class="text-dark mb-0" id="detail_isi" style="white-space: pre-line; line-height: 1.6;"></p>
                    </div>

                    <div class="row g-3 mb-3">
                        <div class="col-md-6">
                            <div class="border rounded-3 p-3">
                                <small class="text-muted d-block fw-bold mb-1">SKPD / INSTANSI TERDISPOSISI:</small>
                                <span class="fw-bold text-primary" id="detail_instansi">-</span>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="border rounded-3 p-3">
                                <small class="text-muted d-block fw-bold mb-1">STATUS TINDAK LANJUT:</small>
                                <span class="badge bg-warning text-dark fw-bold px-3 py-1" id="detail_status">Proses</span>
                            </div>
                        </div>
                    </div>

                    <div class="card border border-info bg-info bg-opacity-10 rounded-3 p-3">
                        <p class="text-info fw-bold small mb-1"><i class="fa-solid fa-reply me-1"></i>RESPON & TINDAK LANJUT SKPD:</p>
                        <p class="text-dark mb-0 small" id="detail_tindaklanjut" style="white-space: pre-line; line-height: 1.5;"></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

<?php require_once 'layout_footer.php'; ?>

<script>
$(document).ready(function() {
    $('#tableLapor').DataTable({
        "language": {
            "search": "Cari Isu / Pelapor:",
            "lengthMenu": "Tampilkan _MENU_ baris",
            "info": "Menampilkan _START_ sampai _END_ dari _TOTAL_ laporan",
            "paginate": {
                "next": "<i class='fa-solid fa-chevron-right'></i>",
                "previous": "<i class='fa-solid fa-chevron-left'></i>"
            }
        }
    });

    // Detail Modal Handler
    $('.btn-detail').on('click', function() {
        $('#detail_tracking').text($(this).data('tracking'));
        $('#detail_tanggal').html('<i class="fa-regular fa-clock me-1"></i>' + $(this).data('tanggal'));
        $('#detail_klasifikasi').text($(this).data('klasifikasi'));
        $('#detail_judul').text($(this).data('judul'));
        $('#detail_isi').text($(this).data('isi'));
        $('#detail_instansi').text($(this).data('instansi'));
        $('#detail_status').text($(this).data('status'));
        $('#detail_tindaklanjut').text($(this).data('tindaklanjut'));
        $('#modalDetailLapor').modal('show');
    });

    // Confirm Delete
    $('.btn-del-trigger').on('click', function() {
        const form = $(this).closest('.form-delete');
        Swal.fire({
            title: 'Hapus Laporan?',
            text: "Data laporan yang dihapus tidak dapat dikembalikan.",
            icon: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#d33',
            cancelButtonColor: '#3085d6',
            confirmButtonText: 'Ya, Hapus!',
            cancelButtonText: 'Batal'
        }).then((result) => {
            if (result.isConfirmed) {
                form.submit();
            }
        });
    });
});
</script>
