<?php
require_once 'config.php';
checkAdminAuth();

// Ultra-fast queries with cache
$stats = $cache->get('dashboard_stats');
if (!$stats) {
    $stats = db_row("
        SELECT 
            (SELECT COUNT(*) FROM orders WHERE DATE(created_at)=CURDATE()) as today_orders,
            (SELECT COUNT(*) FROM orders WHERE status='delivered' AND DATE(created_at)=CURDATE()) as completed,
            (SELECT COUNT(*) FROM captains WHERE status='active') as active_captains,
            (SELECT COUNT(*) FROM meals WHERE is_active=1) as available_meals,
            (SELECT COALESCE(SUM(total),0) FROM orders WHERE status='delivered' AND DATE(created_at)=CURDATE()) as revenue
    ", 300);
    $cache->set('dashboard_stats', $stats, 300);
}

$recent = $cache->get('recent_orders');
if (!$recent) {
    $recent = db_query("
        SELECT o.id, o.total, o.status, o.created_at, c.name as customer 
        FROM orders o 
        JOIN customers c ON o.customer_id=c.id 
        ORDER BY o.id DESC LIMIT 7
    ", 60);
    $cache->set('recent_orders', $recent, 60);
}

$page_title = 'لوحة التحكم';
include 'includes/header.php';
include 'includes/sidebar.php';
?>

<div class="main-content">
    <div class="top-bar">
        <i class="bi bi-person-circle text-primary fs-4 me-2"></i>
        <span>مرحباً بعودتك، <strong><?php echo e($_SESSION['admin_name']); ?></strong></span>
        <span class="ms-auto text-muted">
            <i class="bi bi-calendar3 me-1"></i><?php echo date('Y-m-d'); ?>
        </span>
    </div>

    <!-- Stats Grid -->
    <div class="row g-3 mb-4">
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <div class="stat-number"><?php echo $stats['today_orders'] ?? 0; ?></div>
                <div class="stat-label">طلبات اليوم</div>
                <i class="bi bi-cart"></i>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <div class="stat-number text-success"><?php echo $stats['completed'] ?? 0; ?></div>
                <div class="stat-label">مكتملة</div>
                <i class="bi bi-check-circle-fill text-success"></i>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <div class="stat-number text-warning"><?php echo $stats['active_captains'] ?? 0; ?></div>
                <div class="stat-label">كباتن نشطين</div>
                <i class="bi bi-person-check-fill text-warning"></i>
            </div>
        </div>
        <div class="col-6 col-md-3">
            <div class="stat-card">
                <div class="stat-number text-info"><?php echo $stats['available_meals'] ?? 0; ?></div>
                <div class="stat-label">وجبات متاحة</div>
                <i class="bi bi-egg-fried text-info"></i>
            </div>
        </div>
        <div class="col-12">
            <div class="stat-card">
                <div class="stat-number text-danger"><?php echo number_format($stats['revenue'] ?? 0, 2); ?></div>
                <div class="stat-label">إيرادات اليوم</div>
                <i class="bi bi-currency-dollar text-danger"></i>
            </div>
        </div>
    </div>

    <!-- Recent Orders -->
    <div class="card">
        <div class="card-header d-flex justify-content-between align-items-center">
            <span><i class="bi bi-clock-history me-2 text-primary"></i>آخر الطلبات</span>
            <a href="orders.php" class="btn btn-sm btn-outline-primary">عرض الكل</a>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead>
                    <tr><th>#</th><th>العميل</th><th>المبلغ</th><th>الحالة</th><th>الوقت</th></tr>
                </thead>
                <tbody>
                    <?php foreach ($recent as $o): 
                        $status = $o['status'];
                        $color = $status=='pending'?'warning':($status=='accepted'?'info':($status=='on_the_way'?'primary':'success'));
                        $text = $status=='pending'?'معلق':($status=='accepted'?'مقبول':($status=='on_the_way'?'في الطريق':'مكتمل'));
                    ?>
                    <tr>
                        <td>#<?php echo $o['id']; ?></td>
                        <td><?php echo e($o['customer']); ?></td>
                        <td><?php echo number_format($o['total'],2); ?> ريال</td>
                        <td><span class="badge bg-<?php echo $color; ?>"><?php echo $text; ?></span></td>
                        <td><?php echo date('H:i', strtotime($o['created_at'])); ?></td>
                    </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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