<?php
require_once 'admin_config.php';
requireAdminLogin();

$total_products = getTotalProducts($pdo);
$total_orders = getTotalOrders($pdo);
$total_customers = getTotalCustomers($pdo);
$total_revenue = getTotalRevenue($pdo);
$pending_orders = getPendingOrders($pdo);

// Get recent orders
$stmt = $pdo->query("SELECT o.*, c.full_name FROM orders o JOIN customers c ON o.customer_id = c.id ORDER BY o.order_date DESC LIMIT 5");
$recent_orders = $stmt->fetchAll();

// Get low stock products
$stmt = $pdo->query("SELECT * FROM equipment WHERE stock_quantity < 5 ORDER BY stock_quantity ASC LIMIT 5");
$low_stock = $stmt->fetchAll();
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Dashboard | EliteEquip Admin</title>
    <link href="https://fonts.googleapis.com/css2?family=Inter:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="admin_style.css" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
</head>
<body>
<div class="admin-container">
    <?php include 'sidebar.php'; ?>
    
    <div class="main-content">
        <?php include 'topbar.php'; ?>
        
        <!-- Stats Cards -->
        <div class="stats-grid">
            <div class="stat-card">
                <div class="stat-icon" style="background: rgba(16,185,129,0.1);">
                    <i class="fas fa-box" style="color: #10b981;"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $total_products; ?></h3>
                    <p>Total Products</p>
                </div>
            </div>

            <div class="stat-card" onclick="window.location.href='bank_details.php'" style="cursor: pointer;">
    <div class="stat-icon" style="background: rgba(255,107,53,0.1);">
        <i class="fas fa-university" style="color: #ff6b35;"></i>
    </div>
    <div class="stat-info">
        <h3>Bank Details</h3>
        <p>Manage payment info</p>
    </div>
</div>
            
            <div class="stat-card">
                <div class="stat-icon" style="background: rgba(59,130,246,0.1);">
                    <i class="fas fa-shopping-cart" style="color: #3b82f6;"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $total_orders; ?></h3>
                    <p>Total Orders</p>
                </div>
            </div>
            
            <div class="stat-card">
                <div class="stat-icon" style="background: rgba(245,158,11,0.1);">
                    <i class="fas fa-users" style="color: #f59e0b;"></i>
                </div>
                <div class="stat-info">
                    <h3><?php echo $total_customers; ?></h3>
                    <p>Total Customers</p>
                </div>
            </div>
            
            <div class="stat-card">
                <div class="stat-icon" style="background: rgba(255,107,53,0.1);">
                    <i class="fas fa-dollar-sign" style="color: #ff6b35;"></i>
                </div>
                <div class="stat-info">
                    <h3>$<?php echo number_format($total_revenue, 2); ?></h3>
                    <p>Total Revenue</p>
                </div>
            </div>
        </div>
        
        <div class="row" style="display: grid; grid-template-columns: repeat(auto-fit, minmax(400px, 1fr)); gap: 20px; margin-bottom: 30px;">
            <div class="table-container">
                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
                    <h3>Recent Orders</h3>
                    <a href="orders.php" class="btn-primary" style="padding: 5px 15px; font-size: 0.8rem;">View All</a>
                </div>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Order #</th>
                            <th>Customer</th>
                            <th>Amount</th>
                            <th>Status</th>
                            <th>Date</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($recent_orders as $order): ?>
                        <tr>
                            <td><?php echo $order['invoice_number']; ?></td>
                            <td><?php echo htmlspecialchars($order['full_name']); ?></td>
                            <td>$<?php echo number_format($order['total_amount'], 2); ?></td>
                            <td><span class="status-badge status-<?php echo $order['payment_status']; ?>"><?php echo $order['payment_status']; ?></span></td>
                            <td><?php echo date('M d, Y', strtotime($order['order_date'])); ?></td>
                        </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
            
            <div class="table-container">
                <div style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 20px;">
                    <h3>Low Stock Alert</h3>
                    <a href="products.php" class="btn-primary" style="padding: 5px 15px; font-size: 0.8rem;">Manage Stock</a>
                </div>
                <table class="data-table">
                    <thead>
                        <tr>
                            <th>Product</th>
                            <th>Stock</th>
                            <th>Status</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($low_stock as $product): ?>
                        <tr>
                            <td><?php echo htmlspecialchars($product['name']); ?></td>
                            <td><?php echo $product['stock_quantity']; ?></td>
                            <td><span class="status-badge status-pending">Low Stock</span></td>
                        </tr>
                        <?php endforeach; ?>
                        <?php if (empty($low_stock)): ?>
                        <tr><td colspan="3" style="text-align: center;">All products have sufficient stock</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
        </div>
        
        <!-- Chart Section -->
        <div class="table-container">
            <h3 style="margin-bottom: 20px;">Revenue Overview</h3>
            <canvas id="revenueChart" style="max-height: 300px;"></canvas>
        </div>
    </div>
</div>

<script>
// Revenue Chart
const ctx = document.getElementById('revenueChart').getContext('2d');
new Chart(ctx, {
    type: 'line',
    data: {
        labels: ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun'],
        datasets: [{
            label: 'Revenue ($)',
            data: [12500, 15000, 18000, 22000, 25000, 30000],
            borderColor: '#ff6b35',
            backgroundColor: 'rgba(255,107,53,0.1)',
            tension: 0.4,
            fill: true
        }]
    },
    options: {
        responsive: true,
        maintainAspectRatio: true,
        plugins: {
            legend: {
                position: 'top',
            }
        }
    }
});
</script>
<script src="admin_script.js"></script>
</body>
</html>