<?php 
include $_SERVER['DOCUMENT_ROOT'] . '/assets/db/db.php';

/**
 * File: list_sertifikat.php
 * Deskripsi: Menampilkan daftar sertifikat vaksin berdasarkan struktur database terbaru.
 * Database: sertifikat_vaksin (no_icv, nama_lengkap, no_paspor, tgl_lahir, vaksin_1_nama, dll.)
 */
?>
<!DOCTYPE html>
<html lang="id">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard Admin - Daftar 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.3/css/all.min.css">
    <style>
        .table td { vertical-align: middle; }
        .badge-vaksin { display: block; font-size: 0.8rem; margin-bottom: 2px; text-align: left; }
        .table thead th { vertical-align: middle; }
    </style>
</head>
<body class="bg-light">
<nav class="navbar navbar-expand-lg navbar-dark bg-primary shadow-sm">
    <div class="container-fluid">
        <a class="navbar-brand" href="#"><i class="fas fa-shield-alt"></i> Panel Admin Sertifikat</a>
        <div class="ml-auto">
            <a href="../entry/admin.php" class="btn btn-success">Tambah Data Baru</a>
        </div>
    </div>
</nav>

<div class="container-fluid mt-4">
    <div class="card shadow-sm">
        <div class="card-header bg-white d-flex justify-content-between align-items-center">
            <h5 class="m-0 font-weight-bold text-primary">Database Sertifikat Terdaftar</h5>
            <span class="badge badge-primary p-2">
                Total Data: <?php 
                    $count_res = $conn->query("SELECT COUNT(*) as total FROM sertifikat_vaksin");
                    $count_data = $count_res->fetch_assoc();
                    echo $count_data['total']; 
                ?>
            </span>
        </div>
        <div class="card-body">
            <div class="table-responsive">
                <table class="table table-bordered table-hover">
                    <thead class="thead-dark text-center">
                        <tr>
                            <th>No ICV</th>
                            <th>Nama Lengkap</th>
                            <th>No Paspor / Tgl Lahir</th>
                            <th>Detail Vaksin & Batch</th>
                            <th>Tgl Administrasi</th>
                            <th>Valid Sampai</th>
                            <th width="120">Aksi</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // Query mengambil data sesuai struktur database terbaru
                        $query = "SELECT * FROM sertifikat_vaksin ORDER BY id DESC";
                        $result = $conn->query($query);
                        
                        if ($result && $result->num_rows > 0) {
                            while($row = $result->fetch_assoc()) {
                                // Menangani data vaksin kedua jika ada
                                $vaksin2_info = "";
                                if (!empty($row['vaksin_2_nama'])) {
                                    $vaksin2_info = "<hr class='my-1'><span class='badge badge-secondary badge-vaksin'>
                                        <i class='fas fa-syringe'></i> 2. {$row['vaksin_2_nama']}<br>
                                        <small>Lot: {$row['vaksin_2_batch']}</small>
                                    </span>";
                                }
                                
                                // Format tampilan tanggal [cite: 5, 7]
                                $tgl_lahir = ($row['tgl_lahir']) ? date('d-m-Y', strtotime($row['tgl_lahir'])) : "-";
                                $tgl_admin1 = ($row['vaksin_1_tgl_admin']) ? date('d-m-Y', strtotime($row['vaksin_1_tgl_admin'])) : "-";
                                $tgl_valid1 = ($row['vaksin_1_valid_sampai']) ? date('d-m-Y', strtotime($row['vaksin_1_valid_sampai'])) : "-";
                                
                                echo "<tr>
                                    <td class='text-center font-weight-bold text-danger'>{$row['no_icv']}</td>
                                    <td>" . strtoupper($row['nama_lengkap']) . "</td>
                                    <td>
                                        <small class='text-muted'>Paspor:</small> {$row['no_paspor']}<br>
                                        <small class='text-muted'>Lahir:</small> $tgl_lahir
                                    </td>
                                    <td>
                                        <span class='badge badge-info badge-vaksin'>
                                            <i class='fas fa-syringe'></i> 1. {$row['vaksin_1_nama']}<br>
                                            <small>Lot: {$row['vaksin_1_batch']}</small>
                                        </span>
                                        $vaksin2_info
                                    </td>
                                    <td class='text-center'>
                                        $tgl_admin1" . 
                                        (!empty($row['vaksin_2_tgl_admin']) ? "<br><hr class='my-1'>" . date('d-m-Y', strtotime($row['vaksin_2_tgl_admin'])) : "") . 
                                    "</td>
                                    <td class='text-center text-success font-weight-bold'>
                                        $tgl_valid1" . 
                                        (!empty($row['vaksin_2_valid_sampai']) ? "<br><hr class='my-1'>" . date('d-m-Y', strtotime($row['vaksin_2_valid_sampai'])) : "") . 
                                    "</td>
                                    <td class='text-center'>
                                        <div class='btn-group' role='group'>
                                            <a href='generate_pdf.php?id={$row['id']}' class='btn btn-sm btn-info' target='_blank' title='Cetak PDF'>
                                                <i class='fa fa-print'></i>
                                            </a>
                                            <a href='hapus.php?id={$row['id']}' class='btn btn-sm btn-danger' onclick='return confirm(\"Apakah Anda yakin ingin menghapus data " . addslashes($row['nama_lengkap']) . "?\")' title='Hapus'>
                                                <i class='fa fa-trash'></i>
                                            </a>
                                        </div>
                                    </td>
                                </tr>";
                            }
                        } else {
                            echo "<tr><td colspan='7' class='text-center py-4 text-muted'>Belum ada data sertifikat yang tersimpan dalam database.</td></tr>";
                        }
                        ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<footer class="text-center mt-4 mb-4 text-muted small">
    &copy; 2026 Sistem Verifikasi Sertifikat Internasional | Database SINKARKES
</footer>

</body>
</html>