<?= $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, .loginlog-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;
    }

    /* IP Badge Pill */
    .ip-badge {
        background-color: var(--bg-accent);
        border: 1px solid var(--border-color);
        color: var(--text-main);
        font-family: var(--bs-font-monospace);
        font-size: 0.8rem;
        padding: 0.25rem 0.6rem;
        border-radius: var(--radius-sm);
        display: inline-flex;
        align-items: center;
        gap: 0.35rem;
    }

    /* 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);
    }

    /* Pulse Dot Active Session */
    .pulse-dot {
        width: 8px;
        height: 8px;
        background-color: var(--success);
        border-radius: 50%;
        display: inline-block;
        box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
        animation: pulse 1.6s infinite;
    }

    @keyframes pulse {
        0% {
            transform: scale(0.95);
            box-shadow: 0 0 0 0 rgba(16, 185, 129, 0.7);
        }
        70% {
            transform: scale(1);
            box-shadow: 0 0 0 6px rgba(16, 185, 129, 0);
        }
        100% {
            transform: scale(0.95);
            box-shadow: 0 0 0 0 rgba(16, 185, 129, 0);
        }
    }
</style>

<div class="container-fluid py-4 loginlog-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">Login Log</h3>
            <p class="text-sub small mb-0 mt-0.5">Pemantauan riwayat otentikasi, durasi sesi, dan alamat IP pengguna.</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: 60px;" class="text-center">ID</th>
                        <th>Pengguna</th>
                        <th style="width: 130px;">Role</th>
                        <th>Waktu Login</th>
                        <th>Waktu Logout</th>
                        <th>Alamat IP</th>
                        <th>Perangkat / Browser</th>
                        <th style="width: 80px;" class="text-center">Aksi</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($logs)) : ?>
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <div class="py-4">
                                    <i class="bi bi-shield-check fs-1 text-sub opacity-50 d-block mb-2"></i>
                                    <h6 class="fw-semibold text-main mb-1">Tidak Ada Data Login</h6>
                                    <p class="text-sub small mb-0">Belum ada riwayat masuk sistem yang tercatat pada basis data.</p>
                                </div>
                            </td>
                        </tr>
                    <?php else : ?>
                        <?php foreach ($logs as $log) : ?>
                            <?php 
                                // User Identity Logic
                                $namaLengkap = $log['nama_lengkap'] ?? $log['username'] ?? 'User';
                                $initials = strtoupper(substr(trim($namaLengkap), 0, 1));

                                // Role Badge 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';
                                }

                                // Session Active / Logout Logic
                                $logoutAt = $log['logout_at'] ?? null;
                                $isSessionActive = empty($logoutAt);
                            ?>
                            <tr>
                                <td class="text-center text-sub font-monospace fs-7">#<?= esc($log['id_login']) ?></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($namaLengkap) ?>
                                            </span>
                                            <span class="text-sub fs-7">
                                                @<?= esc($log['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-1.5 text-main small fw-medium">
                                        <i class="bi bi-box-arrow-in-right text-success"></i>
                                        <span><?= esc($log['login_at'] ?? '-') ?></span>
                                    </div>
                                </td>
                                <td>
                                    <?php if ($isSessionActive) : ?>
                                        <span class="badge bg-success-subtle text-success border border-success-subtle rounded-pill px-2.5 py-1 d-inline-flex align-items-center gap-1.5">
                                            <span class="pulse-dot"></span>
                                            Sesi Aktif
                                        </span>
                                    <?php else : ?>
                                        <div class="d-flex align-items-center gap-1.5 text-sub small">
                                            <i class="bi bi-box-arrow-right opacity-75"></i>
                                            <span><?= esc($logoutAt) ?></span>
                                        </div>
                                    <?php endif; ?>
                                </td>
                                <td>
                                    <div class="ip-badge">
                                        <i class="bi bi-pc-display text-sub fs-7"></i>
                                        <span><?= esc($log['ip_address'] ?? '-') ?></span>
                                    </div>
                                </td>
                                <td>
                                    <div class="d-flex align-items-center gap-1.5 text-sub small">
                                        <i class="bi bi-globe fs-7"></i>
                                        <span class="text-truncate" style="max-width: 180px;" title="<?= esc($log['browser'] ?? '-') ?>">
                                            <?= esc($log['browser'] ?? '-') ?>
                                        </span>
                                    </div>
                                </td>
                                <td class="text-center">
                                    <a href="<?= base_url('superadmin/loginlog/detail/' . $log['id_login']) ?>" 
                                       class="btn-action" 
                                       title="Lihat Detail Sesi"
                                       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() ?>