<?= view('admin/layouts/header', ['title' => 'Data User']) ?>
<?php $canManageUsers = ($userRole ?? '') === 'administrator'; ?>
<?= view('admin/layouts/sidebar', [
    'activeMenu' => 'users',
    'userName'   => $userName,
    'userEmail'  => $userEmail,
]) ?>
<?= view('admin/layouts/topbar', [
    'pageTitle'    => 'Data User',
    'pageSubtitle' => 'Kelola akun administrator dan kasir aplikasi.',
    'topbarAction' => $canManageUsers ? '<a href="' . site_url('admin/users/create') . '" class="btn btn-dark"><i class="bi bi-plus-lg me-1"></i>Tambah User</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>Foto</th>
                        <th>Nama</th>
                        <th>Email</th>
                        <th>Role</th>
                        <th>Status</th>
                        <?php if ($canManageUsers) : ?>
                            <th class="text-end">Aksi</th>
                        <?php endif; ?>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($users as $user) : ?>
                        <?php
                        $isCurrentUser = (int) session()->get('userId') === (int) $user['id'];
                        $isUsed = ((int) ($user['total_pembelian'] ?? 0) + (int) ($user['total_penjualan'] ?? 0) + (int) ($user['total_service'] ?? 0) + (int) ($user['total_stok_penyesuaian'] ?? 0)) > 0;
                        ?>
                        <tr>
                            <td>
                                <?php if (! empty($user['photo'])) : ?>
                                    <img src="<?= base_url('uploads/users/' . $user['photo']) ?>" alt="Foto <?= esc($user['name']) ?>" class="user-photo">
                                <?php else : ?>
                                    <div class="photo-placeholder"><?= esc(strtoupper(substr($user['name'], 0, 1))) ?></div>
                                <?php endif; ?>
                            </td>
                            <td class="fw-semibold"><?= esc($user['name']) ?></td>
                            <td><?= esc($user['email']) ?></td>
                            <td>
                                <?php if (($user['role'] ?? '') === 'administrator') : ?>
                                    <span class="badge text-bg-primary">Administrator</span>
                                <?php else : ?>
                                    <span class="badge text-bg-info">Kasir</span>
                                <?php endif; ?>
                            </td>
                            <td>
                                <?php if ((int) $user['is_active'] === 1) : ?>
                                    <span class="badge text-bg-success">Aktif</span>
                                <?php else : ?>
                                    <span class="badge text-bg-secondary">Nonaktif</span>
                                <?php endif; ?>
                            </td>
                            <?php if ($canManageUsers) : ?>
                                <td class="text-end text-nowrap">
                                    <a href="<?= site_url('admin/users/edit/' . $user['id']) ?>" class="btn btn-sm btn-primary btn-icon" title="Edit user" aria-label="Edit user">
                                        <i class="bi bi-pencil-square"></i>
                                    </a>
                                    <?php if ($isCurrentUser || $isUsed) : ?>
                                        <?php
                                        $message = $isCurrentUser
                                            ? 'Akun yang sedang login tidak boleh dihapus.'
                                            : 'User ini tidak bisa dihapus karena sudah terhubung ke transaksi pembelian, penjualan, service, atau penyesuaian stok.';
                                        ?>
                                        <span tabindex="0" data-bs-toggle="popover" data-bs-trigger="hover focus" data-bs-placement="top" data-bs-content="<?= esc($message, 'attr') ?>">
                                            <button type="button" class="btn btn-sm btn-secondary btn-icon" aria-label="User tidak dapat dihapus" disabled>
                                                <i class="bi bi-trash3"></i>
                                            </button>
                                        </span>
                                    <?php else : ?>
                                        <form action="<?= site_url('admin/users/delete/' . $user['id']) ?>" method="post" class="d-inline" data-confirm="Hapus user ini?">
                                            <?= csrf_field() ?>
                                            <button type="submit" class="btn btn-sm btn-danger btn-icon" title="Hapus user" aria-label="Hapus user">
                                                <i class="bi bi-trash3"></i>
                                            </button>
                                        </form>
                                    <?php endif; ?>
                                </td>
                            <?php endif; ?>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</section>

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