<?php
// Check if user is logged in and is admin using the new function
require_once '../includes/require_admin.php';

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

// Get statistics
$total_products = count(getAllProducts($pdo));
$total_users_stmt = $pdo->query("SELECT COUNT(*) as count FROM users");
$total_users = $total_users_stmt->fetch(PDO::FETCH_ASSOC)['count'];

$total_orders_stmt = $pdo->query("SELECT COUNT(*) as count FROM orders");
$total_orders = $total_orders_stmt->fetch(PDO::FETCH_ASSOC)['count'];

$total_revenue_stmt = $pdo->query("SELECT SUM(total_amount) as total FROM orders");
$total_revenue = $total_revenue_stmt->fetch(PDO::FETCH_ASSOC)['total'] ?? 0;
?>

<div class="container">
    <div class="admin-header">
        <h2>Admin Dashboard</h2>
        <a href="<?php echo SITE_URL; ?>/admin/add_product.php" class="btn btn-primary">Add New Product</a>
    </div>
    
    <div class="dashboard-stats">
        <div class="stat-card">
            <h3>Total Products</h3>
            <div class="stat-value"><?php echo $total_products; ?></div>
        </div>
        
        <div class="stat-card">
            <h3>Total Users</h3>
            <div class="stat-value"><?php echo $total_users; ?></div>
        </div>
        
        <div class="stat-card">
            <h3>Total Orders</h3>
            <div class="stat-value"><?php echo $total_orders; ?></div>
        </div>
        
        <div class="stat-card">
            <h3>Total Revenue</h3>
            <div class="stat-value">$<?php echo number_format($total_revenue, 2); ?></div>
        </div>
    </div>
    
    <div class="admin-nav">
        <ul>
            <li><a href="<?php echo SITE_URL; ?>/admin/index.php" class="active">Dashboard</a></li>
            <li><a href="<?php echo SITE_URL; ?>/admin/products.php">Products</a></li>
            <li><a href="<?php echo SITE_URL; ?>/admin/manage_users.php">Users</a></li>
            <li><a href="<?php echo SITE_URL; ?>/admin/orders.php">Orders</a></li>
            <li><a href="<?php echo SITE_URL; ?>/tickets/admin_dashboard.php">Ticket Analytics</a></li>
            <li><a href="<?php echo SITE_URL; ?>/admin/marketing.php">Marketing</a></li>
        </ul>
    </div>
    
    <div class="section-title">
        <h2>Recent Products</h2>
    </div>
    
    <div class="products-grid">
        <?php 
        $recent_products = array_slice(getAllProducts($pdo), 0, 4);
        foreach ($recent_products as $product): ?>
            <div class="product-card" data-product-id="<?php echo $product['id']; ?>">
                <div class="product-image">
                    <?php if (!empty($product['image'])): ?>
                        <img src="<?php echo SITE_URL . '/' . htmlspecialchars($product['image']); ?>" alt="<?php echo htmlspecialchars($product['name']); ?>">
                    <?php else: ?>
                        <i class="fas fa-image" style="font-size: 3rem; color: #ccc;"></i>
                    <?php endif; ?>
                </div>
                <div class="product-info">
                    <h3><?php echo htmlspecialchars($product['name']); ?></h3>
                    <div class="product-price">$<?php echo number_format($product['price'], 2); ?></div>
                    <a href="<?php echo SITE_URL; ?>/admin/edit_product.php?id=<?php echo $product['id']; ?>" class="btn btn-outline btn-block">Edit</a>
                </div>
            </div>
        <?php endforeach; ?>
    </div>
</div>

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