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

// Fetch some metrics
try {
    // 1. Total Products
    $stmt = $pdo->query("SELECT COUNT(*) FROM products");
    $total_products = $stmt->fetchColumn();

    // 2. Total Categories
    $stmt = $pdo->query("SELECT COUNT(*) FROM categories");
    $total_categories = $stmt->fetchColumn();

    // 3. Total Orders
    $stmt = $pdo->query("SELECT COUNT(*) FROM orders");
    $total_orders = $stmt->fetchColumn();

    // 4. Total Sales
    $stmt = $pdo->query("SELECT SUM(total_amount) FROM orders WHERE status != 'cancelled'");
    $total_sales = $stmt->fetchColumn() ?: 0.00;

    // 5. Recent Orders
    $stmt = $pdo->query("SELECT o.*, u.full_name FROM orders o LEFT JOIN users u ON o.user_id = u.id ORDER BY o.id DESC LIMIT 5");
    $recent_orders = $stmt->fetchAll();

} catch (Exception $e) {
    $total_products = 0;
    $total_categories = 0;
    $total_orders = 0;
    $total_sales = 0;
    $recent_orders = [];
}
?>

<div class="row mb-4">
    <div class="col-12">
        <h2 class="text-white mb-2">Welcome Back, <?php echo htmlspecialchars($_SESSION['admin_name']); ?></h2>
        <p class="text-muted small">Atelier Desk &bull; Live Platform Overview &bull; <?php echo date('F d, Y - l'); ?></p>
    </div>
</div>

<!-- Metrics Row -->
<div class="row g-4 mb-5">
    <!-- Total Sales -->
    <div class="col-xl-3 col-sm-6">
        <div class="admin-card d-flex align-items-center justify-content-between">
            <div>
                <span class="text-muted text-uppercase small fw-bold tracking-wider" style="font-size: 0.65rem; letter-spacing: 1.5px;">Gross Revenue</span>
                <h3 class="text-warning mt-1 mb-0">LKR <?php echo number_format($total_sales, 2); ?></h3>
            </div>
            <div class="fs-2 text-muted opacity-50"><i class="fa-solid fa-coins"></i></div>
        </div>
    </div>
    
    <!-- Total Orders -->
    <div class="col-xl-3 col-sm-6">
        <div class="admin-card d-flex align-items-center justify-content-between">
            <div>
                <span class="text-muted text-uppercase small fw-bold tracking-wider" style="font-size: 0.65rem; letter-spacing: 1.5px;">Total Orders</span>
                <h3 class="text-white mt-1 mb-0"><?php echo $total_orders; ?></h3>
            </div>
            <div class="fs-2 text-muted opacity-50"><i class="fa-solid fa-receipt"></i></div>
        </div>
    </div>

    <!-- Total Products -->
    <div class="col-xl-3 col-sm-6">
        <div class="admin-card d-flex align-items-center justify-content-between">
            <div>
                <span class="text-muted text-uppercase small fw-bold tracking-wider" style="font-size: 0.65rem; letter-spacing: 1.5px;">Catalog Products</span>
                <h3 class="text-white mt-1 mb-0"><?php echo $total_products; ?></h3>
            </div>
            <div class="fs-2 text-muted opacity-50"><i class="fa-solid fa-bag-shopping"></i></div>
        </div>
    </div>

    <!-- Total Categories -->
    <div class="col-xl-3 col-sm-6">
        <div class="admin-card d-flex align-items-center justify-content-between">
            <div>
                <span class="text-muted text-uppercase small fw-bold tracking-wider" style="font-size: 0.65rem; letter-spacing: 1.5px;">Catalog Categories</span>
                <h3 class="text-white mt-1 mb-0"><?php echo $total_categories; ?></h3>
            </div>
            <div class="fs-2 text-muted opacity-50"><i class="fa-solid fa-folder-open"></i></div>
        </div>
    </div>
</div>

<div class="row g-4">
    <!-- Left: Recent Orders -->
    <div class="col-lg-8">
        <div class="admin-card h-100">
            <h4 class="text-white mb-4"><i class="fa-solid fa-chart-line text-warning me-2"></i> Recent Platform Orders</h4>
            
            <?php if (empty($recent_orders)): ?>
                <div class="text-center py-5 text-muted">
                    <i class="fa-solid fa-receipt display-4 mb-3 d-block opacity-25"></i>
                    No purchases have been recorded yet.
                </div>
            <?php else: ?>
                <div class="table-responsive">
                    <table class="table table-dark table-luxury">
                        <thead>
                            <tr>
                                <th>Order Ref</th>
                                <th>Patron</th>
                                <th>Method</th>
                                <th>Fulfillment</th>
                                <th class="text-end">Amount</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($recent_orders as $ord): ?>
                                <tr>
                                    <td><strong class="text-warning">#MNDIRECT-<?php echo str_pad($ord['id'], 6, '0', STR_PAD_LEFT); ?></strong></td>
                                    <td><?php echo htmlspecialchars($ord['full_name'] ?? 'Guest'); ?></td>
                                    <td><span class="badge bg-dark border border-secondary text-white"><?php echo htmlspecialchars($ord['payment_method']); ?></span></td>
                                    <td>
                                        <?php 
                                        $badge_class = 'bg-secondary';
                                        if ($ord['status'] === 'pending') $badge_class = 'bg-danger';
                                        elseif ($ord['status'] === 'processing') $badge_class = 'bg-warning text-black';
                                        elseif ($ord['status'] === 'shipped') $badge_class = 'bg-info';
                                        elseif ($ord['status'] === 'delivered') $badge_class = 'bg-success';
                                        ?>
                                        <span class="badge <?php echo $badge_class; ?> text-uppercase" style="font-size: 0.65rem; letter-spacing: 0.5px;">
                                            <?php echo htmlspecialchars($ord['status']); ?>
                                        </span>
                                    </td>
                                    <td class="text-end fw-bold">LKR <?php echo number_format($ord['total_amount'], 2); ?></td>
                                </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            <?php endif; ?>
        </div>
    </div>

    <!-- Right: Administrative Shortcuts -->
    <div class="col-lg-4">
        <div class="admin-card h-100">
            <h4 class="text-white mb-4"><i class="fa-solid fa-sliders text-warning me-2"></i> Catalog Shortcuts</h4>
            <p class="text-muted small">Select any action below to quickly add new luxury assets or refine existings.</p>
            
            <div class="d-flex flex-column gap-3 mt-4">
                <a href="product_add.php" class="btn-luxury py-3 w-100 justify-content-center">
                    <i class="fa-solid fa-plus-circle me-2"></i> Add New Product
                </a>
                <a href="category_add.php" class="btn-outline-luxury py-3 w-100 justify-content-center">
                    <i class="fa-solid fa-folder-plus me-2"></i> Add New Category
                </a>
                <a href="products.php" class="btn btn-outline-secondary py-3 w-100 text-uppercase tracking-wider fw-bold text-white border-secondary rounded-pill" style="font-size: 0.7rem; letter-spacing: 1.5px;">
                    <i class="fa-solid fa-boxes-stacked me-2"></i> Manage Catalog
                </a>
                <a href="categories.php" class="btn btn-outline-secondary py-3 w-100 text-uppercase tracking-wider fw-bold text-white border-secondary rounded-pill" style="font-size: 0.7rem; letter-spacing: 1.5px;">
                    <i class="fa-solid fa-folder me-2"></i> Manage Categories
                </a>
            </div>
        </div>
    </div>
</div>

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