<?php 
// Christo Lochenberg Developments | info@christodev.co.za | +27 61 523 9126
require APPROOT . '/app/Views/layouts/header.php'; 
?>
<div class="max-w-7xl mx-auto mb-10 space-y-8">
    
    <div class="flex flex-col md:flex-row md:items-center justify-between pb-4">
        <div class="mb-4 md:mb-0">
            <h1 class="text-2xl md:text-3xl font-bold text-gray-900 dark:text-white mb-1">System Logs</h1>
            <p class="text-sm text-gray-600 dark:text-gray-400">A complete historical timeline of all platform events.</p>
        </div>
        
        <form action="<?php echo URLROOT; ?>/admin/clearLogs" method="POST" onsubmit="confirmAction(event, this, 'Clear System Logs', 'Are you sure you want to permanently clear ALL system logs? This action cannot be undone.');" class="w-full md:w-auto">
            <button type="submit" class="w-full md:w-auto bg-red-500 hover:bg-red-600 text-white font-bold py-2 px-4 rounded-lg transition-all focus:ring-4 focus:ring-red-500/50 text-sm shadow-sm flex justify-center items-center">
                <svg class="w-4 h-4 mr-2" fill="none" stroke="currentColor" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg">
                    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M19 7l-.867 12.142A2 2 0 0116.138 21H7.862a2 2 0 01-1.995-1.858L5 7m5 4v6m4-6v6m1-10V4a1 1 0 00-1-1h-4a1 1 0 00-1 1v3M4 7h16"></path>
                </svg>
                Clear All Logs
            </button>
        </form>
    </div>

    <div class="bg-white dark:bg-gray-800 rounded-xl shadow-sm border border-gray-200 dark:border-gray-700/50 overflow-hidden">
        <div class="p-5 border-b border-gray-200 dark:border-gray-700/50 bg-gray-50 dark:bg-gray-900/40 flex justify-between items-center">
            <h2 class="text-sm font-bold text-gray-900 dark:text-white flex items-center">
                <svg class="w-4 h-4 mr-2 text-primary" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg>
                Event Timeline
            </h2>
        </div>
        
        <div class="w-full text-gray-900 dark:text-gray-100 p-5">
            <table id="logsTable" class="w-full text-left whitespace-nowrap">
                <thead class="border-b border-gray-200 dark:border-gray-700/50">
                    <tr>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Timestamp</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Action</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">User</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">Description</th>
                        <th class="px-6 py-4 text-[11px] font-extrabold text-gray-500 dark:text-gray-400 uppercase tracking-widest">IP Address</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if(!empty($data['logs'])): ?>
                        <?php foreach($data['logs'] as $log) : ?>
                        <tr class="border-b border-gray-100 dark:border-gray-700/50 last:border-0 hover:bg-gray-50 dark:hover:bg-gray-700/25 transition-colors">
                            <td class="px-6 py-4 whitespace-nowrap text-xs text-gray-500 dark:text-gray-400 font-mono" data-order="<?php echo strtotime($log->created_at); ?>">
                                <?php echo htmlspecialchars($log->created_at); ?>
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-xs font-bold text-primary"><?php echo htmlspecialchars($log->action); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-sm text-gray-900 dark:text-white"><?php echo htmlspecialchars($log->user_name ?? 'Guest/System'); ?></td>
                            <td class="px-6 py-4 text-sm text-gray-600 dark:text-gray-400"><?php echo htmlspecialchars($log->description); ?></td>
                            <td class="px-6 py-4 whitespace-nowrap text-xs text-gray-500 dark:text-gray-400 font-mono"><?php echo htmlspecialchars($log->ip_address); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

<script>
    $(document).ready(function() {
        $('#logsTable').DataTable({
            responsive: true,
            order: [[ 0, "desc" ]],
            pageLength: 50,
            language: {
                search: "",
                searchPlaceholder: "Search logs...",
                lengthMenu: "Show _MENU_ entries",
                emptyTable: `<div class="flex flex-col items-center justify-center py-10">
                                <div class="h-16 w-16 bg-gray-100 dark:bg-gray-800 rounded-full flex items-center justify-center mb-4">
                                    <svg class="w-8 h-8 text-gray-400 dark:text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12h6m-6 4h6m2 5H7a2 2 0 01-2-2V5a2 2 0 012-2h5.586a1 1 0 01.707.293l5.414 5.414a1 1 0 01.293.707V19a2 2 0 01-2 2z"></path></svg>
                                </div>
                                <h3 class="text-lg font-bold text-gray-900 dark:text-white">System Logs Empty</h3>
                                <p class="text-sm text-gray-500 dark:text-gray-400 mt-1">No system events have been recorded yet.</p>
                            </div>`
            }
        });
    });
</script>
<?php require APPROOT . '/app/Views/layouts/footer.php'; ?>