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

$pdo = db();
$usersCount = $pdo->query("SELECT COUNT(*) FROM users")->fetchColumn();
$carsCount = $pdo->query("SELECT COUNT(*) FROM cars")->fetchColumn();
$branchesCount = $pdo->query("SELECT COUNT(*) FROM branches")->fetchColumn();
$salesCount = $pdo->query("SELECT COUNT(*) FROM sales WHERE status = 'Completed'")->fetchColumn();
$revenue = $pdo->query("SELECT SUM(final_price) FROM sales WHERE status = 'Completed'")->fetchColumn() ?: 0;
$activeContracts = $pdo->query("SELECT COUNT(*) FROM installments WHERE status = 'Active'")->fetchColumn();

// Recent Activity
$activities = $pdo->query("SELECT * FROM activity_logs ORDER BY created_at DESC LIMIT 5")->fetchAll();
?>

<div class="page-header">
    <div>
        <h1>Enterprise Dashboard</h1>
        <span style="color: var(--text-secondary);">Overview of AFG_CARS Operations</span>
    </div>
    <div style="background: rgba(255,255,255,0.1); padding: 0.5rem 1rem; border-radius: 4px;">
        Date: <?php echo date('Y-m-d'); ?>
    </div>
</div>

<div class="card-grid">
    <div class="stat-card">
        <div class="stat-value"><?php echo $usersCount; ?></div>
        <div class="stat-label">Total Users</div>
    </div>
    <div class="stat-card">
        <div class="stat-value"><?php echo $branchesCount; ?></div>
        <div class="stat-label">Branches</div>
    </div>
    <div class="stat-card">
        <div class="stat-value"><?php echo $carsCount; ?></div>
        <div class="stat-label">Total Inventory</div>
    </div>
    <div class="stat-card" style="border-color: var(--accent-color);">
        <div class="stat-value">$<?php echo number_format($revenue); ?></div>
        <div class="stat-label">Total Revenue</div>
    </div>
    <div class="stat-card">
        <div class="stat-value"><?php echo $salesCount; ?></div>
        <div class="stat-label">Completed Sales</div>
    </div>
    <div class="stat-card">
        <div class="stat-value"><?php echo $activeContracts; ?></div>
        <div class="stat-label">Active Installments</div>
    </div>
</div>

<div style="display: grid; grid-template-columns: 2fr 1fr; gap: 1.5rem;">
    <div style="background: var(--card-bg); border-radius: var(--border-radius); padding: 1.5rem; border: 1px solid var(--border-color);">
        <h3 style="margin-bottom: 1rem; color: var(--accent-color);">System Activity Log</h3>
        <?php if ($activities): ?>
            <ul style="list-style: none; padding: 0;">
                <?php foreach ($activities as $log): ?>
                    <li style="border-bottom: 1px solid var(--border-color); padding: 0.8rem 0; display: flex; justify-content: space-between;">
                        <span><?php echo htmlspecialchars($log['action']); ?></span>
                        <small style="color: var(--text-secondary);"><?php echo $log['created_at']; ?></small>
                    </li>
                <?php endforeach; ?>
            </ul>
        <?php else: ?>
            <p style="color: var(--text-secondary);">No recent activity logged.</p>
        <?php endif; ?>
    </div>

    <div style="background: var(--card-bg); border-radius: var(--border-radius); padding: 1.5rem; border: 1px solid var(--border-color);">
        <h3 style="margin-bottom: 1rem; color: var(--accent-color);">Quick Actions</h3>
        <div style="display: flex; flex-direction: column; gap: 0.8rem;">
            <a href="cars.php?action=add" class="btn-sm btn-primary" style="text-align: center;">Add New Car</a>
            <a href="branches.php?action=add" class="btn-sm" style="background: #444; color: white; text-align: center;">New Branch</a>
            <a href="users.php?action=add" class="btn-sm" style="background: #444; color: white; text-align: center;">Add User</a>
        </div>
    </div>
</div>

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