<?php
/**
 * Audit Logs
 * View system audit trail
 * Kenyan Pension & Life Insurance Management System
 */

require_once 'layouts/main.php';
require_once 'includes/auth.php';

requireLogin();
requirePermission('view_audit_logs');

$filters = [
    'event_type' => $_GET['event_type'] ?? '',
    'event_category' => $_GET['event_category'] ?? '',
    'date_from' => $_GET['date_from'] ?? date('Y-m-01'),
    'date_to' => $_GET['date_to'] ?? date('Y-m-t')
];

$where = ["1=1"];
$params = [];
$types = '';

if (!empty($filters['event_type'])) {
    $where[] = "al.event_type = ?";
    $params[] = $filters['event_type'];
    $types .= 's';
}

if (!empty($filters['event_category'])) {
    $where[] = "al.event_category = ?";
    $params[] = $filters['event_category'];
    $types .= 's';
}

$where[] = "DATE(al.created_at) BETWEEN ? AND ?";
$params[] = $filters['date_from'];
$params[] = $filters['date_to'];
$types .= 'ss';

$whereClause = implode(' AND ', $where);

$sql = "SELECT al.* FROM audit_logs al WHERE $whereClause ORDER BY al.created_at DESC LIMIT 100";
$stmt = $conn->prepare($sql);
if (!empty($params)) {
    $stmt->bind_param($types, ...$params);
}
$stmt->execute();
$logs = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);

$pageTitle = 'Audit Logs';
include 'layouts/topbar.php';
?>

<head>
    <?php includeFileWithVariables('layouts/title-meta.php', array('title' => 'Audit Logs')); ?>
    <link rel="stylesheet" href="assets/css/pension-dashboard.css">
    <?php include 'layouts/head-css.php'; ?>
</head>

<body data-sidebar="dark">
    <div id="layout-wrapper">
        <?php include 'layouts/sidebar.php'; ?>
        
        <div class="main-content">
            <div class="page-content">
                <div class="container-fluid">
                    <div class="welcome-header" style="background: linear-gradient(135deg, #6366f1 0%, #4f46e5 100%); margin-bottom: 2rem;">
                        <div class="welcome-content">
                            <div class="welcome-text">
                                <h2>Audit Logs</h2>
                                <p>System-wide audit trail and activity logs</p>
                            </div>
                        </div>
                    </div>

                    <div class="row">
                        <div class="col-12">
                            <div class="page-card">
                                <div class="page-card-header">
                                    <h4 class="page-card-title">Audit Trail</h4>
                                </div>
                                <div class="page-card-body">
                                    <form method="GET" class="mb-3">
                                        <div class="row">
                                            <div class="col-md-3 mb-2">
                                                <input type="text" name="event_type" class="form-control" placeholder="Event Type" value="<?php echo htmlspecialchars($filters['event_type']); ?>">
                                            </div>
                                            <div class="col-md-3 mb-2">
                                                <input type="text" name="event_category" class="form-control" placeholder="Category" value="<?php echo htmlspecialchars($filters['event_category']); ?>">
                                            </div>
                                            <div class="col-md-2 mb-2">
                                                <input type="date" name="date_from" class="form-control" value="<?php echo $filters['date_from']; ?>">
                                            </div>
                                            <div class="col-md-2 mb-2">
                                                <input type="date" name="date_to" class="form-control" value="<?php echo $filters['date_to']; ?>">
                                            </div>
                                            <div class="col-md-2 mb-2">
                                                <button type="submit" class="btn btn-primary w-100">Filter</button>
                                            </div>
                                        </div>
                                    </form>
                                    
                                    <div class="table-responsive">
                                        <table class="data-table">
                                            <thead>
                                                <tr>
                                                    <th>Date & Time</th>
                                                    <th>Event Type</th>
                                                    <th>Category</th>
                                                    <th>Description</th>
                                                    <th>Performed By</th>
                                                    <th>IP Address</th>
                                                </tr>
                                            </thead>
                                            <tbody>
                                                <?php if (empty($logs)): ?>
                                                    <tr><td colspan="6" class="text-center">No audit logs found</td></tr>
                                                <?php else: ?>
                                                    <?php foreach ($logs as $log): ?>
                                                        <tr>
                                                            <td><?php echo date('d M Y, h:i A', strtotime($log['created_at'])); ?></td>
                                                            <td><span class="status-badge status-pending"><?php echo htmlspecialchars($log['event_type']); ?></span></td>
                                                            <td><?php echo htmlspecialchars($log['event_category']); ?></td>
                                                            <td><?php echo htmlspecialchars($log['event_description']); ?></td>
                                                            <td><?php echo htmlspecialchars($log['performed_by']); ?></td>
                                                            <td><?php echo htmlspecialchars($log['ip_address'] ?? 'N/A'); ?></td>
                                                        </tr>
                                                    <?php endforeach; ?>
                                                <?php endif; ?>
                                            </tbody>
                                        </table>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <?php include 'layouts/footer.php'; ?>
        </div>
    </div>
    <?php include 'layouts/vendor-scripts.php'; ?>
</body>
</html>
