<?= $this->extend('layouts/admin') ?><?= $this->section('content') ?>
<div class="d-flex flex-wrap gap-2 mb-3"><a class="btn btn-emerald" href="<?= base_url('admin/tamu/new') ?>"><i class="bi bi-plus-lg"></i> Tambah</a><a class="btn btn-outline-success" href="<?= base_url('admin/tamu/export') ?>"><i class="bi bi-file-earmark-excel"></i> Export Excel</a><button class="btn btn-outline-secondary" data-bs-toggle="modal" data-bs-target="#importModal"><i class="bi bi-upload"></i> Import Excel</button></div>
<div class="panel table-responsive"><table class="table align-middle"><thead><tr><th>Nama</th><th>HP</th><th>Status</th><th>Link</th><th></th></tr></thead><tbody>
<?php foreach($items as $item): $link=base_url('u/'.$item['slug']); $whatsapp=preg_replace('/\D/','',$item['nomor_hp'] ?? ''); if(str_starts_with($whatsapp,'0')){$whatsapp='62'.substr($whatsapp,1);} ?>
<tr><td class="fw-semibold"><?= esc($item['nama_tamu']) ?></td><td><?= esc($item['nomor_hp'] ?: '-') ?></td><td><span class="badge text-bg-light"><?= str_replace('_',' ',esc($item['status_kehadiran'])) ?></span></td><td><button class="btn btn-sm btn-outline-primary copy-link" data-link="<?= esc($link) ?>" title="Salin link"><i class="bi bi-copy"></i></button> <?php if($whatsapp): ?><a class="btn btn-sm btn-success" target="_blank" rel="noopener" href="https://wa.me/<?= esc($whatsapp) ?>?text=<?= urlencode('Assalamu\'alaikum, berikut undangan Tasyakuran Khitanan kami: '.$link) ?>" title="Kirim WhatsApp"><i class="bi bi-whatsapp"></i></a><?php endif ?></td><td class="text-end"><a class="btn btn-sm btn-outline-warning" href="<?= base_url('admin/tamu/'.$item['id'].'/edit') ?>"><i class="bi bi-pencil"></i></a><form class="d-inline" method="post" action="<?= base_url('admin/tamu/'.$item['id']) ?>"><?= csrf_field() ?><input type="hidden" name="_method" value="DELETE"><button class="btn btn-sm btn-outline-danger" onclick="return confirm('Hapus tamu?')"><i class="bi bi-trash"></i></button></form></td></tr>
<?php endforeach ?></tbody></table></div>
<div class="modal fade" id="importModal"><div class="modal-dialog"><form class="modal-content" method="post" enctype="multipart/form-data" action="<?= base_url('admin/tamu/import') ?>"><?= csrf_field() ?><div class="modal-header"><h5 class="modal-title">Import Excel</h5><button class="btn-close" data-bs-dismiss="modal"></button></div><div class="modal-body"><p class="small text-secondary">Kolom pertama: Nama Tamu. Kolom kedua: Nomor HP. Baris pertama dianggap header.</p><input class="form-control" type="file" name="file_excel" accept=".xlsx,.xls,.csv" required></div><div class="modal-footer"><button class="btn btn-emerald">Import</button></div></form></div></div>
<?= $this->endSection() ?><?= $this->section('scripts') ?><script>document.querySelectorAll('.copy-link').forEach(b=>b.onclick=()=>navigator.clipboard.writeText(b.dataset.link).then(()=>{b.innerHTML='<i class="bi bi-check-lg"></i>'}))</script><?= $this->endSection() ?>
