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

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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Reports',
    'workspaceCrumbCurrent' => 'Payables Aging',
    'workspaceTitle'        => 'Payables Aging Report',
    'workspaceDesc'         => 'Outstanding supplier bills by aging bucket.',
]) ?>

<div class="enterprise-data-panel mb-4">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Filters</h2>
        <p class="enterprise-data-panel__subtitle">As-of date defaults to today. Grand total <?= number_format((float) $grandTotal, 2) ?>.</p>
    </div>
    <div class="enterprise-data-panel__toolbar">
        <form action="<?= base_url('accounting/payables-aging') ?>" method="get">
            <div class="enterprise-filter-grid">
                <div>
                    <label class="enterprise-field-label">Supplier</label>
                    <select name="supplier_id" class="form-select form-select-sm">
                        <option value="">All suppliers</option>
                        <?php foreach ($suppliers as $s): ?>
                            <option value="<?= $s['id'] ?>" <?= (string) ($filters['supplierId'] ?? '') === (string) $s['id'] ? 'selected' : '' ?>>
                                <?= esc($s['name']) ?>
                            </option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div>
                    <label class="enterprise-field-label">As Of</label>
                    <input type="date" name="as_of" class="form-control form-control-sm" value="<?= esc($filters['asOf'] ?? date('Y-m-d')) ?>">
                </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/payables-aging',
                        'query'      => [
                            'as_of'       => $filters['asOf'],
                            'supplier_id' => $filters['supplierId'] ?? '',
                        ],
                    ]) ?>
                </div>
            </div>
        </form>
    </div>
</div>

<div class="row g-3 mb-4">
    <?php foreach ($buckets as $key => $bucket): ?>
        <div class="col-md">
            <article class="enterprise-settings-card h-100">
                <div class="enterprise-settings-card__head">
                    <i class="fa-solid fa-clock"></i>
                    <h2 class="enterprise-settings-card__title"><?= esc($bucket['label']) ?></h2>
                </div>
                <div class="p-3">
                    <p class="h4 mb-0"><?= number_format((float) $bucket['total'], 2) ?></p>
                    <p class="text-muted small mb-0"><?= count($bucket['rows']) ?> bill(s)</p>
                </div>
            </article>
        </div>
    <?php endforeach; ?>
</div>

<?php foreach ($buckets as $key => $bucket): ?>
    <?php if (count($bucket['rows']) === 0) {
        continue;
    } ?>
    <div class="enterprise-data-panel mb-4">
        <div class="enterprise-data-panel__head">
            <h2 class="enterprise-data-panel__title"><?= esc($bucket['label']) ?></h2>
            <p class="enterprise-data-panel__subtitle"><?= number_format((float) $bucket['total'], 2) ?> outstanding</p>
        </div>
        <div class="enterprise-data-panel__body">
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th>Number</th>
                            <th>Supplier</th>
                            <th>Bill Date</th>
                            <th>Due Date</th>
                            <th class="text-end">Days Overdue</th>
                            <th class="text-end">Outstanding</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($bucket['rows'] as $bill): ?>
                            <tr>
                                <td><a href="<?= base_url('accounting/supplier-bills/' . $bill['id']) ?>"><code><?= esc($bill['number']) ?></code></a></td>
                                <td><?= esc($bill['supplier_name'] ?? '—') ?></td>
                                <td><?= esc($bill['bill_date']) ?></td>
                                <td><?= esc($bill['due_date'] ?: $bill['bill_date']) ?></td>
                                <td class="text-end"><?= (int) ($bill['days_overdue'] ?? 0) ?></td>
                                <td class="text-end"><?= number_format((float) $bill['outstanding'], 2) ?></td>
                            </tr>
                        <?php endforeach; ?>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
<?php endforeach; ?>

<?php if ($grandTotal <= 0): ?>
    <div class="enterprise-data-panel">
        <div class="enterprise-data-panel__body">
            <p class="text-center text-muted py-5 mb-0">No outstanding supplier bills.</p>
        </div>
    </div>
<?php endif; ?>

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