<?= view('admin/layouts/header', ['title' => 'Data Suplier']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'suplier',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Data Suplier',
    'pageSubtitle' => 'Kelola data pemasok barang.',
    'topbarAction' => '<a href="' . site_url('admin/suplier/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah Suplier</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>Kode</th>
                        <th>Nama</th>
                        <th>Kontak</th>                        
                        <th>Alamat</th>
                        <th class="text-end">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($suplier as $item) : ?>
                        <?php $isUsed = (int) ($item['total_pembelian'] ?? 0) > 0; ?>
                        <tr>
                            <td><span class="badge text-bg-light"><?= esc($item['kode_suplier'] ?? '-') ?></span></td>
                            <td>
                                <span class="fw-semibold"><?= esc($item['nama']) ?></span>                                
                                <span class="small d-block"><?= esc($item['perusahaan']) ?></span>
                            </td>
                            <td>
                               <span class="d-block"><?= esc($item['no_hp']) ?></span>
                               <span>
                                 <?php if (! empty($item['email'])) : ?>
                                    <a href="mailto:<?= esc($item['email'], 'attr') ?>" class="text-decoration-none"><?= esc($item['email']) ?></a>
                                <?php else : ?>
                                    <span class="text-secondary">-</span>
                                <?php endif; ?>
                               </span>
                            </td>
                            <td><?= esc($item['alamat']) ?></td>
                            <td class="text-end text-nowrap">
                                <a href="<?= site_url('admin/suplier/edit/' . $item['id']) ?>" class="btn btn-sm btn-primary btn-icon" title="Edit suplier" aria-label="Edit suplier">
                                    <i class="bi bi-pencil-square"></i>
                                </a>
                                <?php if ($isUsed) : ?>
                                    <span tabindex="0" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="top" data-bs-content="Suplier ini tidak bisa dihapus karena sudah terhubung ke transaksi pembelian.">
                                        <button type="button" class="btn btn-sm btn-secondary btn-icon" aria-label="Suplier tidak dapat dihapus" disabled>
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </span>
                                <?php else : ?>
                                    <form action="<?= site_url('admin/suplier/delete/' . $item['id']) ?>" method="post" class="d-inline" data-confirm="Hapus suplier ini?">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-danger btn-icon" title="Hapus suplier" aria-label="Hapus suplier">
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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