<?= $this->extend('layouts/main') ?>

<?= $this->section('title') ?>
Manajemen Penguna
<?= $this->endSection() ?>

<?= $this->section('page_title') ?>
Pengaturan User Terdaftar
<?= $this->endSection() ?>

<?= $this->section('content') ?>
<div class="row">
    <div class="col-lg-12">
        <div class="d-flex justify-content-end mb-4">
            <a href="<?= base_url('admin/user/add') ?>" class="btn btn-primary rounded-pill px-4 shadow-sm">
                <i class="bi bi-person-plus-fill me-2"></i> TAMBAH USER
            </a>
        </div>

        <?php if (session()->getFlashdata('success')): ?>
            <div class="alert alert-success rounded-pill px-4" role="alert">
                <?= session()->getFlashdata('success') ?>
            </div>
        <?php endif; ?>

        <?php if (session()->getFlashdata('error')): ?>
            <div class="alert alert-danger rounded-pill px-4" role="alert">
                <?= session()->getFlashdata('error') ?>
            </div>
        <?php endif; ?>

        <div class="glass-card">
            <div class="table-responsive">
                <table class="table table-hover align-middle">
                    <thead>
                        <tr class="text-secondary small fw-bold opacity-75">
                            <th>#ID</th>
                            <th>DATA USER</th>
                            <th>NIP / USERNAME</th>
                            <th>KONTAK (WA)</th>
                            <th>ROLE</th>
                            <th>STATUS</th>
                            <th class="text-center">AKSI</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php if (empty($users)): ?>
                            <tr>
                                <td colspan="7" class="text-center py-5 text-muted">Belum ada data user.</td>
                            </tr>
                        <?php else: ?>
                            <?php foreach ($users as $row): ?>
                                <tr>
                                    <td>#
                                        <?= $row['id'] ?>
                                    </td>
                                    <td>
                                        <div class="fw-bold">
                                            <?= $row['name'] ?>
                                        </div>
                                        <span class="small text-muted">
                                            <?= $row['email'] ?>
                                        </span>
                                    </td>
                                    <td>
                                        <div class="small fw-bold">NIP:
                                            <?= $row['nip'] ?: '-' ?>
                                        </div>
                                        <div class="text-muted small">User:
                                            <?= $row['username'] ?>
                                        </div>
                                    </td>
                                    <td>
                                        <div class="small fw-bold text-success"><i class="bi bi-whatsapp me-1"></i>
                                            <?= $row['no_hp'] ?: '-' ?></div>
                                    </td>
                                    <td>
                                        <?php foreach ($row['roles'] as $role): ?>
                                            <span
                                                class="badge bg-primary bg-opacity-10 text-primary border border-primary small px-3 rounded-pill">
                                                <?= $role['name'] ?>
                                            </span>
                                        <?php endforeach; ?>
                                    </td>
                                     <td>
                                         <div class="d-flex flex-column gap-2">
                                             <?php if ($row['is_active'] == 1): ?>
                                                 <span class="badge rounded-pill bg-success bg-opacity-25 text-success border border-success">AKTIF / DITERIMA</span>
                                             <?php elseif ($row['is_active'] == 2): ?>
                                                 <span class="badge rounded-pill bg-warning bg-opacity-25 text-warning border border-warning">PENDING (MENUNGGU)</span>
                                             <?php else: ?>
                                                 <span class="badge rounded-pill bg-danger bg-opacity-25 text-danger border border-danger">DITOLAK / NONAKTIF</span>
                                             <?php endif; ?>

                                             <div class="btn-group btn-group-sm rounded-3 overflow-hidden border shadow-xs" style="font-size: 0.75rem;">
                                                 <a href="<?= base_url('admin/user/setStatus/' . $row['id'] . '/1') ?>" 
                                                    class="btn <?= $row['is_active'] == 1 ? 'btn-success' : 'btn-light' ?> px-2 py-1" title="Terima / Aktifkan">
                                                    <i class="bi bi-check-circle"></i>
                                                 </a>
                                                 <a href="<?= base_url('admin/user/setStatus/' . $row['id'] . '/2') ?>" 
                                                    class="btn <?= $row['is_active'] == 2 ? 'btn-warning' : 'btn-light' ?> px-2 py-1" title="Pending / Tunda">
                                                    <i class="bi bi-clock-history"></i>
                                                 </a>
                                                 <a href="<?= base_url('admin/user/setStatus/' . $row['id'] . '/0') ?>" 
                                                    class="btn <?= $row['is_active'] === "0" || $row['is_active'] == 0 ? 'btn-danger' : 'btn-light' ?> px-2 py-1" title="Tolak / Nonaktifkan">
                                                    <i class="bi bi-x-circle"></i>
                                                 </a>
                                             </div>
                                         </div>
                                     </td>
                                    <td class="text-center">
                                        <div class="btn-group rounded-pill overflow-hidden shadow-sm">
                                            <a href="<?= base_url('admin/user/edit/' . $row['id']) ?>"
                                                class="btn btn-light btn-sm"><i
                                                    class="bi bi-pencil-square text-primary"></i></a>
                                            <a href="<?= base_url('admin/user/delete/' . $row['id']) ?>"
                                                class="btn btn-light btn-sm" onclick="return confirm('Hapus user ini?')"><i
                                                    class="bi bi-trash-fill text-danger"></i></a>
                                        </div>
                                    </td>
                                </tr>
                            <?php endforeach; ?>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
<?= $this->endSection() ?>