<?php
require_once __DIR__ . '/../../includes/init.php';
middleware(['mr']);

$db  = getDBConnection();
$uid = (int)getSession('user_id');

$filterDate = $_GET['date'] ?? '';
$where  = "WHERE b.user_id = ?";
$params = [$uid];
if ($filterDate) { $where .= " AND DATE(b.created_at) = ?"; $params[] = $filterDate; }

$bills = dbRows($db,
    "SELECT b.* FROM bills b {$where} ORDER BY b.created_at DESC LIMIT 100",
    $params);

$totalBills = (int)safeDbVal($db, "SELECT COUNT(*) FROM bills WHERE user_id=?", [$uid]);

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

<div class="page-header">
    <div>
        <h1><i class="bi bi-collection me-2 text-success"></i>My Bills</h1>
    </div>
    <span class="badge bg-success p-2">Total: <?= $totalBills ?></span>
</div>

<div class="erp-card mb-3">
    <form method="GET" class="d-flex gap-2 align-items-end">
        <div class="flex-fill">
            <label class="form-label" style="font-size:.82rem;">Filter by Date</label>
            <input type="date" name="date" class="form-select" value="<?= h($filterDate) ?>">
        </div>
        <button type="submit" class="btn btn-erp"><i class="bi bi-funnel me-1"></i>Filter</button>
        <a href="?" class="btn btn-outline-secondary"><i class="bi bi-x"></i></a>
    </form>
</div>

<div class="erp-card">
    <?php if (empty($bills)): ?>
    <div class="text-center py-5 text-muted">
        <i class="bi bi-receipt-cutoff" style="font-size:2.5rem;opacity:.3;"></i>
        <p class="mt-2">No bill records yet.</p>
        <a href="<?= BASE_URL ?>/modules/bills/add.php" class="btn btn-erp btn-sm">Add Bill Record</a>
    </div>
    <?php else: ?>
    <div class="table-responsive">
        <table class="table table-hover" style="font-size:.84rem;">
            <thead class="table-light">
                <tr>
                    <th>#</th>
                    <th>Consumer</th>
                    <th>Bill Type</th>
                    <th>Delivery</th>
                    <th>Photo</th>
                    <th>GPS</th>
                    <th>Date</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($bills as $i => $b): ?>
                <tr>
                    <td class="text-muted"><?= $i+1 ?></td>
                    <td>
                        <div class="fw-semibold"><?= h($b['consumer_name'] ?? '—') ?></div>
                        <?php if ($b['meter_number']): ?>
                        <small class="text-muted">Meter: <?= h($b['meter_number']) ?></small>
                        <?php endif; ?>
                    </td>
                    <td><?= billTypeBadge($b['bill_type']) ?></td>
                    <td style="font-size:.8rem;"><?= h(remarkLabel($b['delivery_remark'])) ?></td>
                    <td>
                        <?php if ($b['image_path']): ?>
                        <a href="<?= imgUrl($b['image_path']) ?>" target="_blank" rel="noopener">
                            <img src="<?= imgUrl($b['image_path']) ?>"
                                 style="width:44px;height:44px;object-fit:cover;border-radius:6px;border:1px solid #e2e8f0;">
                        </a>
                        <?php else: ?>—<?php endif; ?>
                    </td>
                    <td style="font-size:.75rem;">
                        <?php if ($b['gps_lat']): ?>
                        <a href="https://maps.google.com/?q=<?= $b['gps_lat'] ?>,<?= $b['gps_lng'] ?>" target="_blank" class="text-primary">
                            <i class="bi bi-geo-alt-fill"></i> View
                        </a>
                        <?php else: ?>—<?php endif; ?>
                    </td>
                    <td class="text-muted" style="font-size:.76rem;"><?= formatDt($b['created_at']) ?></td>
                </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
    <?php endif; ?>
</div>

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