<?php
declare(strict_types=1);

error_reporting(E_ALL);
ini_set('display_errors', '1');

require_once __DIR__ . '/../config/database.php';
require_once __DIR__ . '/../config/app.php';

checkAuth();
$conn = getConnection();

if (!function_exists('e')) {
    function e($value): string {
        return htmlspecialchars((string)$value, ENT_QUOTES, 'UTF-8');
    }
}

$limit = 20;
$page = isset($_GET['page']) ? (int)$_GET['page'] : 1;
$offset = ($page - 1) * $limit;

$totalStmt = $conn->query("SELECT COUNT(*) as total FROM activity_logs");
$totalRow = $totalStmt->fetch_assoc();
$total = (int)$totalRow['total'];
$totalPages = ceil($total / $limit);

$sql = "
    SELECT a.*, COALESCE(e.full_name, 'System') AS full_name
    FROM activity_logs a
    LEFT JOIN employees e ON a.employee_id = e.id
    ORDER BY a.created_at DESC
    LIMIT $offset, $limit
";
$result = $conn->query($sql);
$activities = [];
if ($result) {
    while ($row = $result->fetch_assoc()) {
        $activities[] = $row;
    }
}
?>
<div class="max-w-7xl mx-auto p-4 md:p-6">
    <!-- Header dengan tombol kembali -->
    <div class="flex justify-between items-center mb-4">
        <h1 class="text-2xl font-bold text-gray-800">Aktivitas Log Sistem</h1>
        <a href="javascript:history.back()" class="px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300">
            <i class="fas fa-arrow-left mr-2"></i> Kembali
        </a>
    </div>

    <div class="bg-white rounded-2xl shadow-md overflow-hidden">
        <div class="bg-gradient-to-r from-blue-600 to-indigo-600 px-6 py-4">
            <h2 class="text-xl font-semibold text-white flex items-center">
                <i class="fas fa-history mr-2"></i> Aktivitas Log Sistem
            </h2>
        </div>
        
        <div class="overflow-x-auto">
            <table class="min-w-full divide-y divide-gray-200">
                <thead class="bg-gray-50">
                    <tr>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Waktu</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Aksi</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Modul</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">Deskripsi</th>
                        <th class="px-6 py-3 text-left text-xs font-medium text-gray-500 uppercase">User</th>
                    </tr>
                </thead>
                <tbody class="bg-white divide-y divide-gray-200">
                    <?php if (empty($activities)): ?>
                        <tr>
                            <td colspan="5" class="px-6 py-8 text-center text-gray-500">Belum ada aktivitas tercatat.</td>
                        </tr>
                    <?php else: ?>
                        <?php foreach ($activities as $act): ?>
                            <tr class="hover:bg-gray-50">
                                <td class="px-6 py-4 text-sm text-gray-600 whitespace-nowrap">
                                    <?= date('d/m/Y H:i:s', strtotime((string)$act['created_at'])) ?>
                                </td>
                                <td class="px-6 py-4 text-sm">
                                    <?php
                                    $badgeClass = '';
                                    if ($act['action'] == 'create') $badgeClass = 'bg-green-100 text-green-800';
                                    elseif ($act['action'] == 'update') $badgeClass = 'bg-yellow-100 text-yellow-800';
                                    elseif ($act['action'] == 'delete') $badgeClass = 'bg-red-100 text-red-800';
                                    else $badgeClass = 'bg-blue-100 text-blue-800';
                                    ?>
                                    <span class="px-2 py-1 text-xs rounded-full <?= $badgeClass ?>">
                                        <?= e(ucfirst($act['action'])) ?>
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-600"><?= e($act['module_name'] ?? '-') ?></td>
                                <td class="px-6 py-4 text-sm text-gray-800"><?= e($act['description'] ?? '-') ?></td>
                                <td class="px-6 py-4 text-sm text-gray-600"><?= e($act['full_name'] ?? 'System') ?></td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($totalPages > 1): ?>
        <div class="px-6 py-4 bg-gray-50 border-t flex justify-between items-center">
            <div class="text-sm text-gray-600">
                Menampilkan <?= $offset + 1 ?> - <?= min($offset + $limit, $total) ?> dari <?= $total ?> aktivitas
            </div>
            <div class="flex space-x-2">
                <?php if ($page > 1): ?>
                    <a href="?module=activity_log&page=<?= $page-1 ?>" class="px-3 py-1 border rounded-lg hover:bg-gray-100">Prev</a>
                <?php endif; ?>
                <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                    <a href="?module=activity_log&page=<?= $i ?>" class="px-3 py-1 border rounded-lg <?= $i == $page ? 'bg-blue-600 text-white' : 'hover:bg-gray-100' ?>"><?= $i ?></a>
                <?php endfor; ?>
                <?php if ($page < $totalPages): ?>
                    <a href="?module=activity_log&page=<?= $page+1 ?>" class="px-3 py-1 border rounded-lg hover:bg-gray-100">Next</a>
                <?php endif; ?>
            </div>
        </div>
        <?php endif; ?>
    </div>
</div>