<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Receivables Aging',
    'workspaceTitle'        => 'Receivables Aging',
    'workspaceDesc'         => 'Outstanding customer balances by aging bucket.',
]) ?>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Aging Report</h2>
        <p class="enterprise-data-panel__subtitle">Based on due date (or invoice date when due date is empty).</p>
    </div>

    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('accounting/receivables-aging') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">As of</label>
                    <input type="date" name="as_of" class="form-control form-control-sm" value="<?= esc($asOf) ?>">
                </div>
                <div class="enterprise-filter-actions">
                    <button type="submit" class="btn btn-sm btn-primary">Apply</button>
                    <?= view('Modules\Reports\Views\partials\export_buttons', [
                        'actionPath' => 'accounting/receivables-aging',
                        'query'      => ['as_of' => $asOf],
                    ]) ?>
                </div>
            </div>
        </form>
    </div>

    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Customer</th>
                        <th class="text-end">Current</th>
                        <th class="text-end">1–30</th>
                        <th class="text-end">31–60</th>
                        <th class="text-end">61–90</th>
                        <th class="text-end">90+</th>
                        <th class="text-end">Total</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($groups as $group): ?>
                        <tr>
                            <td><span class="enterprise-data-table__primary"><?= esc($group['company_name']) ?></span></td>
                            <td class="text-end"><?= number_format($group['current'], 2) ?></td>
                            <td class="text-end"><?= number_format($group['d1_30'], 2) ?></td>
                            <td class="text-end"><?= number_format($group['d31_60'], 2) ?></td>
                            <td class="text-end"><?= number_format($group['d61_90'], 2) ?></td>
                            <td class="text-end"><?= number_format($group['d90_plus'], 2) ?></td>
                            <td class="text-end fw-semibold"><?= number_format($group['total'], 2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($groups) === 0): ?>
                        <tr><td colspan="7" class="text-center text-muted py-5">No outstanding receivables as of <?= esc($asOf) ?>.</td></tr>
                    <?php else: ?>
                        <tr>
                            <td class="fw-semibold">Totals</td>
                            <td class="text-end fw-semibold"><?= number_format($totals['current'], 2) ?></td>
                            <td class="text-end fw-semibold"><?= number_format($totals['d1_30'], 2) ?></td>
                            <td class="text-end fw-semibold"><?= number_format($totals['d31_60'], 2) ?></td>
                            <td class="text-end fw-semibold"><?= number_format($totals['d61_90'], 2) ?></td>
                            <td class="text-end fw-semibold"><?= number_format($totals['d90_plus'], 2) ?></td>
                            <td class="text-end fw-semibold"><?= number_format($totals['total'], 2) ?></td>
                        </tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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