<?php
session_start();

// Periksa apakah pengguna sudah login sebagai jejaring
if (!isset($_SESSION['jejaring_logged_in']) || !$_SESSION['jejaring_logged_in']) {
    header('Location: ../../auth/jejaring_login.php');
    exit();
}

$loggedInNamaInstansi = htmlspecialchars($_SESSION['jejaring_nama_instansi']);
$pageTitle = "Daftar Laporan Terduga TB Bulan Ini";

// Sertakan file koneksi database
include_once '../../api/db_connect.php';

// Inisialisasi variabel untuk menyimpan data laporan
$laporan_tb = [];
$error_message = '';

// Pengaturan Paginasi
$records_per_page = 10;
$current_page = isset($_GET['page']) && is_numeric($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($current_page - 1) * $records_per_page;
$total_records = 0;
$total_pages = 1;

try {
    if (!isset($pdo) || !$pdo instanceof PDO) {
        throw new PDOException("Objek PDO tidak tersedia setelah menyertakan db_connect.php.");
    }

    // Query untuk menghitung total records
    $count_query = "SELECT COUNT(*) FROM terduga_tb
                    WHERE sumber_laporan = :nama_instansi
                    AND YEAR(tanggal) = YEAR(CURDATE())
                    AND MONTH(tanggal) = MONTH(CURDATE())";
    $count_stmt = $pdo->prepare($count_query);
    $count_stmt->bindParam(':nama_instansi', $_SESSION['jejaring_nama_instansi'], PDO::PARAM_STR);
    $count_stmt->execute();
    $total_records = $count_stmt->fetchColumn();
    $total_pages = ceil($total_records / $records_per_page);

    if ($current_page < 1) $current_page = 1;
    if ($current_page > $total_pages && $total_pages > 0) {
        $current_page = $total_pages;
        $offset = ($current_page - 1) * $records_per_page;
    }

    // Query untuk mengambil data laporan TB
    $query = "SELECT * FROM terduga_tb
              WHERE sumber_laporan = :nama_instansi
              AND YEAR(tanggal) = YEAR(CURDATE())
              AND MONTH(tanggal) = MONTH(CURDATE())
              ORDER BY tanggal DESC
              LIMIT :limit OFFSET :offset";
    $stmt = $pdo->prepare($query);
    $stmt->bindParam(':nama_instansi', $_SESSION['jejaring_nama_instansi'], PDO::PARAM_STR);
    $stmt->bindParam(':limit', $records_per_page, PDO::PARAM_INT);
    $stmt->bindParam(':offset', $offset, PDO::PARAM_INT);
    $stmt->execute();
    $laporan_tb = $stmt->fetchAll(PDO::FETCH_ASSOC);

} catch (PDOException $e) {
    $error_message = "Error database: " . $e->getMessage();
}

// Fungsi untuk format tanggal
function formatDateIndonesian($dateString) {
    if (empty($dateString) || $dateString == '0000-00-00') {
        return '-';
    }
    $timestamp = strtotime($dateString);
    if ($timestamp === false) {
        return '-';
    }
    $bulan = ['Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni', 'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'];
    return date('d', $timestamp) . ' ' . $bulan[date('n', $timestamp) - 1] . ' ' . date('Y', $timestamp);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title><?php echo $pageTitle; ?> - RekamSehat</title>
    <link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;500;600;700&display=swap" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <style>
        :root {
            --primary-color: #4dab25;
            --secondary-color: #024d52;
            --bg-light: #f4f7f6;
            --text-dark: #333;
            --text-light: #fefefe;
            --border-color: #e0e0e0;
            --sidebar-width: 250px;
            --header-height: 60px;
        }

        body {
            font-family: 'Poppins', sans-serif;
            margin: 0;
            background-color: var(--bg-light);
            color: var(--text-dark);
            display: flex;
            min-height: 100vh;
            overflow-x: hidden;
        }

        /* --- Layout Styles (same as before) --- */
        .header {
            position: fixed;
            top: 0;
            left: 0;
            width: 100%;
            height: var(--header-height);
            background-color: #fff;
            box-shadow: 0 2px 4px rgba(0,0,0,0.1);
            display: flex;
            align-items: center;
            padding: 0 0px;
            z-index: 1000;
            transition: margin-left 0.3s ease;
        }
        .header .logo {
            font-size: 1.5rem;
            font-weight: 700;
            color: var(--secondary-color);
            margin-right: auto;
        }
        .header .user-info {
            display: flex;
            align-items: center;
            font-size: 0.9rem;
            color: var(--text-dark);
        }
        .header .user-info .user-icon {
            margin-right: 8px;
            color: var(--primary-color);
        }
        .header .user-info .username {
            font-weight: 600;
            margin-right: 15px;
        }
        .header .logout-btn {
            background: none;
            border: none;
            color: var(--secondary-color);
            cursor: pointer;
            font-size: 1rem;
            padding: 5px 10px;
            border-radius: 5px;
            transition: background-color 0.3s ease;
        }
        .header .logout-btn:hover {
            background-color: rgba(0,0,0,0.05);
        }
        .sidebar {
            width: var(--sidebar-width);
            background: var(--secondary-color);
            color: var(--text-light);
            padding-top: var(--header-height);
            position: fixed;
            top: 0;
            left: 0;
            height: 100%;
            box-shadow: 2px 0 5px rgba(0,0,0,0.1);
            z-index: 999;
            transition: transform 0.3s ease;
            transform: translateX(0);
            overflow-y: auto;
        }
        .sidebar.hidden {
            transform: translateX(calc(-1 * var(--sidebar-width)));
        }
        .sidebar-header {
            padding: 20px;
            text-align: center;
            background-color: var(--primary-color);
            color: var(--text-light);
            font-size: 1.2rem;
            font-weight: 600;
            margin-bottom: 20px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        .sidebar ul {
            list-style: none;
            padding: 0;
            margin: 0;
        }
        .sidebar ul li a {
            display: flex;
            align-items: center;
            padding: 15px 20px;
            color: var(--text-light);
            text-decoration: none;
            font-size: 1rem;
            transition: background-color 0.3s ease, color 0.3s ease;
        }
        .sidebar ul li a i {
            margin-right: 10px;
            font-size: 1.2rem;
        }
        .sidebar ul li a:hover,
        .sidebar ul li a.active {
            background-color: rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        .sidebar .dropdown-btn {
            display: flex;
            align-items: center;
            justify-content: space-between;
            padding: 15px 20px;
            color: var(--text-light);
            text-decoration: none;
            font-size: 1rem;
            transition: background-color 0.3s ease, color 0.3s ease;
            cursor: pointer;
        }
        .sidebar .dropdown-btn i {
            margin-right: 10px;
            font-size: 1.2rem;
        }
        .sidebar .dropdown-btn:hover,
        .sidebar .dropdown-btn.active {
            background-color: rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        .sidebar .dropdown-container {
            display: none;
            background-color: rgba(0, 0, 0, 0.1);
            padding-left: 20px;
        }
        .sidebar .dropdown-container a {
            padding: 12px 16px;
            text-decoration: none;
            color: var(--text-light);
            display: block;
            transition: background-color 0.3s ease, color 0.3s ease;
        }
        .sidebar .dropdown-container a:hover,
        .sidebar .dropdown-container a.active {
            background-color: rgba(255, 255, 255, 0.2);
            color: #fff;
        }
        .sidebar .dropdown-container.show {
            display: block;
        }
        .main-content {
            margin-left: var(--sidebar-width);
            padding: calc(var(--header-height) + 30px) 30px 30px 30px;
            flex-grow: 1;
            transition: margin-left 0.3s ease;
            width: calc(100% - var(--sidebar-width));
        }
        .main-content.full-width {
            margin-left: 0;
            width: 100%;
        }
        .sidebar-toggle {
            position: fixed;
            top: 15px;
            left: calc(var(--sidebar-width) + 15px);
            background-color: var(--primary-color);
            color: white;
            border: none;
            border-radius: 5px;
            padding: 8px 12px;
            cursor: pointer;
            z-index: 1001;
            transition: left 0.3s ease;
            font-size: 1.2rem;
            box-shadow: 0 2px 5px rgba(0,0,0,0.2);
        }
        .sidebar-toggle.shifted {
            left: 15px;
        }
        .sidebar-toggle:hover {
            background-color: #3e8d22;
        }
        
        /* --- Table Styles --- */
        .table-container {
            background-color: #fff;
            padding: 20px;
            border-radius: 10px;
            box-shadow: 0 4px 10px rgba(0,0,0,0.08);
            margin-top: 20px;
            overflow-x: auto;
        }
        .data-table {
            width: 100%;
            border-collapse: collapse;
            margin-top: 15px;
        }
        .data-table th, .data-table td {
            padding: 12px 15px;
            text-align: left;
            border-bottom: 1px solid var(--border-color);
        }
        .data-table th {
            background-color: var(--primary-color);
            color: var(--text-light);
            font-weight: 600;
            text-transform: uppercase;
            font-size: 0.9rem;
        }
        .data-table tbody tr:nth-child(even) {
            background-color: #f8f8f8;
        }
        .data-table tbody tr:hover {
            background-color: #f1f1f1;
        }
        .data-table td img {
            max-width: 80px;
            height: auto;
            border-radius: 5px;
            display: block;
            margin: 0 auto;
        }
        .no-data-message {
            text-align: center;
            padding: 30px;
            color: #777;
            font-style: italic;
        }

        /* Alert messages */
        .alert {
            padding: 15px;
            border-radius: 8px;
            margin-bottom: 20px;
            font-weight: 500;
        }
        .alert.error-message {
            background-color: #ffe6e6;
            color: #cc0000;
            border: 1px solid #ffb3b3;
        }
        .alert.success-message {
            background-color: #e6ffe6;
            color: #008000;
            border: 1px solid #b3ffb3;
        }
        
        /* Report Button (the modal trigger) */
        .report-button-container {
            text-align: right;
            margin-bottom: 20px;
        }
        .report-button {
            display: inline-flex;
            align-items: center;
            background-color: var(--primary-color);
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 8px;
            text-decoration: none;
            font-weight: 600;
            transition: background-color 0.3s ease, transform 0.2s ease;
            box-shadow: 0 4px 8px rgba(0,0,0,0.1);
            cursor: pointer;
            font-size: 1rem;
        }
        .report-button:hover {
            background-color: #3e8d22;
            transform: translateY(-2px);
        }
        .report-button i {
            margin-right: 8px;
            font-size: 1.1rem;
        }

        /* --- Modal Styles --- */
        .modal {
            display: none;
            position: fixed;
            z-index: 2000;
            left: 0;
            top: 0;
            width: 100%;
            height: 100%;
            overflow: auto;
            background-color: rgba(0,0,0,0.6);
            justify-content: center;
            align-items: center;
        }
        .modal.show-modal {
            display: flex;
        }
        .modal-content {
            background-color: #fefefe;
            margin: auto;
            padding: 30px;
            border-radius: 10px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.3);
            width: 90%;
            max-width: 600px;
            position: relative;
            animation: fadeIn 0.3s ease-out;
        }
        .close-button {
            color: #aaa;
            position: absolute;
            top: 15px;
            right: 25px;
            font-size: 28px;
            font-weight: bold;
            cursor: pointer;
            transition: color 0.3s ease;
        }
        .close-button:hover,
        .close-button:focus {
            color: #333;
            text-decoration: none;
        }
        .modal-content h3 {
            color: var(--secondary-color);
            margin-top: 0;
            margin-bottom: 25px;
            text-align: center;
            font-size: 1.8rem;
        }
        .form-group {
            margin-bottom: 15px;
        }
        .form-group label {
            display: block;
            margin-bottom: 8px;
            font-weight: 500;
            color: var(--text-dark);
        }
        .form-control {
            width: calc(100% - 20px);
            padding: 10px;
            border: 1px solid var(--border-color);
            border-radius: 5px;
            font-size: 1rem;
            font-family: 'Poppins', sans-serif;
            transition: border-color 0.3s ease, box-shadow 0.3s ease;
            box-sizing: border-box;
        }
        .form-control:focus {
            border-color: var(--primary-color);
            box-shadow: 0 0 0 3px rgba(77, 171, 37, 0.2);
            outline: none;
        }
        textarea.form-control {
            resize: vertical;
            min-height: 80px;
        }
        .form-group.error input,
        .form-group.error textarea,
        .form-group.error select {
            border-color: #e74c3c;
        }
        .error-message-form {
            color: #e74c3c;
            font-size: 0.85rem;
            margin-top: 5px;
            display: none;
        }
        .modal-buttons {
            text-align: right;
            margin-top: 30px;
        }
        .modal-buttons button {
            padding: 10px 20px;
            border: none;
            border-radius: 8px;
            cursor: pointer;
            font-size: 1rem;
            font-weight: 600;
            transition: background-color 0.3s ease;
        }
        .modal-buttons .submit-btn {
            background-color: var(--primary-color);
            color: white;
            margin-left: 10px;
        }
        .modal-buttons .submit-btn:hover {
            background-color: #3e8d22;
        }
        .modal-buttons .cancel-btn {
            background-color: #ccc;
            color: var(--text-dark);
        }
        .modal-buttons .cancel-btn:hover {
            background-color: #bbb;
        }
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }

        /* --- Responsive Design (same as before) --- */
        @media (max-width: 768px) {
            .sidebar {
                transform: translateX(calc(-1 * var(--sidebar-width)));
                box-shadow: none;
            }
            .sidebar.show {
                transform: translateX(0);
            }
            .main-content {
                margin-left: 0;
                width: 100%;
                padding: calc(var(--header-height) + 20px) 20px 20px 20px;
            }
            .main-content.full-width {
                margin-left: 0;
            }
            .sidebar-toggle {
                left: 15px;
            }
            .sidebar-toggle.shifted {
                left: calc(var(--sidebar-width) + 15px);
            }
            .report-button-container {
                text-align: center;
            }
            .modal-content {
                width: 95%;
                padding: 20px;
            }
            .modal-content h3 {
                font-size: 1.5rem;
            }
            .form-control {
                width: calc(100% - 20px);
            }
            .pagination a, .pagination span {
                padding: 6px 10px;
            }
        }
        @media (max-width: 500px) {
            .header {
                padding: 0 10px;
            }
            .header .logo {
                font-size: 1.3rem;
            }
            .header .user-info {
                font-size: 0.8rem;
            }
            .header .logout-btn {
                font-size: 0.9rem;
                padding: 3px 8px;
            }
            .main-content {
                padding: calc(var(--header-height) + 15px) 15px 15px 15px;
            }
            .data-table th, .data-table td {
                padding: 8px 10px;
            }
        }
    </style>
</head>
<body>
    <div class="header">
        <button class="sidebar-toggle" id="sidebarToggle">
            <i class="fas fa-bars"></i>
        </button>
        <div class="logo"></div>
        <div class="user-info">
            <span class="username"><i class="fas fa-user-circle user-icon"></i> <?php echo $loggedInNamaInstansi; ?></span>
            <a href="../jejaring_logout.php" class="logout-btn"><i class="fas fa-sign-out-alt"></i> Logout</a>
        </div>
    </div>

    <aside class="sidebar" id="sidebar">
        <div class="sidebar-header">
            Menu Jejaring
        </div>
        <ul>
            <li><a href="../"><i class="fas fa-tachometer-alt"></i> Dashboard</a></li>
            <li>
                <a href="#" class="dropdown-btn active">
                    <i class="fas fa-file-alt"></i> Laporan
                    <i class="fas fa-caret-down"></i>
                </a>
                <div class="dropdown-container show">
                    <a href="../kehamilan/kehamilan.php"><i class="fas fa-baby"></i> Laporan Kehamilan</a>
                    <a href="#" class="active"><i class="fas fa-lungs"></i> Laporan Terduga TB</a>
                </div>
            </li>
            <li><a href="../change_password.php"><i class="fas fa-key"></i> Ubah Kata Sandi</a></li>
        </ul>
    </aside>

    <main class="main-content" id="mainContent">
        <h2><?php echo $pageTitle; ?></h2>

        <?php if (isset($_SESSION['success_message'])): ?>
            <div class="alert success-message">
                <?php echo $_SESSION['success_message']; ?>
            </div>
            <?php unset($_SESSION['success_message']); ?>
        <?php endif; ?>

        <?php if (isset($_SESSION['error_message'])): ?>
            <div class="alert error-message">
                <?php echo $_SESSION['error_message']; ?>
            </div>
            <?php unset($_SESSION['error_message']); ?>
        <?php endif; ?>

        <div class="report-button-container">
            <button id="openReportModal" class="report-button">
                <i class="fas fa-plus-circle"></i> Laporkan Terduga TB Baru
            </button>
        </div>

        <?php if (!empty($error_message)): ?>
            <div class="alert error-message"><?php echo $error_message; ?></div>
        <?php endif; ?>

        <div class="table-container">
            <?php if ($total_records == 0): ?>
                <p class="no-data-message">Belum ada laporan terduga TB yang tersedia untuk bulan ini.</p>
            <?php else: ?>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>No.</th>
                            <th>Tanggal Lapor</th>
                            <th>Nama Terduga</th>
                            <th>NIK</th>
                            <th>Umur</th>
                            <th>Jenis Kelamin</th>
                            <th>Alamat</th>
                            <th>Desa</th>
                            <th>No. HP</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $start_number = ($current_page - 1) * $records_per_page + 1;
                        foreach ($laporan_tb as $laporan):
                        ?>
                            <tr>
                                <td><?php echo $start_number++; ?></td>
                                <td><?php echo formatDateIndonesian($laporan['tanggal']); ?></td>
                                <td><?php echo htmlspecialchars($laporan['nama_terduga']); ?></td>
                                <td><?php echo htmlspecialchars($laporan['nik']); ?></td>
                                <td><?php echo htmlspecialchars($laporan['umur']); ?></td>
                                <td><?php echo htmlspecialchars($laporan['jenis_kelamin']); ?></td>
                                <td><?php echo htmlspecialchars($laporan['alamat']); ?></td>
                                <td><?php echo htmlspecialchars($laporan['desa']); ?></td>
                                <td><?php echo htmlspecialchars($laporan['no_hp']); ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>

                <div class="pagination">
                    <?php if ($current_page > 1): ?>
                        <a href="?page=<?php echo $current_page - 1; ?>">Previous</a>
                    <?php else: ?>
                        <span class="disabled">Previous</span>
                    <?php endif; ?>

                    <?php for ($i = 1; $i <= $total_pages; $i++): ?>
                        <a href="?page=<?php echo $i; ?>" class="<?php echo ($i == $current_page) ? 'active' : ''; ?>">
                            <?php echo $i; ?>
                        </a>
                    <?php endfor; ?>

                    <?php if ($current_page < $total_pages): ?>
                        <a href="?page=<?php echo $current_page + 1; ?>">Next</a>
                    <?php else: ?>
                        <span class="disabled">Next</span>
                    <?php endif; ?>
                </div>
            <?php endif; ?>
        </div>
    </main>

    <div id="reportModal" class="modal">
        <div class="modal-content">
            <span class="close-button">&times;</span>
            <h3>Laporkan Terduga TB Baru</h3>
            <form action="../../api/lapor_terduga_tb.php" method="POST" id="reportForm">
                <input type="hidden" name="sumber_laporan" value="<?php echo $loggedInNamaInstansi; ?>">

                <div class="form-group">
                    <label for="tanggal">Tanggal Lapor:</label>
                    <input type="date" id="tanggal" name="tanggal" class="form-control" max="<?php echo date('Y-m-d'); ?>" value="<?php echo date('Y-m-d'); ?>" required>
                    <span class="error-message-form" id="tanggalError">Tanggal tidak boleh di masa depan.</span>
                </div>
                <div class="form-group">
                    <label for="nama_terduga">Nama Terduga TB:</label>
                    <input type="text" id="nama_terduga" name="nama_terduga" class="form-control" required>
                </div>
                <div class="form-group">
                    <label for="nik_terduga">NIK:</label>
                    <input type="text" id="nik_terduga" name="nik_terduga" class="form-control" pattern="\d{16}" title="NIK harus 16 digit angka" required minlength="16" maxlength="16">
                    <span class="error-message-form" id="nikError">NIK harus 16 digit angka.</span>
                </div>
                <div class="form-group">
                    <label for="umur">Umur:</label>
                    <input type="number" id="umur" name="umur" class="form-control" min="0" max="150" required>
                </div>
                <div class="form-group">
                    <label for="jenis_kelamin">Jenis Kelamin:</label>
                    <select id="jenis_kelamin" name="jenis_kelamin" class="form-control" required>
                        <option value="">-- Pilih Jenis Kelamin --</option>
                        <option value="Laki-laki">Laki-laki</option>
                        <option value="Perempuan">Perempuan</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="alamat">Alamat:</label>
                    <textarea id="alamat" name="alamat" class="form-control" required></textarea>
                </div>
                <div class="form-group">
                    <label for="desa">Desa:</label>
                    <select id="desa" name="desa" class="form-control" required>
                        <option value="">-- Pilih Desa --</option>
                        <option value="RONDOKUNING">RONDOKUNING</option>
                        <option value="KREGENAN">KREGENAN</option>
                        <option value="SEMAMPIR">SEMAMPIR</option>
                        <option value="BULU">BULU</option>
                        <option value="SIDOMUKTI">SIDOMUKTI</option>
                        <option value="KRAKSAAN WETAN">KRAKSAAN WETAN</option>
                        <option value="RANGKANG">RANGKANG</option>
                        <option value="KANDANGJATI KULON">KANDANGJATI KULON</option>
                        <option value="KANDANGJATI WETAN">KANDANGJATI WETAN</option>
                        <option value="ALASSUMUR KULON">ALASSUMUR KULON</option>
                        <option value="SUMBERLELE">SUMBERLELE</option>
                        <option value="TAMANSARI">TAMANSARI</option>
                        <option value="ASEMBAKOR">ASEMBAKOR</option>
                        <option value="KEBONAGUNG">KEBONAGUNG</option>
                        <option value="SIDOPEKSO">SIDOPEKSO</option>
                        <option value="PATOKAN">PATOKAN</option>
                        <option value="ASEMBAGUS">ASEMBAGUS</option>
                        <option value="KALIBUNTU">KALIBUNTU</option>
                    </select>
                </div>
                <div class="form-group">
                    <label for="no_hp">No. HP:</label>
                    <input type="text" id="no_hp" name="no_hp" class="form-control" placeholder="Opsional" pattern="[0-9]{10,15}" title="Masukkan nomor HP yang valid (10-15 digit)">
                    <span class="error-message-form" id="hpError">Nomor HP harus terdiri dari 10-15 digit angka.</span>
                </div>

                <div class="modal-buttons">
                    <button type="button" class="cancel-btn" id="closeModalButton">Batal</button>
                    <button type="submit" class="submit-btn" id="submitReportBtn">Laporkan</button>
                </div>
            </form>
        </div>
    </div>

    <script>
        document.addEventListener('DOMContentLoaded', function() {
            const sidebarToggle = document.getElementById('sidebarToggle');
            const sidebar = document.getElementById('sidebar');
            const mainContent = document.getElementById('mainContent');
            const dropdownBtn = document.querySelector('.dropdown-btn');
            const dropdownContainer = document.querySelector('.dropdown-container');

            // Modal elements
            const openReportModalBtn = document.getElementById('openReportModal');
            const reportModal = document.getElementById('reportModal');
            const closeModalBtn = document.querySelector('.close-button');
            const cancelModalBtn = document.getElementById('closeModalButton');
            const nikInput = document.getElementById('nik_terduga');
            const noHpInput = document.getElementById('no_hp');
            const tanggalInput = document.getElementById('tanggal');
            const nikError = document.getElementById('nikError');
            const hpError = document.getElementById('hpError');
            const tanggalError = document.getElementById('tanggalError');

            function checkSidebarVisibility() {
                if (window.innerWidth <= 768) {
                    sidebar.classList.add('hidden');
                    mainContent.classList.add('full-width');
                    sidebarToggle.classList.add('shifted');
                } else {
                    sidebar.classList.remove('hidden');
                    mainContent.classList.remove('full-width');
                    sidebarToggle.classList.remove('shifted');
                }
            }
            checkSidebarVisibility();
            window.addEventListener('resize', checkSidebarVisibility);
            sidebarToggle.addEventListener('click', function() {
                sidebar.classList.toggle('hidden');
                mainContent.classList.toggle('full-width');
                sidebarToggle.classList.toggle('shifted');
            });

            const sidebarLinks = document.querySelectorAll('.sidebar ul li a');
            sidebarLinks.forEach(link => {
                link.addEventListener('click', function() {
                    if (window.innerWidth <= 768) {
                        sidebar.classList.add('hidden');
                        mainContent.classList.add('full-width');
                        sidebarToggle.classList.add('shifted');
                    }
                });
            });

            // Dropdown functionality
            if (document.querySelector('.dropdown-container a.active')) {
                dropdownContainer.classList.add('show');
                dropdownBtn.classList.add('active');
            }
            dropdownBtn.addEventListener('click', function(event) {
                event.preventDefault();
                this.classList.toggle('active');
                dropdownContainer.classList.toggle('show');
            });

            // --- Modal Functions ---
            function openModal() {
                reportModal.classList.add('show-modal');
            }

            function closeModal() {
                reportModal.classList.remove('show-modal');
                document.getElementById('reportForm').reset();
                
                // Reset validation errors
                document.getElementById('nik_terduga').parentElement.classList.remove('error');
                nikError.style.display = 'none';
                document.getElementById('tanggal').parentElement.classList.remove('error');
                tanggalError.style.display = 'none';
                document.getElementById('no_hp').parentElement.classList.remove('error');
                hpError.style.display = 'none';
            }

            // Event listeners for modal
            openReportModalBtn.addEventListener('click', openModal);
            closeModalBtn.addEventListener('click', closeModal);
            cancelModalBtn.addEventListener('click', closeModal);
            
            // Form submission validation
            const reportForm = document.getElementById('reportForm');
            reportForm.addEventListener('submit', function(event) {
                let isValid = true;
                
                // NIK validation
                if (nikInput.value.length !== 16 || isNaN(nikInput.value)) {
                    nikInput.parentElement.classList.add('error');
                    nikError.style.display = 'block';
                    isValid = false;
                } else {
                    nikInput.parentElement.classList.remove('error');
                    nikError.style.display = 'none';
                }

                // Tanggal validation
                const tanggalLapor = new Date(tanggalInput.value);
                const today = new Date();
                if (tanggalLapor > today) {
                    tanggalInput.parentElement.classList.add('error');
                    tanggalError.style.display = 'block';
                    isValid = false;
                } else {
                    tanggalInput.parentElement.classList.remove('error');
                    tanggalError.style.display = 'none';
                }
                
                // No HP validation (opsional, hanya jika diisi)
                if (noHpInput.value.length > 0 && !noHpInput.checkValidity()) {
                    noHpInput.parentElement.classList.add('error');
                    hpError.style.display = 'block';
                    isValid = false;
                } else {
                    noHpInput.parentElement.classList.remove('error');
                    hpError.style.display = 'none';
                }

                if (!isValid) {
                    event.preventDefault();
                    alert('Mohon perbaiki kesalahan pada formulir.');
                }
            });
        });
    </script>
</body>
</html>