<?= $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);
        --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);
        }
    }

    [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);
    }

    body, .content-wrapper, .main-content, .content, .page-content, main, .activity-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);
        box-shadow: var(--shadow-sm);
        transition: var(--transition);
        overflow: hidden;
    }

    /* Modern Table Styling */
    .table-modern {
        margin-bottom: 0;
        color: var(--text-main);
    }

    .table-modern th {
        background-color: var(--bg-accent);
        color: var(--text-sub);
        font-weight: 600;
        font-size: 0.75rem;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        padding: 1rem 1.25rem;
        border-bottom: 1px solid var(--border-color);
    }

    .table-modern td {
        padding: 1rem 1.25rem;
        border-bottom: 1px solid var(--border-color);
        background-color: transparent;
        vertical-align: middle;
        font-size: 0.875rem;
    }

    .table-modern tbody tr {
        transition: var(--transition);
    }

    .table-modern tbody tr:hover {
        background-color: var(--primary-light) !important;
    }

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

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

    /* Action Buttons */
    .btn-action {
        width: 32px;
        height: 32px;
        padding: 0;
        display: inline-flex;
        align-items: center;
        justify-content: center;
        border-radius: var(--radius-sm);
        color: var(--text-sub);
        border: 1px solid var(--border-color);
        background-color: var(--bg-card);
        transition: var(--transition);
    }

    .btn-action:hover {
        background-color: var(--primary);
        color: #ffffff;
        border-color: var(--primary);
    }
</style>

<div class="container-fluid py-4 activity-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">Activity Log</h3>
            <p class="text-sub small mb-0 mt-0.5">Rekam jejak audit serta riwayat aktivitas pengguna dalam sistem.</p>
        </div>
        <div class="d-flex align-items-center gap-2">
            <button onclick="window.location.reload()" class="btn btn-outline-secondary rounded-3 px-3 py-2 fw-semibold d-inline-flex align-items-center gap-2 fs-7">
                <i class="bi bi-arrow-clockwise"></i> Refresh
            </button>
        </div>
    </div>

    <!-- MAIN TABLE CARD -->
    <div class="card-modern">
        <div class="table-responsive">
            <table class="table table-modern align-middle">
                <thead>
                    <tr>
                        <th style="width: 50px;" class="text-center">No</th>
                        <th style="width: 180px;">Waktu</th>
                        <th>User</th>
                        <th style="width: 140px;">Role</th>
                        <th>Aktivitas</th>
                        <th style="width: 80px;" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($logs)) : ?>
                        <tr>
                            <td colspan="6" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-journal-x fs-1 text-sub opacity-50 d-block mb-2"></i>
                                    <h6 class="fw-semibold text-main mb-1">Belum Ada Catatan Aktivitas</h6>
                                    <p class="text-sub small mb-0">Semua aktivitas sistem akan dicatat dan ditampilkan secara otomatis di sini.</p>
                                </div>
                            </td>
                        </tr>
                    <?php else : ?>
                        <?php $no = 1; foreach ($logs as $log) : ?>
                            <?php 
                                // Inisial Avatar User
                                $userName = $log['nama_lengkap'] ?? 'System';
                                $initials = strtoupper(substr($userName, 0, 1));

                                // Badge Role Color logic
                                $roleName = $log['nama_role'] ?? '-';
                                $roleBadgeClass = 'bg-secondary-subtle text-secondary border-secondary-subtle';
                                if (stripos($roleName, 'superadmin') !== false) {
                                    $roleBadgeClass = 'bg-danger-subtle text-danger border-danger-subtle';
                                } elseif (stripos($roleName, 'admin') !== false) {
                                    $roleBadgeClass = 'bg-primary-subtle text-primary border-primary-subtle';
                                } elseif (stripos($roleName, 'user') !== false) {
                                    $roleBadgeClass = 'bg-info-subtle text-info border-info-subtle';
                                }

                                // Activity highlight logic
                                $activity = $log['aktivitas'] ?? '-';
                                $activityIcon = 'bi-activity';
                                if (stripos($activity, 'login') !== false) {
                                    $activityIcon = 'bi-box-arrow-in-right text-success';
                                } elseif (stripos($activity, 'tambah') !== false || stripos($activity, 'simpan') !== false || stripos($activity, 'create') !== false) {
                                    $activityIcon = 'bi-plus-circle text-primary';
                                } elseif (stripos($activity, 'edit') !== false || stripos($activity, 'update') !== false) {
                                    $activityIcon = 'bi-pencil-square text-warning';
                                } elseif (stripos($activity, 'hapus') !== false || stripos($activity, 'delete') !== false) {
                                    $activityIcon = 'bi-trash text-danger';
                                }
                            ?>
                            <tr>
                                <td class="text-center text-sub fw-medium"><?= $no++ ?></td>
                                <td>
                                    <div class="d-flex align-items-center gap-1.5 text-sub small">
                                        <i class="bi bi-clock fs-7"></i>
                                        <span><?= esc($log['timestamp'] ?? '-') ?></span>
                                    </div>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center gap-2.5">
                                        <div class="user-avatar">
                                            <?= $initials ?>
                                        </div>
                                        <div>
                                            <span class="fw-semibold text-main d-block leading-tight">
                                                <?= esc($userName) ?>
                                            </span>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="badge <?= $roleBadgeClass ?> border px-2.5 py-1 rounded-pill fw-medium">
                                        <?= esc($roleName) ?>
                                    </span>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center gap-2">
                                        <i class="bi <?= $activityIcon ?> fs-6"></i>
                                        <span class="text-main fw-normal"><?= esc($activity) ?></span>
                                    </div>
                                </td>
                                <td class="text-center">
                                    <a href="<?= base_url('superadmin/activitylog/detail/' . $log['id_log']) ?>" 
                                       class="btn-action" 
                                       title="Lihat Detail Log"
                                       data-bs-toggle="tooltip" 
                                       data-bs-placement="top">
                                        <i class="bi bi-eye"></i>
                                    </a>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>

</div>

<!-- TOOLTIP INITIALIZATION JS -->
<script>
    document.addEventListener('DOMContentLoaded', function () {
        var tooltipTriggerList = [].slice.call(document.querySelectorAll('[data-bs-toggle="tooltip"]'));
        var tooltipList = tooltipTriggerList.map(function (tooltipTriggerEl) {
            return new bootstrap.Tooltip(tooltipTriggerEl);
        });
    });
</script>

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