<?php
if (!defined('BASE_URL')) { die('Direct access not permitted.'); }

$db     = getDBConnection();
$uid    = (int)getSession('user_id');
$today  = date('Y-m-d');

// My counts
$totalSolar = (int)safeDbVal($db, "SELECT COUNT(*) FROM readings WHERE user_id=? AND reading_type='solar'", [$uid]);
$totalKcc   = (int)safeDbVal($db, "SELECT COUNT(*) FROM readings WHERE user_id=? AND reading_type='kcc'", [$uid]);
$totalBills = (int)safeDbVal($db, "SELECT COUNT(*) FROM bills WHERE user_id=?", [$uid]);
$todaySolar = (int)safeDbVal($db, "SELECT COUNT(*) FROM readings WHERE user_id=? AND reading_type='solar' AND reading_date=?", [$uid,$today]);
$todayKcc   = (int)safeDbVal($db, "SELECT COUNT(*) FROM readings WHERE user_id=? AND reading_type='kcc' AND reading_date=?", [$uid,$today]);
$todayBills = (int)safeDbVal($db, "SELECT COUNT(*) FROM bills WHERE user_id=? AND DATE(created_at)=?", [$uid,$today]);

// Recent activity
$recentReadings = dbRows($db,
    "SELECT r.*, c.consumer_name, c.meter_number
     FROM readings r JOIN consumers c ON c.id=r.consumer_id
     WHERE r.user_id=? ORDER BY r.created_at DESC LIMIT 5", [$uid]);

$pageTitle = 'Dashboard';
include __DIR__ . '/../../includes/header.php';
?>

<!-- Page Header -->
<div class="page-header">
    <div>
        <h1><i class="bi bi-speedometer2 me-2 text-primary"></i>Dashboard</h1>
        <p class="text-muted mb-0" style="font-size:.85rem;">Welcome back, <strong><?= h(getSession('full_name')) ?></strong></p>
    </div>
    <div class="text-muted small"><?= date('l, d M Y') ?></div>
</div>

<!-- ── Quick Action Cards ──────────────────────────────────── -->
<div class="row g-3 mb-4">
    <div class="col-12">
        <p class="fw-bold text-uppercase" style="font-size:.72rem;letter-spacing:1px;color:var(--erp-text-muted);">Quick Actions</p>
    </div>

    <!-- Solar -->
    <div class="col-12 col-md-4">
        <a href="<?= BASE_URL ?>/modules/readings/add.php?type=solar" class="erp-action-card text-decoration-none">
            <div class="erp-action-icon" style="background:#fff3cd;color:#e65100;">
                <i class="bi bi-sun-fill"></i>
            </div>
            <div class="erp-action-body">
                <h3>Solar Reading</h3>
                <p>Capture solar meter MRI</p>
                <span class="erp-action-badge" style="background:#fff3cd;color:#e65100;">
                    Today: <?= $todaySolar ?>
                </span>
            </div>
            <i class="bi bi-chevron-right erp-action-arrow"></i>
        </a>
    </div>

    <!-- KCC -->
    <div class="col-12 col-md-4">
        <a href="<?= BASE_URL ?>/modules/readings/add.php?type=kcc" class="erp-action-card text-decoration-none">
            <div class="erp-action-icon" style="background:#e3f2fd;color:#1565c0;">
                <i class="bi bi-lightning-fill"></i>
            </div>
            <div class="erp-action-body">
                <h3>KCC Reading</h3>
                <p>Capture KCC meter MRI</p>
                <span class="erp-action-badge" style="background:#e3f2fd;color:#1565c0;">
                    Today: <?= $todayKcc ?>
                </span>
            </div>
            <i class="bi bi-chevron-right erp-action-arrow"></i>
        </a>
    </div>

    <!-- Bill Distribution -->
    <div class="col-12 col-md-4">
        <a href="<?= BASE_URL ?>/modules/bills/add.php" class="erp-action-card text-decoration-none">
            <div class="erp-action-icon" style="background:#e8f5e9;color:#2e7d32;">
                <i class="bi bi-receipt-cutoff"></i>
            </div>
            <div class="erp-action-body">
                <h3>Bill Distribution</h3>
                <p>Record bill delivery</p>
                <span class="erp-action-badge" style="background:#e8f5e9;color:#2e7d32;">
                    Today: <?= $todayBills ?>
                </span>
            </div>
            <i class="bi bi-chevron-right erp-action-arrow"></i>
        </a>
    </div>
</div>

<!-- ── Stats ─────────────────────────────────────────────────── -->
<div class="row g-3 mb-4">
    <div class="col-12">
        <p class="fw-bold text-uppercase" style="font-size:.72rem;letter-spacing:1px;color:var(--erp-text-muted);">My Overall Stats</p>
    </div>
    <div class="col-4">
        <div class="stat-card">
            <div class="stat-card-icon orange"><i class="bi bi-sun-fill"></i></div>
            <div>
                <h3>Solar</h3>
                <p><?= $totalSolar ?></p>
            </div>
        </div>
    </div>
    <div class="col-4">
        <div class="stat-card">
            <div class="stat-card-icon blue"><i class="bi bi-lightning-fill"></i></div>
            <div>
                <h3>KCC</h3>
                <p><?= $totalKcc ?></p>
            </div>
        </div>
    </div>
    <div class="col-4">
        <div class="stat-card">
            <div class="stat-card-icon green"><i class="bi bi-receipt"></i></div>
            <div>
                <h3>Bills</h3>
                <p><?= $totalBills ?></p>
            </div>
        </div>
    </div>
</div>

<!-- ── Recent Readings ──────────────────────────────────────── -->
<?php if (!empty($recentReadings)): ?>
<div class="erp-card">
    <div class="d-flex align-items-center justify-content-between mb-3">
        <h6 class="fw-bold mb-0">Recent Readings</h6>
        <a href="<?= BASE_URL ?>/modules/readings/index.php" class="btn btn-sm btn-outline-secondary" style="font-size:.78rem;">View All</a>
    </div>
    <div class="table-responsive">
        <table class="table table-hover mb-0" style="font-size:.84rem;">
            <thead class="table-light">
                <tr>
                    <th>Consumer</th>
                    <th>Type</th>
                    <th>Status</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($recentReadings as $r): ?>
                <tr>
                    <td>
                        <div class="fw-semibold"><?= h($r['consumer_name']) ?></div>
                        <small class="text-muted"><?= h($r['meter_number']) ?></small>
                    </td>
                    <td><?= typeBadge($r['reading_type']) ?></td>
                    <td><?= statusBadge($r['status']) ?></td>
                    <td class="text-muted" style="font-size:.78rem;"><?= formatDt($r['created_at']) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php else: ?>
<div class="erp-card text-center py-5 text-muted">
    <i class="bi bi-clipboard-data" style="font-size:2.5rem;opacity:.3;"></i>
    <p class="mt-2">No readings yet. Start by selecting an action above.</p>
</div>
<?php endif; ?>

<?php include __DIR__ . '/../../includes/footer.php'; ?>
