<!-- Page Title -->
<div class="d-flex justify-content-between align-items-center mb-4">
    <h3 class="mb-0" style="color: var(--sidebar-bg);">
        <i class="bi bi-file-earmark-text me-2"></i>Data Hasil TKA
    </h3>
    <a href="<?php echo site_url('admin/import_hasil_tka'); ?>" class="btn btn-success">
        <i class="bi bi-upload me-2"></i>Import Excel
    </a>
</div>

<!-- Data Table -->
<div class="card">
    <div class="card-header">
        <h5 class="mb-0">Daftar Hasil TKA</h5>
    </div>
    <div class="card-body">
        <div class="table-responsive">
            <table class="table table-hover datatable">
                <thead>
                    <tr>
                        <th>No</th>
                        <th>NISN</th>
                        <th>No Peserta</th>
                        <th>Nama Peserta</th>
                        <th>Tempat, Tgl Lahir</th>
                        <th>B. Indonesia</th>
                        <th>Matematika</th>
                        <th>B. Inggris</th>
                        <th>Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php $no = 1; ?>
                    <?php foreach ($results as $result): ?>
                        <tr>
                            <td><?php echo $no++; ?></td>
                            <td><strong><?php echo $result->NISN; ?></strong></td>
                            <td><?php echo $result->NO_PESERTA; ?></td>
                            <td><?php echo $result->NAMA_PESERTA; ?></td>
                            <td><?php echo $result->TEMPAT_TGL_LAHIR; ?></td>

                            <td>
                                <span class="badge bg-primary"><?php echo $result->BHS_INDONESIA; ?></span>
                            </td>
                            <td>
                                <span class="badge bg-primary"><?php echo $result->MATEMATIKA; ?></span>
                            </td>
                            <td>
                                <span class="badge bg-primary"><?php echo $result->BHS_INGGRIS; ?></span>
                            </td>
                            <td>
                                <a href="#" class="btn btn-sm btn-info" data-bs-toggle="modal"
                                    data-bs-target="#detailModal<?php echo $result->ID; ?>">
                                    <i class="bi bi-eye"></i>
                                </a>
                                <a href="<?= site_url('admin/hasil_tka_delete/' . $result->ID) ?>"
                                    class="btn btn-sm btn-danger confirm-delete">
                                    <i class="bi bi-trash"></i>
                                </a>
                            </td>
                        </tr>


                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<?php
// Generate modal untuk setiap data
if (!empty($results)):
    foreach ($results as $result):
?>
        <!-- Detail Modal -->
        <div class="modal fade" id="detailModal<?php echo $result->ID; ?>" tabindex="-1"
            aria-labelledby="detailModalLabel<?php echo $result->ID; ?>" aria-hidden="true">
            <div class="modal-dialog modal-lg">
                <div class="modal-content">
                    <div class="modal-header">
                        <h5 class="modal-title" id="detailModalLabel<?php echo $result->ID; ?>">Detail Hasil TKA</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">
                                <table class="table table-borderless">
                                    <tr>
                                        <th width="40%">NISN</th>
                                        <td>: <?php echo htmlspecialchars($result->NISN ?? '-'); ?></td>
                                    </tr>
                                    <tr>
                                        <th>No. Peserta</th>
                                        <td>: <?php echo htmlspecialchars($result->NO_PESERTA ?? '-'); ?></td>
                                    </tr>
                                    <tr>
                                        <th>Nama Peserta</th>
                                        <td>: <?php echo htmlspecialchars($result->NAMA_PESERTA ?? '-'); ?></td>
                                    </tr>
                                    <tr>
                                        <th>Tempat/Tgl Lahir</th>
                                        <td>: <?php echo htmlspecialchars($result->TEMPAT_TGL_LAHIR ?? '-'); ?></td>
                                    </tr>
                                </table>
                            </div>
                            <div class="col-md-6">
                                <table class="table table-borderless">
                                    <tr>
                                        <th width="40%">B. Indonesia</th>
                                        <td>: <?php echo htmlspecialchars($result->BHS_INDONESIA ?? '0'); ?></td>
                                    </tr>
                                    <tr>
                                        <th>Matematika</th>
                                        <td>: <?php echo htmlspecialchars($result->MATEMATIKA ?? '0'); ?></td>
                                    </tr>
                                    <tr>
                                        <th>B. Inggris</th>
                                        <td>: <?php echo htmlspecialchars($result->BHS_INGGRIS ?? '0'); ?></td>
                                    </tr>
                                </table>
                            </div>
                        </div>

                        <?php if (!empty($result->NAMA_MATPEL_PIL_1)): ?>
                            <hr>
                            <h6>Mata Pelajaran Pilihan</h6>
                            <div class="row">
                                <div class="col-md-6">
                                    <table class="table table-borderless">
                                        <tr>
                                            <th width="50%"><?php echo htmlspecialchars($result->NAMA_MATPEL_PIL_1); ?></th>
                                            <td>: <?php echo htmlspecialchars($result->NILAI_MATPEL_PIL_1); ?></td>
                                        </tr>
                                    </table>
                                </div>
                                <?php if (!empty($result->NAMA_MATPEL_PIL_2)): ?>
                                    <div class="col-md-6">
                                        <table class="table table-borderless">
                                            <tr>
                                                <th width="50%"><?php echo htmlspecialchars($result->NAMA_MATPEL_PIL_2); ?></th>
                                                <td>: <?php echo htmlspecialchars($result->NILAI_MATPEL_PIL_2); ?></td>
                                            </tr>
                                        </table>
                                    </div>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                        <?php if (!empty($result->LAMPIRAN)): ?>
                            <hr>
                            <h6>Surat Keterangan Hasil TKA</h6>
                            <div class="row">
                                <div class="col-md-12">
                                    <table class="table table-borderless">
                                        <tr>
                                            <th width="100%">
                                                <a href="<?php echo base_url('uploads/' . $result->LAMPIRAN); ?>" target="_blank">

                                                    <button type="button" class="btn btn-sm btn-primary ms-2">
                                                        <i class="bi bi-download"></i> Tampilkan Surat Hasil TKA</button>
                                                </a>
                                            </th>
                                        </tr>
                                    </table>
                                </div>
                            </div>
                        <?php endif; ?>



                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Tutup</button>
                        <!-- <button type="button" class="btn btn-primary" onclick="printResult(<?php echo $result->ID; ?>)">
                            <i class="bi bi-printer me-2"></i>Cetak
                        </button> -->
                    </div>
                </div>
            </div>
        </div>
    <?php endforeach; ?>
<?php endif; ?>