<?php
require_once __DIR__ . '/../includes/auth.php';
requireLogin();
require_once __DIR__ . '/../config/database.php';

/* =============================
   DASHBOARD STATS
============================= */

$totalMeters = $pdo->query("SELECT COUNT(*) FROM meters")->fetchColumn();
$inStock = $pdo->query("SELECT COUNT(*) FROM meters WHERE status='IN_STOCK'")->fetchColumn();
$withDistributor = $pdo->query("SELECT COUNT(*) FROM meters WHERE status='WITH_DISTRIBUTOR'")->fetchColumn();
$installed = $pdo->query("SELECT COUNT(*) FROM meters WHERE status='INSTALLED'")->fetchColumn();
$pendingRegistrations = $pdo->query("SELECT COUNT(*) FROM meter_registrations WHERE status='SUBMITTED'")->fetchColumn();
$pendingReplacements = $pdo->query("SELECT COUNT(*) FROM replacement_requests WHERE status='PENDING'")->fetchColumn();

/* ===== LOAD MASTER LAYOUT ===== */
require_once __DIR__ . '/../includes/layout.php';
?>

    <div class="page-header">
        <h2>Welcome, <?= htmlspecialchars($_SESSION['full_name'] ?? 'User') ?></h2>
        <p class="page-subtitle">
            <?php echo SYSTEM_NAME; ?> • <?php echo SYSTEM_TAGLINE; ?>
        </p>
    </div>

    <div class="dashboard-grid">

        <div class="stat-card orange">
            <h4>Total Meters</h4>
            <div class="number"><?= $totalMeters ?></div>
        </div>

        <div class="stat-card green">
            <h4>In Stock</h4>
            <div class="number"><?= $inStock ?></div>
        </div>

        <div class="stat-card blue">
            <h4>With Distributor</h4>
            <div class="number"><?= $withDistributor ?></div>
        </div>

        <div class="stat-card red">
            <h4>Installed</h4>
            <div class="number"><?= $installed ?></div>
        </div>

        <div class="stat-card orange">
            <h4>Pending Registrations</h4>
            <div class="number"><?= $pendingRegistrations ?></div>
        </div>

        <div class="stat-card red">
            <h4>Pending Replacements</h4>
            <div class="number"><?= $pendingReplacements ?></div>
        </div>

    </div>

</div>

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

