<?= $this->extend('layouts/app') ?>

<?= $this->section('content') ?>

<style>
    /* ===== MODERN SAAS DESIGN SYSTEM TOKENS ===== */
    :root {
        --bg-body: #f8fafc;
        --bg-card: #ffffff;
        --bg-accent: #f1f5f9;
        --bg-input: #ffffff;
        --border-color: #e2e8f0;
        --border-hover: #cbd5e1;
        --text-main: #0f172a;
        --text-sub: #64748b;
        --primary: #4f46e5;
        --primary-hover: #4338ca;
        --primary-light: rgba(79, 70, 229, 0.08);
        --success: #10b981;
        --warning: #f59e0b;
        --danger: #ef4444;
        --info: #06b6d4;
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.05);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.07), 0 2px 4px -2px rgba(0, 0, 0, 0.05);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -4px rgba(0, 0, 0, 0.05);
        --radius-lg: 16px;
        --radius-md: 12px;
        --radius-sm: 8px;
        --transition: all 0.2s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @media (prefers-color-scheme: dark) {
        :root {
            --bg-body: #0b0f19;
            --bg-card: #151e2e;
            --bg-accent: #1e293b;
            --bg-input: #0f172a;
            --border-color: #243044;
            --border-hover: #334155;
            --text-main: #f8fafc;
            --text-sub: #94a3b8;
            --primary-light: rgba(79, 70, 229, 0.18);
            --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
            --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
            --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
        }
    }

    [data-bs-theme="dark"] {
        --bg-body: #0b0f19;
        --bg-card: #151e2e;
        --bg-accent: #1e293b;
        --bg-input: #0f172a;
        --border-color: #243044;
        --border-hover: #334155;
        --text-main: #f8fafc;
        --text-sub: #94a3b8;
        --primary-light: rgba(79, 70, 229, 0.18);
        --shadow-sm: 0 1px 2px 0 rgba(0, 0, 0, 0.3);
        --shadow-md: 0 4px 6px -1px rgba(0, 0, 0, 0.4);
        --shadow-lg: 0 10px 15px -3px rgba(0, 0, 0, 0.5);
    }

    body, .content-wrapper, .main-content, .content, .page-content, main, .user-management-workspace {
        background-color: var(--bg-body) !important;
        color: var(--text-main) !important;
        transition: var(--transition);
    }

    /* Card Premium Styling */
    .card-modern {
        background: var(--bg-card);
        border: 1px solid var(--border-color);
        border-radius: var(--radius-lg);
        padding: 1.5rem;
        box-shadow: var(--shadow-sm);
        transition: var(--transition);
    }

    .card-modern:hover {
        border-color: var(--border-hover);
    }

    /* Avatar Initial Circle */
    .avatar-circle {
        width: 38px;
        height: 38px;
        border-radius: 50%;
        background: var(--primary-light);
        color: var(--primary);
        font-weight: 700;
        font-size: 0.9rem;
        display: flex;
        align-items: center;
        justify-content: center;
        flex-shrink: 0;
    }

    /* Code Pill for Username / Identifiers */
    .code-pill {
        background: var(--bg-accent);
        border: 1px solid var(--border-color);
        color: var(--text-main);
        font-family: var(--bs-font-monospace);
        font-size: 0.82rem;
        font-weight: 600;
        padding: 0.2rem 0.55rem;
        border-radius: var(--radius-sm);
    }

    /* Soft Badges */
    .badge-soft-success   { background: rgba(16, 185, 129, 0.12); color: #10b981; border: 1px solid rgba(16, 185, 129, 0.2); }
    .badge-soft-warning   { background: rgba(245, 158, 11, 0.12); color: #f59e0b; border: 1px solid rgba(245, 158, 11, 0.2); }
    .badge-soft-danger    { background: rgba(239, 68, 68, 0.12); color: #ef4444; border: 1px solid rgba(239, 68, 68, 0.2); }
    .badge-soft-primary   { background: rgba(79, 70, 229, 0.12); color: #4f46e5; border: 1px solid rgba(79, 70, 229, 0.2); }
    .badge-soft-info      { background: rgba(6, 182, 212, 0.12); color: #06b6d4; border: 1px solid rgba(6, 182, 212, 0.2); }
    .badge-soft-secondary { background: rgba(100, 116, 139, 0.12); color: #64748b; border: 1px solid rgba(100, 116, 139, 0.2); }

    /* Custom Table */
    .custom-table {
        margin-bottom: 0;
        width: 100%;
        color: var(--text-main);
    }

    .custom-table thead th {
        font-size: 0.72rem;
        text-transform: uppercase;
        letter-spacing: 0.08em;
        font-weight: 700;
        color: var(--text-sub);
        padding: 0.85rem 0.75rem;
        border-bottom: 1px solid var(--border-color);
        background: var(--bg-accent);
    }

    .custom-table tbody tr {
        border-bottom: 1px solid var(--border-color);
        transition: var(--transition);
    }

    .custom-table tbody tr:last-child {
        border-bottom: none;
    }

    .custom-table tbody tr:hover {
        background-color: var(--bg-accent) !important;
    }

    .custom-table td {
        padding: 0.85rem 0.75rem !important;
        vertical-align: middle;
    }

    /* Action Buttons */
    .btn-action-icon {
        width: 32px;
        height: 32px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: var(--radius-sm);
        transition: var(--transition);
        font-size: 0.85rem;
    }

    .search-input-box {
        background: var(--bg-input);
        border: 1px solid var(--border-color);
        color: var(--text-main);
        border-radius: var(--radius-md);
        padding: 0.5rem 0.85rem;
        font-size: 0.88rem;
        transition: var(--transition);
    }

    .search-input-box:focus {
        border-color: var(--primary);
        box-shadow: 0 0 0 3px var(--primary-light);
        outline: none;
        color: var(--text-main);
        background: var(--bg-input);
    }
</style>

<div class="container-fluid py-4 user-management-workspace">
    
    <!-- PAGE HEADER -->
    <div class="d-flex flex-column flex-sm-row justify-content-between align-items-sm-center mb-4 gap-3">
        <div>
            <h3 class="fw-bold mb-0 text-main">Manajemen User</h3>
            <p class="text-sub small mb-0 mt-0.5">Kelola akun pengguna, hak akses role, serta hak otorisasi sistem secara terpusat.</p>
        </div>
        <div class="d-flex align-items-center gap-2">
            <a href="<?= base_url('superadmin/user/create') ?>" class="btn btn-primary px-3.5 py-2 rounded-3 shadow-sm fw-semibold d-inline-flex align-items-center gap-2 fs-7">
                <i class="bi bi-person-plus-fill"></i> Tambah User Baru
            </a>
        </div>
    </div>

    <!-- FLASH MESSAGES -->
    <?php if (session()->getFlashdata('success')) : ?>
        <div class="alert alert-success alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
            <div class="d-flex align-items-center gap-2">
                <i class="bi bi-check-circle-fill fs-5"></i>
                <div class="fw-medium"><?= session()->getFlashdata('success') ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <?php if (session()->getFlashdata('error')) : ?>
        <div class="alert alert-danger alert-dismissible fade show rounded-3 shadow-sm border-0 mb-4" role="alert">
            <div class="d-flex align-items-center gap-2">
                <i class="bi bi-exclamation-triangle-fill fs-5"></i>
                <div class="fw-medium"><?= session()->getFlashdata('error') ?></div>
            </div>
            <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
        </div>
    <?php endif; ?>

    <!-- TABLE CARD -->
    <div class="card-modern">
        
        <!-- TOOLBAR: SEARCH FILTER & TOTAL COUNTER -->
        <div class="d-flex flex-column flex-sm-row justify-content-between align-items-sm-center mb-3 gap-2 pb-3 border-bottom border-color">
            <div class="d-flex align-items-center gap-2">
                <span class="badge badge-soft-primary rounded-pill px-3 py-1.5 fw-semibold fs-7">
                    Total: <?= count($users ?? []) ?> User
                </span>
            </div>
            <div class="position-relative" style="min-width: 260px;">
                <i class="bi bi-search position-absolute top-50 start-0 translate-middle-y ms-3 text-sub"></i>
                <input type="text" id="userSearchInput" class="form-control search-input-box ps-5" placeholder="Cari username, nama, email...">
            </div>
        </div>

        <!-- TABLE WORKSPACE -->
        <div class="table-responsive">
            <table class="table custom-table align-middle" id="userTable">
                <thead>
                    <tr>
                        <th style="width: 50px;">No</th>
                        <th>Nama Lengkap</th>
                        <th>Username</th>
                        <th>Email</th>
                        <th>Role</th>
                        <th class="text-center">Status</th>
                        <th style="width: 220px;" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($users)): ?>
                        <tr>
                            <td colspan="7" class="text-center py-5 text-sub">
                                <i class="bi bi-people fs-2 d-block opacity-40 mb-2"></i>
                                Tidak ada data pengguna ditemukan dalam sistem.
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $no = 1; foreach ($users as $user): ?>
                            <?php 
                                $namaLengkap = $user['nama_lengkap'] ?? 'Tanpa Nama';
                                $initial = strtoupper(substr(trim($namaLengkap), 0, 1));
                                $roleName = strtolower($user['nama_role'] ?? 'user');
                                
                                // Role Badge Color Resolver
                                $roleBadgeClass = 'badge-soft-info';
                                if (str_contains($roleName, 'admin') || str_contains($roleName, 'super')) {
                                    $roleBadgeClass = 'badge-soft-primary';
                                } elseif (str_contains($roleName, 'staff') || str_contains($roleName, 'operator')) {
                                    $roleBadgeClass = 'badge-soft-warning';
                                }
                            ?>
                            <tr class="user-row">
                                <!-- No -->
                                <td class="fw-semibold text-sub text-center" style="font-size: 0.85rem;"><?= $no++ ?></td>

                                <!-- Nama Lengkap with Avatar -->
                                <td>
                                    <div class="d-flex align-items-center gap-2.5">
                                        <div class="avatar-circle"><?= $initial ?></div>
                                        <div>
                                            <div class="fw-bold text-main" style="font-size: 0.9rem;"><?= esc($namaLengkap) ?></div>
                                            <small class="text-sub font-monospace" style="font-size: 0.75rem;">ID: #<?= esc($user['id_user']) ?></small>
                                        </div>
                                    </div>
                                </td>

                                <!-- Username -->
                                <td>
                                    <span class="code-pill text-primary">
                                        <i class="bi bi-at"></i><?= esc($user['username']) ?>
                                    </span>
                                </td>

                                <!-- Email -->
                                <td>
                                    <span class="text-main font-monospace small">
                                        <?= esc($user['email']) ?>
                                    </span>
                                </td>

                                <!-- Role -->
                                <td>
                                    <span class="badge <?= $roleBadgeClass ?> rounded-pill px-2.5 py-1">
                                        <?= esc($user['nama_role']) ?>
                                    </span>
                                </td>

                                <!-- Status -->
                                <td class="text-center">
                                    <?php if ($user['is_active']): ?>
                                        <span class="badge badge-soft-success rounded-pill px-2.5 py-1 fw-semibold d-inline-flex align-items-center gap-1">
                                            <i class="bi bi-check-circle-fill"></i> Aktif
                                        </span>
                                    <?php else: ?>
                                        <span class="badge badge-soft-danger rounded-pill px-2.5 py-1 fw-semibold d-inline-flex align-items-center gap-1">
                                            <i class="bi bi-x-circle-fill"></i> Nonaktif
                                        </span>
                                    <?php endif; ?>
                                </td>

                                <!-- Aksi Buttons -->
                                <td class="text-center">
                                    <div class="d-inline-flex align-items-center gap-1.5">
                                        
                                        <!-- Detail -->
                                        <a href="<?= base_url('superadmin/user/detail/' . $user['id_user']) ?>" 
                                           class="btn btn-outline-info btn-action-icon" 
                                           data-bs-toggle="tooltip" 
                                           data-bs-title="Detail User">
                                            <i class="bi bi-eye"></i>
                                        </a>

                                        <!-- Edit -->
                                        <a href="<?= base_url('superadmin/user/edit/' . $user['id_user']) ?>" 
                                           class="btn btn-outline-warning btn-action-icon" 
                                           data-bs-toggle="tooltip" 
                                           data-bs-title="Edit User">
                                            <i class="bi bi-pencil-square"></i>
                                        </a>

                                        <!-- Reset Password -->
                                        <button type="button" 
                                                class="btn btn-outline-secondary btn-action-icon" 
                                                onclick="confirmReset(<?= $user['id_user'] ?>)" 
                                                data-bs-toggle="tooltip" 
                                                data-bs-title="Reset Password">
                                            <i class="bi bi-key-fill"></i>
                                        </button>

                                        <!-- Toggle Status (Activate / Deactivate) -->
                                        <?php if ($user['is_active']): ?>
                                            <a href="<?= base_url('superadmin/user/deactivate/' . $user['id_user']) ?>" 
                                               class="btn btn-outline-danger btn-action-icon" 
                                               onclick="return confirm('Apakah Anda yakin ingin menonaktifkan akun ini?')" 
                                               data-bs-toggle="tooltip" 
                                               data-bs-title="Nonaktifkan Akun">
                                                <i class="bi bi-person-x-fill"></i>
                                            </a>
                                        <?php else: ?>
                                            <a href="<?= base_url('superadmin/user/activate/' . $user['id_user']) ?>" 
                                               class="btn btn-outline-success btn-action-icon" 
                                               onclick="return confirm('Apakah Anda yakin ingin mengaktifkan akun ini kembali?')" 
                                               data-bs-toggle="tooltip" 
                                               data-bs-title="Aktifkan Akun">
                                                <i class="bi bi-person-check-fill"></i>
                                            </a>
                                        <?php endif; ?>

                                        <!-- Delete -->
                                        <a href="<?= base_url('superadmin/user/delete/' . $user['id_user']) ?>" 
                                           class="btn btn-outline-danger btn-action-icon" 
                                           onclick="return confirm('Apakah Anda yakin ingin menghapus permanen data user ini?')" 
                                           data-bs-toggle="tooltip" 
                                           data-bs-title="Hapus Permanen">
                                            <i class="bi bi-trash-fill"></i>
                                        </a>

                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

    </div>
</div>

<!-- JAVASCRIPT & INTERACTION LOGIC -->
<script>
    // Confirm Password Reset Logic
    function confirmReset(id) {
        if (confirm('Apakah Anda yakin ingin mereset password user ini ke default (12345678)?')) {
            window.location.href = "<?= base_url('superadmin/user/reset-password/') ?>/" + id;
        }
    }

    // Live Client-side Table Search Filter
    document.addEventListener('DOMContentLoaded', function () {
        // Initialize Tooltips
        const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]');
        const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl));

        // Search Input Filter
        const searchInput = document.getElementById('userSearchInput');
        if (searchInput) {
            searchInput.addEventListener('keyup', function () {
                const filter = this.value.toLowerCase();
                const rows = document.querySelectorAll('#userTable tbody tr.user-row');

                rows.forEach(row => {
                    const text = row.textContent.toLowerCase();
                    if (text.includes(filter)) {
                        row.style.display = '';
                    } else {
                        row.style.display = 'none';
                    }
                });
            });
        }
    });
</script>

<?= $this->endSection() ?>