<?php
require_once __DIR__ . '/../includes/header.php';

// حماية: فقط للسوبر أدمن والمدير التنفيذي
if (!in_array($_SESSION['role'], ['super_admin', 'ceo'])) {
    echo "<div class='container mt-5'><div class='alert alert-danger'>عذراً، ليس لديك صلاحية.</div></div>";
    require_once __DIR__ . '/../includes/footer.php';
    exit;
}
?>

<div class="container mt-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h3 class="text-secondary fw-bold"><i class="bi bi-shield-check"></i> سجل نشاطات النظام</h3>
            <p class="text-muted">مراقبة العمليات الحساسة والأمنية</p>
        </div>
        <button onclick="window.print()" class="btn btn-outline-dark btn-sm"><i class="bi bi-printer"></i> طباعة</button>
    </div>

    <div class="card shadow-sm border-0">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-hover table-striped mb-0 align-middle">
                    <thead class="table-dark">
                        <tr>
                            <th>الوقت والتاريخ</th>
                            <th>المستخدم</th>
                            <th>الدور</th>
                            <th>نوع الحدث</th>
                            <th>التفاصيل</th>
                            <th>IP Address</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php
                        // عرض آخر 100 عملية
                        $sql = "SELECT * FROM system_logs ORDER BY created_at DESC LIMIT 100";
                        $stmt = $pdo->query($sql);
                        
                        if ($stmt->rowCount() > 0):
                            while ($row = $stmt->fetch()):
                                $badge_color = match($row['action']) {
                                    'login' => 'info',
                                    'delete', 'reject' => 'danger',
                                    'approve', 'issue_cheque' => 'success',
                                    'update' => 'warning',
                                    default => 'secondary'
                                };
                        ?>
                        <tr>
                            <td style="direction: ltr;"><?php echo $row['created_at']; ?></td>
                            <td class="fw-bold"><?php echo htmlspecialchars($row['username']); ?></td>
                            <td><small class="badge bg-light text-dark border"><?php echo $row['role']; ?></small></td>
                            <td><span class="badge bg-<?php echo $badge_color; ?>"><?php echo $row['action']; ?></span></td>
                            <td><?php echo htmlspecialchars($row['description']); ?></td>
                            <td class="text-muted small"><?php echo $row['ip_address']; ?></td>
                        </tr>
                        <?php endwhile; else: ?>
                            <tr><td colspan="6" class="text-center py-4">لا توجد سجلات محفوظة بعد.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

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