<?php
require_once __DIR__ . '/../includes/auth.php';
require_once __DIR__ . '/../includes/functions.php';
requireRole('super_admin');
$pageTitle = 'Activity Logs';

$db = db();

// Filters
$search = trim($_GET['search'] ?? '');
$filterAction = $_GET['action'] ?? '';
$filterUser = $_GET['user'] ?? '';
$filterDate = $_GET['date'] ?? '';

// Build query
$sql = "SELECT l.*, u.username, u.name as user_fullname 
        FROM activity_logs l 
        LEFT JOIN users u ON l.user_id = u.id 
        WHERE 1=1";
$params = [];

if (!empty($search)) {
    $sql .= " AND (l.description LIKE ? OR l.action LIKE ? OR u.username LIKE ?)";
    $params[] = "%$search%";
    $params[] = "%$search%";
    $params[] = "%$search%";
}
if (!empty($filterAction)) {
    $sql .= " AND l.action = ?";
    $params[] = $filterAction;
}
if (!empty($filterUser)) {
    $sql .= " AND l.user_id = ?";
    $params[] = $filterUser;
}
if (!empty($filterDate)) {
    $sql .= " AND DATE(l.created_at) = ?";
    $params[] = $filterDate;
}

$sql .= " ORDER BY l.created_at DESC LIMIT 500";

$stmt = $db->prepare($sql);
$stmt->execute($params);
$logs = $stmt->fetchAll();

// Stats
$totalLogs = count($logs);
$todayLogs = 0;
$today = date('Y-m-d');
$uniqueUsers = [];
$uniqueActions = [];
foreach ($logs as $l) {
    if (date('Y-m-d', strtotime($l['created_at'])) === $today) $todayLogs++;
    if (!empty($l['user_id'])) $uniqueUsers[$l['user_id']] = true;
    $uniqueActions[$l['action']] = true;
}

// Get unique users for filter
$usersList = $db->query("SELECT id, username, name FROM users ORDER BY username")->fetchAll();

// Get unique actions for filter
$actionsList = [];
$allActions = $db->query("SELECT DISTINCT action FROM activity_logs ORDER BY action")->fetchAll();
foreach ($allActions as $a) $actionsList[] = $a['action'];

// Helper: action icon & color
function getActionInfo($action) {
    $action = strtolower($action);
    $map = [
        'login' => ['bi-box-arrow-in-right', 'success', '🔐'],
        'logout' => ['bi-box-arrow-right', 'secondary', '🚪'],
        'create' => ['bi-plus-circle', 'primary', '➕'],
        'add' => ['bi-plus-circle', 'primary', '➕'],
        'update' => ['bi-pencil', 'warning', '✏️'],
        'edit' => ['bi-pencil', 'warning', '✏️'],
        'delete' => ['bi-trash', 'danger', '🗑️'],
        'remove' => ['bi-trash', 'danger', '🗑️'],
        'approve' => ['bi-check-circle', 'success', '✅'],
        'reject' => ['bi-x-circle', 'danger', '❌'],
        'suspend' => ['bi-pause-circle', 'warning', '⏸️'],
        'restore' => ['bi-play-circle', 'success', '▶️'],
        'kick' => ['bi-lightning', 'warning', '⚡'],
        'broadcast' => ['bi-megaphone', 'info', '📢'],
        'send' => ['bi-send', 'info', '📤'],
        'payment' => ['bi-cash-stack', 'success', '💰'],
        'withdraw' => ['bi-cash', 'warning', '💸'],
    ];
    
    foreach ($map as $key => $val) {
        if (strpos($action, $key) !== false) return $val;
    }
    return ['bi-circle', 'secondary', '📝'];
}

// Helper: relative time
function timeAgo($datetime) {
    $now = new DateTime();
    $past = new DateTime($datetime);
    $diff = $now->diff($past);
    
    if ($diff->y > 0) return $diff->y . ' tahun lalu';
    if ($diff->m > 0) return $diff->m . ' bulan lalu';
    if ($diff->d > 0) return $diff->d . ' hari lalu';
    if ($diff->h > 0) return $diff->h . ' jam lalu';
    if ($diff->i > 0) return $diff->i . ' menit lalu';
    return 'Baru saja';
}

require __DIR__ . '/../includes/header.php';
require __DIR__ . '/../includes/sidebar.php';
?>

<style>
/* === NAVBAR MODERN === */
.navbar-modern {
    background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%);
    padding: 15px 20px;
    display: flex;
    align-items: center;
    justify-content: space-between;
    box-shadow: 0 2px 10px rgba(0,0,0,0.1);
    position: sticky;
    top: 0;
    z-index: 1000;
}

.navbar-left {
    display: flex;
    align-items: center;
    gap: 15px;
}

.burger-btn {
    background: rgba(255,255,255,0.15);
    border: none;
    color: white;
    width: 40px;
    height: 40px;
    border-radius: 10px;
    cursor: pointer;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 1.3rem;
    transition: all 0.3s ease;
}

.burger-btn:hover {
    background: rgba(255,255,255,0.25);
    transform: scale(1.05);
}

.navbar-title {
    color: white;
    font-size: 1.2rem;
    font-weight: 600;
    margin: 0;
}

.navbar-subtitle {
    color: rgba(255,255,255,0.85);
    font-size: 0.8rem;
    margin: 0;
}

.navbar-right {
    display: flex;
    align-items: center;
    gap: 10px;
}

.navbar-badge {
    background: rgba(255,255,255,0.2);
    color: white;
    padding: 6px 12px;
    border-radius: 20px;
    font-size: 0.85rem;
    font-weight: 500;
    display: inline-flex;
    align-items: center;
    gap: 6px;
}

/* === MAIN CONTENT === */
.main-content {
    padding: 20px;
    max-width: 1400px;
    margin: 0 auto;
}

.page-header {
    margin-bottom: 25px;
}

.page-title {
    font-size: 1.8rem;
    font-weight: 700;
    color: #003D7A;
    margin: 0 0 8px;
    display: flex;
    align-items: center;
    gap: 12px;
}

.page-title i {
    font-size: 2rem;
    color: #0066CC;
}

.page-subtitle {
    color: #666;
    font-size: 0.95rem;
    margin: 0;
}

/* === SUMMARY CARDS === */
.summary-row {
    display: grid;
    grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
    gap: 16px;
    margin-bottom: 25px;
}

.summary-card {
    background: white;
    border-radius: 16px;
    padding: 20px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    position: relative;
    overflow: hidden;
    transition: all 0.3s ease;
}

.summary-card:hover {
    transform: translateY(-3px);
    box-shadow: 0 8px 25px rgba(0,0,0,0.12);
}

.summary-card::before {
    content: '';
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    height: 4px;
}

.summary-card.primary::before { background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%); }
.summary-card.success::before { background: linear-gradient(135deg, #00C853 0%, #69F0AE 100%); }
.summary-card.warning::before { background: linear-gradient(135deg, #FF9900 0%, #FFB84D 100%); }
.summary-card.info::before { background: linear-gradient(135deg, #00B8D4 0%, #26C6DA 100%); }

.summary-card-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 12px;
}

.summary-card-title {
    color: #666;
    font-size: 0.85rem;
    font-weight: 600;
    margin: 0;
    display: flex;
    align-items: center;
    gap: 6px;
}

.summary-card-icon {
    width: 40px;
    height: 40px;
    border-radius: 12px;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 1.2rem;
}

.summary-card.primary .summary-card-icon { background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%); }
.summary-card.success .summary-card-icon { background: linear-gradient(135deg, #00C853 0%, #69F0AE 100%); }
.summary-card.warning .summary-card-icon { background: linear-gradient(135deg, #FF9900 0%, #FFB84D 100%); }
.summary-card.info .summary-card-icon { background: linear-gradient(135deg, #00B8D4 0%, #26C6DA 100%); }

.summary-card-value {
    font-size: 1.8rem;
    font-weight: 800;
    margin-bottom: 4px;
    letter-spacing: -0.5px;
}

.summary-card.primary .summary-card-value { color: #0066CC; }
.summary-card.success .summary-card-value { color: #00C853; }
.summary-card.warning .summary-card-value { color: #FF9900; }
.summary-card.info .summary-card-value { color: #00B8D4; }

.summary-card-footer {
    font-size: 0.8rem;
    color: #999;
}

/* === CARD MODERN === */
.card-modern {
    background: white;
    border-radius: 16px;
    box-shadow: 0 4px 20px rgba(0,0,0,0.08);
    overflow: hidden;
    margin-bottom: 20px;
    border: none;
}

.card-header-modern {
    color: white;
    padding: 18px 24px;
    font-size: 1.1rem;
    font-weight: 600;
    display: flex;
    align-items: center;
    justify-content: space-between;
    gap: 10px;
}

.card-header-modern.primary { background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%); }
.card-header-modern.info { background: linear-gradient(135deg, #00B8D4 0%, #26C6DA 100%); }

.card-header-modern .title-left {
    display: flex;
    align-items: center;
    gap: 10px;
}

.card-body-modern {
    padding: 24px;
}

/* === FILTER FORM === */
.filter-form {
    display: grid;
    grid-template-columns: 2fr 1fr 1fr auto;
    gap: 12px;
    align-items: end;
}

.form-group-modern {
    margin-bottom: 0;
}

.form-label-modern {
    display: block;
    font-weight: 600;
    color: #003D7A;
    margin-bottom: 6px;
    font-size: 0.85rem;
}

.form-control-modern,
.form-select-modern {
    width: 100%;
    padding: 10px 14px;
    border: 2px solid #e8f4f8;
    border-radius: 10px;
    font-size: 0.9rem;
    transition: all 0.3s ease;
    background: #f8fafc;
    font-family: inherit;
}

.form-control-modern:focus,
.form-select-modern:focus {
    outline: none;
    border-color: #0066CC;
    background: white;
    box-shadow: 0 0 0 4px rgba(0, 102, 204, 0.1);
}

.btn-modern {
    padding: 10px 18px;
    border: none;
    border-radius: 10px;
    font-size: 0.9rem;
    font-weight: 600;
    cursor: pointer;
    transition: all 0.3s ease;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    gap: 6px;
    text-decoration: none;
    white-space: nowrap;
    height: 42px;
}

.btn-modern.primary {
    background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%);
    color: white;
    box-shadow: 0 4px 12px rgba(0, 102, 204, 0.3);
}

.btn-modern.primary:hover {
    transform: translateY(-2px);
    box-shadow: 0 6px 16px rgba(0, 102, 204, 0.4);
}

.btn-modern.outline {
    background: white;
    color: #666;
    border: 2px solid #e8f4f8;
}

.btn-modern.outline:hover {
    background: #f8fafc;
    border-color: #0066CC;
    color: #0066CC;
}

/* === TIMELINE LOG === */
.log-timeline {
    position: relative;
    padding: 0;
}

.log-timeline::before {
    content: '';
    position: absolute;
    left: 24px;
    top: 0;
    bottom: 0;
    width: 2px;
    background: linear-gradient(to bottom, #e8f4f8 0%, #cbd5e1 100%);
}

.log-item {
    position: relative;
    padding: 16px 16px 16px 60px;
    margin-bottom: 12px;
    background: white;
    border-radius: 12px;
    border: 1px solid #f0f0f0;
    transition: all 0.3s ease;
    cursor: pointer;
}

.log-item:hover {
    border-color: #0066CC;
    box-shadow: 0 4px 15px rgba(0, 102, 204, 0.1);
    transform: translateX(4px);
}

.log-icon {
    position: absolute;
    left: 8px;
    top: 16px;
    width: 34px;
    height: 34px;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    color: white;
    font-size: 0.95rem;
    z-index: 2;
    box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}

.log-icon.success { background: linear-gradient(135deg, #00C853 0%, #69F0AE 100%); }
.log-icon.danger { background: linear-gradient(135deg, #DC3545 0%, #FF6B7A 100%); }
.log-icon.warning { background: linear-gradient(135deg, #FF9900 0%, #FFB84D 100%); }
.log-icon.primary { background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%); }
.log-icon.info { background: linear-gradient(135deg, #00B8D4 0%, #26C6DA 100%); }
.log-icon.secondary { background: linear-gradient(135deg, #607D8B 0%, #90A4AE 100%); }

.log-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start;
    gap: 10px;
    margin-bottom: 8px;
    flex-wrap: wrap;
}

.log-title {
    display: flex;
    align-items: center;
    gap: 8px;
    flex-wrap: wrap;
    flex: 1;
    min-width: 0;
}

.log-action-badge {
    display: inline-flex;
    align-items: center;
    gap: 4px;
    padding: 3px 10px;
    border-radius: 12px;
    font-size: 0.72rem;
    font-weight: 700;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    font-family: 'Courier New', monospace;
}

.log-action-badge.success { background: #d1e7dd; color: #0f5132; }
.log-action-badge.danger { background: #f8d7da; color: #842029; }
.log-action-badge.warning { background: #fff3cd; color: #856404; }
.log-action-badge.primary { background: #cfe2ff; color: #084298; }
.log-action-badge.info { background: #cff4fc; color: #055160; }
.log-action-badge.secondary { background: #e9ecef; color: #41464b; }

.log-user {
    font-weight: 700;
    color: #003D7A;
    font-size: 0.9rem;
}

.log-time {
    font-size: 0.78rem;
    color: #999;
    white-space: nowrap;
    display: flex;
    align-items: center;
    gap: 4px;
}

.log-description {
    color: #333;
    font-size: 0.9rem;
    line-height: 1.5;
    margin-bottom: 6px;
}

.log-meta {
    display: flex;
    gap: 12px;
    flex-wrap: wrap;
    font-size: 0.78rem;
    color: #999;
}

.log-meta-item {
    display: inline-flex;
    align-items: center;
    gap: 4px;
}

.log-meta-item i {
    font-size: 0.85rem;
}

/* === DESKTOP TABLE === */
.table-modern {
    width: 100%;
    border-collapse: separate;
    border-spacing: 0;
    font-size: 0.9rem;
}

.table-modern thead {
    background: #f8fafc;
}

.table-modern th {
    padding: 14px 16px;
    text-align: left;
    font-weight: 700;
    color: #003D7A;
    border-bottom: 2px solid #e8f4f8;
    font-size: 0.82rem;
    text-transform: uppercase;
    letter-spacing: 0.5px;
}

.table-modern td {
    padding: 14px 16px;
    border-bottom: 1px solid #f0f0f0;
    vertical-align: middle;
}

.table-modern tbody tr {
    transition: all 0.2s ease;
    cursor: pointer;
}

.table-modern tbody tr:hover {
    background: #f8fafc;
}

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

/* === MOBILE CARDS === */
.mobile-cards {
    display: none;
}

/* === EMPTY STATE === */
.empty-state {
    text-align: center;
    padding: 60px 20px;
}

.empty-state-icon {
    width: 80px;
    height: 80px;
    margin: 0 auto 20px;
    background: #f8fafc;
    border-radius: 50%;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 2.5rem;
    color: #ccc;
}

.empty-state-title {
    color: #666;
    font-weight: 600;
    margin-bottom: 6px;
    font-size: 1.1rem;
}

.empty-state-subtitle {
    color: #999;
    font-size: 0.9rem;
    margin: 0;
}

/* === MODAL MODERN === */
.modal-modern .modal-content {
    border: none;
    border-radius: 16px;
    overflow: hidden;
    box-shadow: 0 20px 60px rgba(0,0,0,0.2);
}

.modal-modern .modal-header {
    background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%);
    color: white;
    padding: 18px 24px;
    border: none;
}

.modal-modern .modal-title {
    font-weight: 700;
    display: flex;
    align-items: center;
    gap: 10px;
    font-size: 1.1rem;
}

.modal-modern .modal-body {
    padding: 24px;
}

.modal-modern .modal-footer {
    padding: 16px 24px;
    border-top: 1px solid #f0f0f0;
    background: #f8fafc;
    display: flex;
    gap: 10px;
    justify-content: flex-end;
}

.detail-row {
    display: grid;
    grid-template-columns: 140px 1fr;
    gap: 12px;
    padding: 12px 0;
    border-bottom: 1px dashed #e8f4f8;
    align-items: flex-start;
}

.detail-row:last-child {
    border-bottom: none;
}

.detail-label {
    font-weight: 600;
    color: #666;
    font-size: 0.85rem;
    display: flex;
    align-items: center;
    gap: 6px;
}

.detail-value {
    color: #333;
    font-size: 0.92rem;
    word-break: break-word;
}

.detail-value.code {
    font-family: 'Courier New', monospace;
    background: #f8fafc;
    padding: 4px 10px;
    border-radius: 6px;
    border-left: 3px solid #0066CC;
}

/* === ALERT MODERN === */
.alert-modern {
    padding: 16px 20px;
    border-radius: 12px;
    margin-bottom: 20px;
    display: flex;
    align-items: flex-start;
    gap: 12px;
    font-size: 0.95rem;
    border: 2px solid;
}

.alert-modern.success {
    background: #e8f5e9;
    border-color: #a5d6a7;
    color: #2e7d32;
}

.alert-modern.danger {
    background: #ffebee;
    border-color: #ef9a9a;
    color: #c62828;
}

.alert-modern.info {
    background: #e3f2fd;
    border-color: #90caf9;
    color: #1565c0;
}

.alert-modern i {
    font-size: 1.3rem;
    margin-top: 2px;
    flex-shrink: 0;
}

/* === RESPONSIVE === */
@media (max-width: 992px) {
    .filter-form {
        grid-template-columns: 1fr 1fr;
    }
    
    .filter-form .form-group-modern:first-child {
        grid-column: 1 / -1;
    }
}

@media (max-width: 768px) {
    .navbar-modern {
        padding: 12px 15px;
    }
    
    .navbar-title {
        font-size: 1rem;
    }
    
    .navbar-subtitle {
        display: none;
    }
    
    .navbar-badge {
        display: none;
    }
    
    .main-content {
        padding: 15px;
    }
    
    .page-title {
        font-size: 1.4rem;
    }
    
    .page-title i {
        font-size: 1.6rem;
    }
    
    .summary-row {
        grid-template-columns: repeat(2, 1fr);
        gap: 12px;
    }
    
    .summary-card {
        padding: 14px;
    }
    
    .summary-card-value {
        font-size: 1.4rem;
    }
    
    .summary-card-title {
        font-size: 0.75rem;
    }
    
    .summary-card-icon {
        width: 34px;
        height: 34px;
        font-size: 1rem;
    }
    
    /* Hide desktop table, show mobile cards */
    .desktop-table {
        display: none;
    }
    
    .mobile-cards {
        display: block;
    }
    
    .card-header-modern {
        padding: 15px 18px;
        font-size: 1rem;
    }
    
    .card-body-modern {
        padding: 16px;
    }
    
    .filter-form {
        grid-template-columns: 1fr;
    }
    
    .filter-form .form-group-modern {
        grid-column: 1 / -1;
    }
    
    .btn-modern {
        width: 100%;
    }
    
    .log-timeline::before {
        left: 20px;
    }
    
    .log-item {
        padding: 14px 14px 14px 52px;
    }
    
    .log-icon {
        left: 4px;
        width: 32px;
        height: 32px;
        font-size: 0.88rem;
    }
    
    .detail-row {
        grid-template-columns: 1fr;
        gap: 4px;
    }
    
    .modal-modern .modal-dialog {
        margin: 0;
        max-width: 100%;
        min-height: 100vh;
    }
    
    .modal-modern .modal-content {
        border-radius: 0;
        min-height: 100vh;
    }
}

@media (max-width: 480px) {
    .navbar-modern {
        padding: 10px 12px;
    }
    
    .burger-btn {
        width: 36px;
        height: 36px;
        font-size: 1.1rem;
    }
    
    .navbar-title {
        font-size: 0.95rem;
    }
    
    .main-content {
        padding: 12px;
    }
    
    .page-title {
        font-size: 1.2rem;
        gap: 8px;
    }
    
    .summary-row {
        grid-template-columns: 1fr 1fr;
        gap: 10px;
    }
    
    .summary-card {
        padding: 12px;
    }
    
    .summary-card-value {
        font-size: 1.15rem;
    }
    
    .summary-card-footer {
        font-size: 0.7rem;
    }
    
    .card-header-modern {
        padding: 12px 15px;
        font-size: 0.95rem;
    }
    
    .card-body-modern {
        padding: 14px;
    }
    
    .log-item {
        padding: 12px 12px 12px 48px;
    }
    
    .log-description {
        font-size: 0.85rem;
    }
    
    .log-meta {
        font-size: 0.72rem;
    }
}
</style>

<!-- Navbar Modern dengan Burger Icon -->
<div class="navbar-modern">
    <div class="navbar-left">
        <button class="burger-btn" onclick="toggleSidebar()">
            <i class="bi bi-list"></i>
        </button>
        <div>
            <h1 class="navbar-title">Activity Logs</h1>
            <p class="navbar-subtitle">Audit Trail Sistem</p>
        </div>
    </div>
    <div class="navbar-right">
        <span class="navbar-badge">
            <i class="bi bi-journal-text"></i> Logs
        </span>
    </div>
</div>

<div class="main-content">
    <!-- Page Header -->
    <div class="page-header">
        <h2 class="page-title">
            <i class="bi bi-journal-text"></i>
            <span>Activity Logs</span>
        </h2>
        <p class="page-subtitle">Audit trail aktivitas pengguna sistem</p>
    </div>

    <?= flashMessage() ?>

    <!-- Summary Cards -->
    <div class="summary-row">
        <div class="summary-card primary">
            <div class="summary-card-header">
                <h6 class="summary-card-title">
                    <i class="bi bi-list-ul"></i> Total Log
                </h6>
                <div class="summary-card-icon">
                    <i class="bi bi-journal-text"></i>
                </div>
            </div>
            <div class="summary-card-value"><?= number_format($totalLogs) ?></div>
            <div class="summary-card-footer">📋 500 log terakhir</div>
        </div>
        <div class="summary-card success">
            <div class="summary-card-header">
                <h6 class="summary-card-title">
                    <i class="bi bi-calendar-day"></i> Hari Ini
                </h6>
                <div class="summary-card-icon">
                    <i class="bi bi-calendar-check"></i>
                </div>
            </div>
            <div class="summary-card-value"><?= number_format($todayLogs) ?></div>
            <div class="summary-card-footer">📅 Aktivitas hari ini</div>
        </div>
        <div class="summary-card warning">
            <div class="summary-card-header">
                <h6 class="summary-card-title">
                    <i class="bi bi-people"></i> User Aktif
                </h6>
                <div class="summary-card-icon">
                    <i class="bi bi-person-check"></i>
                </div>
            </div>
            <div class="summary-card-value"><?= count($uniqueUsers) ?></div>
            <div class="summary-card-footer">👥 User unik</div>
        </div>
        <div class="summary-card info">
            <div class="summary-card-header">
                <h6 class="summary-card-title">
                    <i class="bi bi-lightning"></i> Jenis Aksi
                </h6>
                <div class="summary-card-icon">
                    <i class="bi bi-activity"></i>
                </div>
            </div>
            <div class="summary-card-value"><?= count($uniqueActions) ?></div>
            <div class="summary-card-footer">⚡ Tipe aksi berbeda</div>
        </div>
    </div>

    <!-- Filter -->
    <div class="card-modern">
        <div class="card-header-modern primary">
            <div class="title-left">
                <i class="bi bi-funnel-fill"></i>
                <span>Filter & Pencarian</span>
            </div>
        </div>
        <div class="card-body-modern">
            <form method="GET" class="filter-form">
                <div class="form-group-modern">
                    <label class="form-label-modern">
                        <i class="bi bi-search"></i> Cari
                    </label>
                    <input type="text" name="search" class="form-control-modern" 
                           value="<?= htmlspecialchars($search) ?>" 
                           placeholder="Cari deskripsi, aksi, atau username...">
                </div>
                <div class="form-group-modern">
                    <label class="form-label-modern">
                        <i class="bi bi-person"></i> User
                    </label>
                    <select name="user" class="form-select-modern">
                        <option value="">Semua User</option>
                        <?php foreach ($usersList as $u): ?>
                            <option value="<?= $u['id'] ?>" <?= $filterUser == $u['id'] ? 'selected' : '' ?>>
                                <?= htmlspecialchars($u['username']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group-modern">
                    <label class="form-label-modern">
                        <i class="bi bi-lightning"></i> Aksi
                    </label>
                    <select name="action" class="form-select-modern">
                        <option value="">Semua Aksi</option>
                        <?php foreach ($actionsList as $a): ?>
                            <option value="<?= htmlspecialchars($a) ?>" <?= $filterAction === $a ? 'selected' : '' ?>>
                                <?= htmlspecialchars($a) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="form-group-modern">
                    <label class="form-label-modern">
                        <i class="bi bi-calendar3"></i> Tanggal
                    </label>
                    <input type="date" name="date" class="form-control-modern" 
                           value="<?= htmlspecialchars($filterDate) ?>">
                </div>
                <div style="display: flex; gap: 8px; grid-column: 1 / -1;">
                    <button type="submit" class="btn-modern primary" style="flex: 1;">
                        <i class="bi bi-search"></i>
                        <span>Filter</span>
                    </button>
                    <a href="logs.php" class="btn-modern outline" style="flex: 1;">
                        <i class="bi bi-arrow-counterclockwise"></i>
                        <span>Reset</span>
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Logs List -->
    <div class="card-modern">
        <div class="card-header-modern info">
            <div class="title-left">
                <i class="bi bi-clock-history"></i>
                <span>Riwayat Aktivitas</span>
            </div>
            <span class="navbar-badge" style="background: rgba(255,255,255,0.25);">
                <?= count($logs) ?> log
            </span>
        </div>
        
        <?php if (empty($logs)): ?>
            <div class="empty-state">
                <div class="empty-state-icon">
                    <i class="bi bi-inbox"></i>
                </div>
                <h5 class="empty-state-title">Tidak ada log aktivitas</h5>
                <p class="empty-state-subtitle">Coba ubah filter atau kata kunci pencarian</p>
            </div>
        <?php else: ?>
            <!-- Desktop Table View -->
            <div class="desktop-table" style="overflow-x: auto;">
                <table class="table-modern">
                    <thead>
                        <tr>
                            <th style="width: 160px;">Waktu</th>
                            <th>User</th>
                            <th>Aksi</th>
                            <th>Deskripsi</th>
                            <th>Target</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($logs as $l): 
                            $actionInfo = getActionInfo($l['action']);
                        ?>
                            <tr data-bs-toggle="modal" data-bs-target="#logModal<?= $l['id'] ?>">
                                <td>
                                    <div style="font-weight: 600; color: #003D7A; font-size: 0.88rem;">
                                        <?= date('d/m/Y', strtotime($l['created_at'])) ?>
                                    </div>
                                    <div style="font-size: 0.78rem; color: #999;">
                                        <?= date('H:i', strtotime($l['created_at'])) ?> WIB
                                        <div style="color: #0066CC; font-size: 0.72rem; margin-top: 2px;">
                                            <?= timeAgo($l['created_at']) ?>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <div style="display: flex; align-items: center; gap: 8px;">
                                        <div style="width: 32px; height: 32px; border-radius: 50%; background: linear-gradient(135deg, #0066CC 0%, #00B4FF 100%); color: white; display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 0.85rem; flex-shrink: 0;">
                                            <?= strtoupper(substr($l['username'] ?? 'S', 0, 1)) ?>
                                        </div>
                                        <div>
                                            <div style="font-weight: 600; color: #003D7A; font-size: 0.88rem;">
                                                <?= htmlspecialchars($l['username'] ?? 'System') ?>
                                            </div>
                                        </div>
                                    </div>
                                </td>
                                <td>
                                    <span class="log-action-badge <?= $actionInfo[1] ?>">
                                        <i class="bi <?= $actionInfo[0] ?>"></i>
                                        <?= htmlspecialchars($l['action']) ?>
                                    </span>
                                </td>
                                <td>
                                    <div style="font-size: 0.88rem; color: #333;">
                                        <?= htmlspecialchars($l['description']) ?>
                                    </div>
                                </td>
                                <td>
                                    <?php if ($l['target_type']): ?>
                                        <code style="background: #f8fafc; padding: 3px 8px; border-radius: 6px; font-size: 0.78rem; border-left: 3px solid #0066CC;">
                                            <?= htmlspecialchars($l['target_type']) ?> #<?= $l['target_id'] ?>
                                        </code>
                                    <?php else: ?>
                                        <span style="color: #ccc;">-</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            
            <!-- Mobile Timeline View -->
            <div class="mobile-cards" style="padding: 20px 16px;">
                <div class="log-timeline">
                    <?php foreach ($logs as $l): 
                        $actionInfo = getActionInfo($l['action']);
                    ?>
                        <div class="log-item" data-bs-toggle="modal" data-bs-target="#logModal<?= $l['id'] ?>">
                            <div class="log-icon <?= $actionInfo[1] ?>">
                                <i class="bi <?= $actionInfo[0] ?>"></i>
                            </div>
                            
                            <div class="log-header">
                                <div class="log-title">
                                    <span class="log-action-badge <?= $actionInfo[1] ?>">
                                        <?= htmlspecialchars($l['action']) ?>
                                    </span>
                                    <span class="log-user">
                                        <?= htmlspecialchars($l['username'] ?? 'System') ?>
                                    </span>
                                </div>
                                <div class="log-time">
                                    <i class="bi bi-clock"></i>
                                    <?= timeAgo($l['created_at']) ?>
                                </div>
                            </div>
                            
                            <div class="log-description">
                                <?= htmlspecialchars($l['description']) ?>
                            </div>
                            
                            <div class="log-meta">
                                <span class="log-meta-item">
                                    <i class="bi bi-calendar3"></i>
                                    <?= date('d/m/Y H:i', strtotime($l['created_at'])) ?>
                                </span>
                                <?php if ($l['target_type']): ?>
                                    <span class="log-meta-item">
                                        <i class="bi bi-bullseye"></i>
                                        <?= htmlspecialchars($l['target_type']) ?> #<?= $l['target_id'] ?>
                                    </span>
                                <?php endif; ?>
                            </div>
                        </div>
                    <?php endforeach; ?>
                </div>
            </div>
        <?php endif; ?>
    </div>
</div>

<!-- Detail Modals -->
<?php foreach ($logs as $l): 
    $actionInfo = getActionInfo($l['action']);
?>
<div class="modal fade modal-modern" id="logModal<?= $l['id'] ?>" tabindex="-1">
    <div class="modal-dialog modal-dialog-centered modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title">
                    <span class="log-icon <?= $actionInfo[1] ?>" style="position: static; width: 32px; height: 32px; font-size: 0.9rem;">
                        <i class="bi <?= $actionInfo[0] ?>"></i>
                    </span>
                    <span>Detail Log Aktivitas</span>
                </h5>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <div class="modal-body">
                <div class="detail-row">
                    <div class="detail-label">
                        <i class="bi bi-lightning"></i> Aksi
                    </div>
                    <div class="detail-value">
                        <span class="log-action-badge <?= $actionInfo[1] ?>">
                            <i class="bi <?= $actionInfo[0] ?>"></i>
                            <?= htmlspecialchars($l['action']) ?>
                        </span>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">
                        <i class="bi bi-person"></i> User
                    </div>
                    <div class="detail-value">
                        <strong><?= htmlspecialchars($l['username'] ?? 'System') ?></strong>
                        <?php if (!empty($l['user_fullname'])): ?>
                            <div style="font-size: 0.82rem; color: #999; margin-top: 2px;">
                                <?= htmlspecialchars($l['user_fullname']) ?>
                            </div>
                        <?php endif; ?>
                    </div>
                </div>
                <div class="detail-row">
                    <div class="detail-label">
                        <i class="bi bi-chat-left-text"></i> Deskripsi
                    </div>
                    <div class="detail-value">
                        <?= htmlspecialchars($l['description']) ?>
                    </div>
                </div>
                <?php if ($l['target_type']): ?>
                <div class="detail-row">
                    <div class="detail-label">
                        <i class="bi bi-bullseye"></i> Target
                    </div>
                    <div class="detail-value">
                        <code style="background: #f8fafc; padding: 6px 12px; border-radius: 6px; border-left: 3px solid #0066CC;">
                            <?= htmlspecialchars($l['target_type']) ?> #<?= $l['target_id'] ?>
                        </code>
                    </div>
                </div>
                <?php endif; ?>
                <div class="detail-row">
                    <div class="detail-label">
                        <i class="bi bi-calendar3"></i> Waktu
                    </div>
                    <div class="detail-value">
                        <strong><?= date('d F Y, H:i:s', strtotime($l['created_at'])) ?> WIB</strong>
                        <div style="font-size: 0.82rem; color: #0066CC; margin-top: 4px;">
                            ⏱️ <?= timeAgo($l['created_at']) ?>
                        </div>
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn-modern outline" data-bs-dismiss="modal">Tutup</button>
            </div>
        </div>
    </div>
</div>
<?php endforeach; ?>

<script>
function toggleSidebar() {
    const sidebar = document.getElementById('sidebar');
    const overlay = document.getElementById('sidebarOverlay');
    if (sidebar && overlay) {
        sidebar.classList.toggle('active');
        overlay.classList.toggle('active');
        document.body.style.overflow = sidebar.classList.contains('active') ? 'hidden' : '';
    }
}
</script>

<?php require __DIR__ . '/../includes/footer.php'; ?>