<?php
session_start();

if (!isset($_SESSION['admin_id'])) {
    header("Location: login.php");
    exit();
}

include "../config/db.php";

// Get statistics
$total_products = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM products WHERE (is_deleted = 0 OR is_deleted IS NULL)"))['count'];
$total_orders = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM orders"))['count'];
$total_users = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM users WHERE (is_deleted = 0 OR is_deleted IS NULL)"))['count'];
$total_revenue = mysqli_fetch_assoc(mysqli_query($conn, "SELECT SUM(total_price) as sum FROM orders WHERE status='Delivered'"))['sum'];
$total_inuse = mysqli_fetch_assoc(mysqli_query($conn, "SELECT COUNT(*) as count FROM orders WHERE status='In Use'"))['count'];
$recent_orders = mysqli_query($conn, "SELECT orders.*, users.name FROM orders JOIN users ON orders.user_id = users.id ORDER BY id DESC LIMIT 5");
?>

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Admin Dashboard - ShopNest</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
    <link href="../assets/css/style.css" rel="stylesheet">
    <style>
        /* Replaced sidebar CSS */
        .main-content {
            margin-left: 220px;
            padding: 30px;
            background: #f8f9fa;
            min-height: 100vh;
        }
        .stat-card {
            background: white;
            border-radius: 15px;
            padding: 25px;
            text-align: center;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
            transition: all 0.3s;
            height: 100%;
            border-top: 4px solid;
        }
        .stat-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 10px 25px rgba(0,0,0,0.1);
        }
        .stat-icon {
            font-size: 2.5rem;
            margin-bottom: 15px;
        }
        .stat-card.products { border-color: #3498db; }
        .stat-card.orders { border-color: #2ecc71; }
        .stat-card.users { border-color: #9b59b6; }
        .stat-card.revenue { border-color: #e74c3c; }
        .stat-card.products .stat-icon { color: #3498db; }
        .stat-card.orders .stat-icon { color: #2ecc71; }
        .stat-card.users .stat-icon { color: #9b59b6; }
        .stat-card.revenue .stat-icon { color: #e74c3c; }
        .stat-card.inuse { border-color: #fd7e14; }
        .stat-card.inuse .stat-icon { color: #fd7e14; }
        .recent-orders {
            background: white;
            border-radius: 15px;
            padding: 25px;
            margin-top: 30px;
            box-shadow: 0 5px 15px rgba(0,0,0,0.05);
        }
        .status-badge {
            padding: 5px 15px;
            border-radius: 20px;
            font-size: 0.8rem;
            font-weight: 600;
        }
        .status-paid { background: #d4edda; color: #155724; }
        .status-shipped { background: #fff3cd; color: #856404; }
        .status-delivered { background: #cce5ff; color: #004085; }
        .status-returned { background: #f3e5f5; color: #7b1fa2; }
        .status-inuse { background: #fff4e5; color: #d9480f; }
    </style>
</head>
<body>
    <!-- Sidebar -->
    <?php include "includes/sidebar.php"; ?>

    <div class="main-content">
        <div class="d-flex justify-content-between align-items-center mb-4">
            <h1 style="color: var(--dark-blue);">
                <i class="fas fa-tachometer-alt me-2"></i>Admin Dashboard
            </h1>
            <div class="text-muted">
                <?php echo date('F j, Y'); ?>
            </div>
        </div>

        <!-- Statistics Cards -->
        <div class="row g-4 mb-4">
            <div class="col-md-3">
                <div class="stat-card products">
                    <div class="stat-icon">
                        <i class="fas fa-box"></i>
                    </div>
                    <h3><?php echo $total_products; ?></h3>
                    <p>Total Products</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card orders">
                    <div class="stat-icon">
                        <i class="fas fa-shopping-cart"></i>
                    </div>
                    <h3><?php echo $total_orders; ?></h3>
                    <p>Total Orders</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card users">
                    <div class="stat-icon">
                        <i class="fas fa-users"></i>
                    </div>
                    <h3><?php echo $total_users; ?></h3>
                    <p>Total Users</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card revenue">
                    <div class="stat-icon">
                        <i class="fas fa-rupee-sign"></i>
                    </div>
                    <h3>₹<?php echo number_format($total_revenue ?: 0, 2); ?></h3>
                    <p>Total Revenue</p>
                </div>
            </div>
            <div class="col-md-3">
                <div class="stat-card inuse">
                    <div class="stat-icon">
                        <i class="fas fa-tshirt"></i>
                    </div>
                    <h3><?php echo $total_inuse; ?></h3>
                    <p>In Use Items</p>
                </div>
            </div>
        </div>

        <!-- Recent Orders -->
        <div class="recent-orders">
            <h4 class="mb-4" style="color: var(--dark-blue);">
                <i class="fas fa-clock me-2"></i>Recent Orders
            </h4>
            
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th>Order ID</th>
                            <th>Customer</th>
                            <th>Amount</th>
                            <th>Date</th>
                            <th>Status</th>
                            <th>Action</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php while($order = mysqli_fetch_assoc($recent_orders)): ?>
                        <tr>
                            <td>#<?php echo str_pad($order['id'], 6, '0', STR_PAD_LEFT); ?></td>
                            <td><?php echo htmlspecialchars($order['name']); ?></td>
                            <td class="fw-bold">₹<?php echo number_format($order['total_price'], 2); ?></td>
                            <td>
                                <?php 
                                // Use created_at instead of order_date
                                echo isset($order['created_at']) ? date('M d, Y', strtotime($order['created_at'])) : 
                                    (isset($order['order_date']) ? date('M d, Y', strtotime($order['order_date'])) : 'N/A');
                                ?>
                            </td>
                            <td>
                                <?php 
                                $status_class = '';
                                if($order['status'] == 'Paid') $status_class = 'status-paid';
                                if($order['status'] == 'Shipped') $status_class = 'status-shipped';
                                if($order['status'] == 'Delivered') $status_class = 'status-delivered';
                                if($order['status'] == 'Returned') $status_class = 'status-returned';
                                if($order['status'] == 'In Use') $status_class = 'status-inuse';
                                if($order['status'] == 'Cancelled') $status_class = 'status-cancelled';
                                ?>
                                <span class="status-badge <?php echo $status_class; ?>">
                                    <?php echo $order['status'] ?? 'Pending'; ?>
                                </span>
                            </td>
                            <td>
                                <a href="view-order.php?id=<?php echo $order['id']; ?>" 
                                   class="btn btn-sm btn-primary">
                                    <i class="fas fa-eye"></i>
                                </a>
                            </td>
                        </tr>
                        <?php endwhile; ?>
                    </tbody>
                </table>
            </div>
            
            <div class="text-center mt-3">
                <a href="orders.php" class="btn btn-orange">
                    <i class="fas fa-list me-2"></i>View All Orders
                </a>
            </div>
        </div>

        <!-- Quick Actions -->
        <div class="row mt-4">
            <div class="col-md-12">
                <div class="dashboard-card">
                    <h4 class="mb-4" style="color: var(--dark-blue);">
                        <i class="fas fa-bolt me-2"></i>Quick Actions
                    </h4>
                    <div class="row g-3">
                        <div class="col-md-3">
                            <a href="add-product.php" class="btn btn-primary w-100">
                                <i class="fas fa-plus me-2"></i>Add Product
                            </a>
                        </div>
                        <div class="col-md-3">
                            <a href="orders.php" class="btn btn-success w-100">
                                <i class="fas fa-shopping-cart me-2"></i>Manage Orders
                            </a>
                        </div>
                        <div class="col-md-3">
                            <a href="add-category.php" class="btn btn-warning w-100">
                                <i class="fas fa-tags me-2"></i>Add Category
                            </a>
                        </div>
                        <div class="col-md-3">
                            <a href="reports.php" class="btn btn-info w-100">
                                <i class="fas fa-chart-bar me-2"></i>View Reports
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
    <script>
        // Auto-refresh dashboard every 60 seconds
        setTimeout(function() {
            window.location.reload();
        }, 60000);
    </script>
</body>
</html>