<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <form action="<?= site_url('reports/profit-loss') ?>" method="get" class="d-flex align-items-center gap-2">
        <input type="date" class="form-control form-control-sm" name="date_from" value="<?= esc($dateFrom) ?>">
        <span class="text-muted small">to</span>
        <input type="date" class="form-control form-control-sm" name="date_to" value="<?= esc($dateTo) ?>">
        <button type="submit" class="btn btn-sm btn-outline-primary"><i class="bi bi-funnel me-1"></i>Filters</button>
    </form>
    <a href="<?= site_url('reports') ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-arrow-left me-1"></i>Back to Reports</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-6 col-lg-4">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Revenue <span class="text-muted" style="font-size:0.65rem;">(charges posted)</span></div>
            <div class="fs-3 fw-bold text-success"><?= format_money($revenue) ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-4">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Expenses <span class="text-muted" style="font-size:0.65rem;">(approved only)</span></div>
            <div class="fs-3 fw-bold text-danger"><?= format_money($expenseTotal) ?></div>
        </div></div>
    </div>
    <div class="col-6 col-lg-4">
        <div class="card h-100"><div class="card-body">
            <div class="text-muted small mb-1">Net</div>
            <div class="fs-3 fw-bold <?= (float) $net >= 0 ? 'text-success' : 'text-danger' ?>"><?= format_money($net) ?></div>
        </div></div>
    </div>
</div>

<div class="row g-3">
    <div class="col-lg-6">
        <div class="card h-100">
            <div class="card-header">Revenue by Category</div>
            <div class="table-responsive">
                <table class="table mb-0 small">
                    <thead><tr><th>Category</th><th class="text-end">Amount</th></tr></thead>
                    <tbody>
                    <?php if (empty($revenueByCategory)): ?>
                        <tr><td colspan="2" class="text-center text-muted py-3">No revenue posted in this range.</td></tr>
                    <?php endif; ?>
                    <?php foreach ($revenueByCategory as $category => $amount): ?>
                        <tr>
                            <td><?= esc($lookups->folioCategories[$category]['label'] ?? ucfirst($category)) ?></td>
                            <td class="text-end"><?= format_money($amount) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
    <div class="col-lg-6">
        <div class="card h-100">
            <div class="card-header d-flex justify-content-between align-items-center">
                Expenses by Category
                <a href="<?= site_url('expenses') ?>" class="btn btn-sm btn-outline-secondary">View Expenses</a>
            </div>
            <div class="table-responsive">
                <table class="table mb-0 small">
                    <thead><tr><th>Category</th><th class="text-end">Amount</th></tr></thead>
                    <tbody>
                    <?php if (empty($expensesByCategory)): ?>
                        <tr><td colspan="2" class="text-center text-muted py-3">No approved expenses in this range.</td></tr>
                    <?php endif; ?>
                    <?php foreach ($expensesByCategory as $category => $amount): ?>
                        <tr>
                            <td><?= esc($lookups->expenseCategories[$category]['label'] ?? ucfirst($category)) ?></td>
                            <td class="text-end"><?= format_money($amount) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<?= $this->endSection() ?>
