<?php 
// 1. Cek Keamanan Login
require_once $_SERVER['DOCUMENT_ROOT'] . '/auth/cek_login.php'; 

// 2. Hubungkan ke database
include $_SERVER['DOCUMENT_ROOT'] . '/assets/db/db.php';
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>SINKARKES - Database Sertifikat</title>
    
    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css">
    <link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@400;600;700&display=swap" rel="stylesheet">

    <style>
        body { background-color: #f4f7f6; font-family: 'Open Sans', sans-serif; color: #444; }
        
        /* Navbar Area */
        .navbar-custom { background: #ffffff; border-bottom: 3px solid #40ada6; padding: 12px 0; box-shadow: 0 2px 10px rgba(0,0,0,0.05); }
        .header-logo { height: 45px; }
        
        /* Info Admin & Logout */
        .user-info { font-size: 0.8rem; border-right: 1px solid #ddd; padding-right: 15px; margin-right: 15px; line-height: 1.2; }
        .btn-logout { border-radius: 20px; font-weight: 700; font-size: 0.7rem; text-transform: uppercase; padding: 4px 12px; }
        .btn-profil { border-radius: 20px; font-weight: 700; font-size: 0.7rem; text-transform: uppercase; padding: 4px 12px; margin-right: 5px; }

        .card-main { border: none; border-radius: 15px; box-shadow: 0 10px 30px rgba(0,0,0,0.08); margin-top: 20px; }
        .card-header-custom { background: #fff; border-bottom: 1px solid #eee; padding: 20px 25px; border-radius: 15px 15px 0 0 !important; }
        
        .search-box { position: relative; max-width: 400px; width: 100%; }
        .search-box input { border-radius: 20px; padding-left: 40px; border: 1px solid #ced4da; }
        .search-box i { position: absolute; left: 15px; top: 10px; color: #aaa; }

        .table thead th { background: #f8fbfa; color: #40ada6; font-weight: 700; text-transform: uppercase; font-size: 0.75rem; text-align: center; border: none; }
        .table td { vertical-align: middle; font-size: 0.9rem; padding: 15px 10px; }
        .table tbody tr:hover { background-color: #f1f9f8; }
        
        .no-icv-text { color: #d9534f; font-family: 'Courier New', Courier, monospace; font-weight: 700; }
        .badge-vaksin-info { background: #eef9f8; color: #358e88; border: 1px solid #ccece9; padding: 8px 12px; border-radius: 8px; display: block; margin-bottom: 5px; }
        .badge-vaksin-secondary { background: #f8f9fa; color: #666; border: 1px solid #ddd; padding: 8px 12px; border-radius: 8px; display: block; }

        .btn-add { background: #40ada6; color: #fff; border-radius: 8px; font-weight: 600; padding: 8px 18px; font-size: 0.9rem; }
        .btn-add:hover { background: #358e88; color: #fff; }
        
        .action-btns .btn { width: 35px; height: 35px; line-height: 35px; padding: 0; border-radius: 8px; margin: 0 2px; border: none; color: #fff; }
        .btn-edit { background: #ffc107; } .btn-edit:hover { background: #e0a800; }
        .btn-print { background: #17a2b8; } .btn-print:hover { background: #138496; }
        .btn-del { background: #dc3545; } .btn-del:hover { background: #c82333; }

        .status-valid { color: #28a745; font-weight: 700; }
        footer { color: #aaa; margin: 40px 0; }
    </style>
</head>
<body>

<nav class="navbar navbar-custom">
    <div class="container-fluid px-4 d-flex justify-content-between align-items-center">
        <a class="navbar-brand" href="../entry/admin.php">
            <img src="../assets/img/SINKARKES.png" alt="SINKARKES" class="header-logo">
        </a>
        
        <div class="d-flex align-items-center">
            <div class="user-info d-none d-md-block text-right">
                <span class="text-muted">Admin:</span><br>
                <strong class="text-dark"><?php echo $_SESSION['nama_admin']; ?></strong>
            </div>

            <a href="../auth/profil.php" class="btn btn-outline-info btn-profil" title="Ganti Password">
                <i class="fas fa-user-cog"></i> Profil
            </a>

            <a href="../auth/logout.php" class="btn btn-outline-danger btn-logout" onclick="return confirm('Keluar dari sistem?')">
                <i class="fas fa-sign-out-alt"></i> Logout
            </a>
        </div>
    </div>
</nav>

<div class="container-fluid px-4">
    <div class="card card-main">
        <div class="card-header-custom d-flex flex-wrap justify-content-between align-items-center">
            <div class="mb-3 mb-md-0">
                <h5 class="m-0 font-weight-bold" style="color: #333;">Database Sertifikat Nasional</h5>
                <div class="d-flex align-items-center mt-1">
                    <span class="badge badge-pill badge-light border mr-2">Total: 
                        <?php 
                            $count_res = $conn->query("SELECT COUNT(*) as total FROM sertifikat_vaksin");
                            echo $count_res->fetch_assoc()['total']; 
                        ?> Data
                    </span>
                    <a href="../entry/admin.php" class="btn btn-add btn-sm">
                        <i class="fas fa-plus-circle mr-1"></i> Tambah Baru
                    </a>
                </div>
            </div>
            
            <div class="search-box">
                <i class="fas fa-search"></i>
                <input type="text" id="searchInput" class="form-control" placeholder="Cari Nama, NIK, atau No. ICV...">
            </div>
        </div>

        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table" id="sertifikatTable">
                    <thead>
                        <tr>
                            <th>Nomor ICV</th>
                            <th class="text-left">Identitas Pemegang</th>
                            <th class="text-left">Paspor & Lahir</th>
                            <th class="text-left">Detail Vaksinasi</th>
                            <th>Tgl Vaksin</th>
                            <th>Masa Berlaku</th>
                            <th>Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        $query = "SELECT * FROM sertifikat_vaksin ORDER BY id DESC";
                        $result = $conn->query($query);
                        
                        if ($result && $result->num_rows > 0) {
                            while($row = $result->fetch_assoc()) {
                                $tgl_lahir = ($row['tgl_lahir']) ? date('d/m/Y', strtotime($row['tgl_lahir'])) : "-";
                                ?>
                                <tr>
                                    <td class="text-center no-icv-text"><?php echo $row['no_icv']; ?></td>
                                    <td>
                                        <div class="font-weight-bold text-uppercase"><?php echo $row['nama_lengkap']; ?></div>
                                        <div class="small text-muted">NIK: <?php echo $row['nik']; ?></div>
                                        <div class="small text-muted">Gender: <?php echo $row['jenis_kelamin']; ?></div>
                                    </td>
                                    <td>
                                        <div class="small"><i class="fas fa-passport fa-fw text-muted"></i> <?php echo $row['no_paspor']; ?></div>
                                        <div class="small"><i class="fas fa-calendar-alt fa-fw text-muted"></i> <?php echo $tgl_lahir; ?></div>
                                    </td>
                                    <td>
                                        <div class="badge-vaksin-info">
                                            <strong>1. <?php echo $row['vaksin_1_nama']; ?></strong><br>
                                            <small>Lot: <?php echo $row['vaksin_1_batch']; ?></small>
                                        </div>
                                        <?php if (!empty($row['vaksin_2_nama'])): ?>
                                        <div class="badge-vaksin-secondary">
                                            <strong>2. <?php echo $row['vaksin_2_nama']; ?></strong><br>
                                            <small>Lot: <?php echo $row['vaksin_2_batch']; ?></small>
                                        </div>
                                        <?php endif; ?>
                                    </td>
                                    <td class="text-center small">
                                        <?php echo date('d/m/Y', strtotime($row['vaksin_1_tgl_admin'])); ?>
                                        <?php if(!empty($row['vaksin_2_tgl_admin'])) echo "<hr class='my-1'>".date('d/m/Y', strtotime($row['vaksin_2_tgl_admin'])); ?>
                                    </td>
                                    <td class="text-center status-valid small">
                                        <?php echo date('d/m/Y', strtotime($row['vaksin_1_valid_sampai'])); ?>
                                        <?php if(!empty($row['vaksin_2_valid_sampai'])) echo "<hr class='my-1'>".date('d/m/Y', strtotime($row['vaksin_2_valid_sampai'])); ?>
                                    </td>
                                    <td class="text-center">
                                        <div class="action-btns d-flex justify-content-center">
                                            <a href="edit.php?id=<?php echo $row['id']; ?>" class="btn btn-edit" title="Edit Data">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                            <a href="generate_pdf.php?id=<?php echo $row['id']; ?>" class="btn btn-print" target="_blank" title="Cetak PDF">
                                                <i class="fas fa-print"></i>
                                            </a>
                                            <a href="hapus.php?id=<?php echo $row['id']; ?>" class="btn btn-del" onclick="return confirm('Hapus data <?php echo addslashes($row['nama_lengkap']); ?>?')" title="Hapus">
                                                <i class="fas fa-trash"></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>
                                <?php
                            }
                        } else {
                            echo "<tr><td colspan='7' class='text-center py-5 text-muted'>Belum ada sertifikat terdaftar.</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    
    <footer class="text-center small">
        &copy; 2026 Balai Kekarantinaan Kesehatan &mdash; Kementerian Kesehatan Republik Indonesia
    </footer>
</div>

<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/js/bootstrap.bundle.min.js"></script>

<script>
    $(document).ready(function(){
        $("#searchInput").on("keyup", function() {
            var value = $(this).val().toLowerCase();
            $("#sertifikatTable tbody tr").filter(function() {
                $(this).toggle($(this).text().toLowerCase().indexOf(value) > -1)
            });
        });
    });
</script>

</body>
</html>