<?= view('admin/layouts/header', ['title' => 'Data Kategori']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'kategori',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Data Kategori',
    'pageSubtitle' => 'Kelola kategori untuk pengelompokan data barang.',
    'topbarAction' => '<a href="' . site_url('admin/kategori/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah Kategori</a>',
    'userName'     => $userName,
    'userRole'     => $userRole,
]) ?>

<section class="content-area">
    <?php if (session()->getFlashdata('success')) : ?>
        <div class="alert alert-success"><?= esc(session()->getFlashdata('success')) ?></div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')) : ?>
        <div class="alert alert-danger"><?= esc(session()->getFlashdata('error')) ?></div>
    <?php endif; ?>

    <div class="panel p-4">
        <div class="table-responsive">
            <table class="table table-hover align-middle datatable">
                <thead>
                    <tr>
                        <th style="width: 90px;">Icon</th>
                        <th>Nama Kategori</th>
                        <th class="text-end">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($kategori as $item) : ?>
                        <?php $isUsedByBarang = (int) ($item['total_barang'] ?? 0) > 0; ?>
                        <tr>
                            <td>
                                <div class="icon-box">
                                    <i class="bi <?= esc($item['icon_kategori'], 'attr') ?>"></i>
                                </div>
                            </td>
                            <td class="fw-semibold"><?= esc($item['nama_kategori']) ?></td>
                            <td class="text-end text-nowrap">
                                <a href="<?= site_url('admin/kategori/edit/' . $item['id']) ?>" class="btn btn-sm btn-primary btn-icon" title="Edit kategori" aria-label="Edit kategori">
                                    <i class="bi bi-pencil-square"></i>
                                </a>
                                <?php if ($isUsedByBarang) : ?>
                                    <span tabindex="0" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="top" data-bs-content="Kategori ini tidak bisa dihapus karena sudah terhubung ke data produk/barang.">
                                        <button type="button" class="btn btn-sm btn-secondary btn-icon" aria-label="Kategori tidak dapat dihapus" disabled>
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </span>
                                <?php else : ?>
                                    <form action="<?= site_url('admin/kategori/delete/' . $item['id']) ?>" method="post" class="d-inline" data-confirm="Hapus kategori ini?">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-danger btn-icon" title="Hapus kategori" aria-label="Hapus kategori">
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

<?= view('admin/layouts/footer') ?>
