<?php
require_once __DIR__ . '/../includes/auth.php';
require_owner_or_admin();
require_once __DIR__ . '/../includes/header_admin.php';
$pdo = require __DIR__ . '/../config/pdo.php';

$totalProducts = (int) $pdo->query('SELECT COUNT(*) FROM products')->fetchColumn();
$lowStock = (int) $pdo->query('SELECT COUNT(*) FROM products WHERE stock <= 5')->fetchColumn();
$pendingOrders = (int) $pdo->query("SELECT COUNT(*) FROM orders WHERE status = 'pending'")->fetchColumn();

$monthStart = date('Y-m-01 00:00:00');
$monthEnd = date('Y-m-t 23:59:59');
$revenueStmt = $pdo->prepare("SELECT COALESCE(SUM(total), 0) FROM orders WHERE status IN ('paid','packed','shipped','done') AND created_at BETWEEN ? AND ?");
$revenueStmt->execute([$monthStart, $monthEnd]);
$revenue = (float) $revenueStmt->fetchColumn();
?>

<h2 class="fw-bold mb-4">Dashboard</h2>

<div class="row g-4">
    <div class="col-md-3">
        <div class="card shadow-sm">
            <div class="card-body">
                <p class="text-muted mb-1">Total Produk</p>
                <h3 class="fw-bold mb-0"><?= $totalProducts; ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card shadow-sm">
            <div class="card-body">
                <p class="text-muted mb-1">Stok Menipis</p>
                <h3 class="fw-bold mb-0"><?= $lowStock; ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card shadow-sm">
            <div class="card-body">
                <p class="text-muted mb-1">Order Pending</p>
                <h3 class="fw-bold mb-0"><?= $pendingOrders; ?></h3>
            </div>
        </div>
    </div>
    <div class="col-md-3">
        <div class="card shadow-sm">
            <div class="card-body">
                <p class="text-muted mb-1">Omzet Bulan Ini</p>
                <h3 class="fw-bold mb-0"><?= format_currency($revenue); ?></h3>
            </div>
        </div>
    </div>
</div>

<?php require_once __DIR__ . '/../includes/footer_admin.php'; ?>
