<?= view('admin/layouts/header', ['title' => 'Data Pelanggan']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'pelanggan',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Data Pelanggan',
    'pageSubtitle' => 'Kelola data pelanggan.',
    'topbarAction' => '<a href="' . site_url('admin/pelanggan/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah Pelanggan</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>No. HP</th>
                        <th>Alamat</th>
                        <th class="text-end">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($pelanggan as $item) : ?>
                        <?php $isUsed = ((int) ($item['total_penjualan'] ?? 0) + (int) ($item['total_service'] ?? 0)) > 0; ?>
                        <tr>
                            <td><span class="badge text-bg-light"><?= esc($item['kode_pelanggan'] ?? '-') ?></span></td>
                            <td>
                                <span class="fw-semibold"><?= esc($item['nama']) ?></span>
                            </td>
                            <td><?= esc($item['no_hp']) ?></td>
                            <td><?= esc($item['alamat']) ?></td>
                            <td class="text-end text-nowrap">
                                <a href="<?= site_url('admin/pelanggan/edit/' . $item['id']) ?>" class="btn btn-sm btn-primary btn-icon" title="Edit pelanggan" aria-label="Edit pelanggan">
                                    <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="Pelanggan ini tidak bisa dihapus karena sudah terhubung ke transaksi penjualan atau service.">
                                        <button type="button" class="btn btn-sm btn-danger btn-icon" disabled title="Hapus pelanggan" aria-label="Hapus pelanggan">
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </span>
                                <?php else : ?>
                                    <form action="<?= site_url('admin/pelanggan/delete/' . $item['id']) ?>" method="post" class="d-inline" data-confirm="Hapus pelanggan ini?">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-danger btn-icon" title="Hapus pelanggan" aria-label="Hapus pelanggan">
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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