<?php
require_once __DIR__ . '/../config.php';
require_once __DIR__ . '/../includes/product.php';
require_once __DIR__ . '/../includes/order.php';

// Check if user is admin
if (!isAdmin()) {
    header('Location: ../login.php?redirect=admin/');
    exit;
}

$pageTitle = 'Admin Dashboard';
$productModel = new Product();
$orderModel = new Order();

// Get statistics
$totalProducts = $productModel->getCount();
$totalOrders = $orderModel->getCount();
$revenue = $orderModel->getRevenue();
$recentOrders = $orderModel->getRecent(5);

require_once __DIR__ . '/admin_header.php';
?>

<!-- Admin Dashboard -->
<div class="admin-header">
    <h1>Dashboard</h1>
    <a href="<?php echo SITE_URL; ?>/" target="_blank" class="btn btn-secondary">View Website</a>
</div>

<!-- Stats -->
<div class="stats-grid">
    <div class="stat-card" onclick="window.location.href='products.php'" style="cursor: pointer;">
        <div class="stat-value"><?php echo $totalProducts; ?></div>
        <div class="stat-label" href="products.php">Products</div>
    </div>
    <div class="stat-card" onclick="window.location.href='orders.php'" style="cursor: pointer;">
        <div class="stat-value"><?php echo $totalOrders; ?></div>
        <div class="stat-label">Orders</div>
    </div>
    <div class="stat-card" onclick="window.location.href='total_revenue.php'" style="cursor: pointer;">
        <div class="stat-value"><?php echo formatPrice($revenue); ?></div>
        <div class="stat-label">Total Revenue</div>
    </div>
    <div class="stat-card">
        <div class="stat-value"><?php echo $orderModel->getCount('pending'); ?></div>
        <div class="stat-label">Pending Orders</div>
    </div>
</div>

<!-- Recent Orders -->
<div class="admin-card">
    <div class="admin-card-header">
        <h2>Recent Orders</h2>
        <a href="orders.php" class="btn btn-small">View All</a>
    </div>
    
    <table class="admin-table">
        <thead>
            <tr>
                <th>Order #</th>
                <th>Customer</th>
                <th>Amount</th>
                <th>Status</th>
                <th>Date</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            <?php foreach ($recentOrders as $order): ?>
                <tr>
                    <td><?php echo htmlspecialchars($order['order_number']); ?></td>
                    <td><?php echo htmlspecialchars($order['full_name'] ?? 'Guest'); ?></td>
                    <td><?php echo formatPrice($order['total_amount']); ?></td>
                    <td><span class="status-badge status-<?php echo $order['status']; ?>"><?php echo htmlspecialchars($order['status']); ?></span></td>
                    <td><?php echo date('M j, Y', strtotime($order['created_at'])); ?></td>
                    <td><a href="order.php?id=<?php echo $order['id']; ?>">View</a></td>
                </tr>
            <?php endforeach; ?>
        </tbody>
    </table>
</div>

<?php require_once __DIR__ . '/admin_footer.php'; ?>
