<?= view('admin/layouts/header', ['title' => 'Data Teknisi']) ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'teknisi',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Data Teknisi',
    'pageSubtitle' => 'Kelola data teknisi.',
    'topbarAction' => '<a href="' . site_url('admin/teknisi/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah Teknisi</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>Teknisi</th>
                        <th>No. HP</th>
                        <th>Alamat</th>
                        <th class="text-end">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($teknisi as $item) : ?>
                        <?php $isUsed = (int) ($item['total_service'] ?? 0) > 0; ?>
                        <tr>
                            <td><span class="badge text-bg-light"><?= esc($item['kode_teknisi'] ?? '-') ?></span></td>
                            <td>
                                <div class="d-flex align-items-center gap-3">
                                    <?php if (! empty($item['foto'])) : ?>
                                        <img src="<?= base_url('uploads/teknisi/' . $item['foto']) ?>" alt="Foto <?= esc($item['nama']) ?>" class="sidebar-user-photo" style="width: 44px; height: 44px;">
                                    <?php else : ?>
                                        <div class="sidebar-user-photo-placeholder avatar" style="width: 44px; height: 44px;"><?= esc(strtoupper(substr((string) $item['nama'], 0, 1))) ?></div>
                                    <?php endif; ?>
                                    <span class="fw-semibold"><?= esc($item['nama']) ?></span>
                                </div>
                            </td>
                            <td><?= esc($item['no_hp']) ?></td>
                            <td><?= esc($item['alamat']) ?></td>
                            <td class="text-end text-nowrap">
                                <a href="<?= site_url('admin/teknisi/edit/' . $item['id']) ?>" class="btn btn-sm btn-primary btn-icon" title="Edit teknisi" aria-label="Edit teknisi">
                                    <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="Teknisi ini tidak bisa dihapus karena sudah terhubung ke transaksi service.">
                                        <button type="button" class="btn btn-sm btn-secondary btn-icon" aria-label="Teknisi tidak dapat dihapus" disabled>
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </span>
                                <?php else : ?>
                                    <form action="<?= site_url('admin/teknisi/delete/' . $item['id']) ?>" method="post" class="d-inline" data-confirm="Hapus teknisi ini?">
                                        <?= csrf_field() ?>
                                        <button type="submit" class="btn btn-sm btn-danger btn-icon" title="Hapus teknisi" aria-label="Hapus teknisi">
                                            <i class="bi bi-trash3"></i>
                                        </button>
                                    </form>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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