<?php $this->start('body');
    $rows   = $this->data['rows']   ?? [];
    $tot    = $this->data['totals'] ?? [];
    $asAt   = $this->v('as_at');
    $filter = $this->v('filter');

    $bucketColor = function (string $b): string {
        return ['current'=>'#22c55e','1_30'=>'#3b82f6','31_60'=>'#f59e0b','61_90'=>'#f97316','90_plus'=>'#ef4444'][$b] ?? '#94a3b8';
    };
?>

<?php $q = 'as_at=' . urlencode($asAt) . '&filter=' . urlencode((string)($filter ?? '')); ?>
<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-file-invoice me-2 text-muted"></i>Supplier Statements · AP Ageing</h5>
    <div class="btn-group">
        <a href="<?= PROOT; ?>SupplierStatement/xlsx?<?= $q; ?>" class="btn btn-sm" style="background:#faf6f0;border:1px solid #eae0d0;color:#4a3f36;"><i class="fas fa-file-excel me-1" style="color:#4a6b3f;"></i>Excel</a>
        <a href="<?= PROOT; ?>SupplierStatement/pdf?<?= $q; ?>"  class="btn btn-sm" style="background:#faf6f0;border:1px solid #eae0d0;color:#4a3f36;"><i class="fas fa-file-pdf   me-1" style="color:#8a4a3a;"></i>PDF</a>
    </div>
</div>

<form method="get" action="<?= PROOT; ?>SupplierStatement" class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2">
        <div class="row g-2 align-items-end">
            <div class="col-md-3">
                <label class="form-label">As at</label>
                <input type="date" name="as_at" value="<?= htmlspecialchars($asAt); ?>" class="form-control form-control-sm">
            </div>
            <div class="col-md-9 text-end">
                <a href="<?= PROOT; ?>SupplierStatement" class="badge <?= $filter===''?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">All vendors</a>
                <a href="<?= PROOT; ?>SupplierStatement?filter=outstanding&amp;as_at=<?= htmlspecialchars($asAt); ?>" class="badge <?= $filter==='outstanding'?'bg-dark':'bg-light text-muted'; ?>" style="text-decoration:none;padding:.35em .6em;">With outstanding</a>
                <button type="submit" class="btn btn-sm btn-primary ms-2" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;"><i class="fas fa-filter me-1"></i>Apply</button>
            </div>
        </div>
    </div>
</form>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body p-3"><div class="text-muted small">Total committed (LPO value)</div><div style="font-weight:700;font-size:1.35rem;color:#3b82f6;">TZS <?= number_format((float)$tot['committed']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100"><div class="card-body p-3"><div class="text-muted small">Total paid</div><div style="font-weight:700;font-size:1.35rem;color:#22c55e;">TZS <?= number_format((float)$tot['paid']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= (float)$tot['outstanding']>0?'#f59e0b':'#22c55e'; ?> !important;"><div class="card-body p-3"><div class="text-muted small">Total AP outstanding</div><div style="font-weight:700;font-size:1.5rem;color:<?= (float)$tot['outstanding']>0?'#f59e0b':'#22c55e'; ?>;">TZS <?= number_format((float)$tot['outstanding']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= (float)$tot['90_plus']>0?'#ef4444':'#22c55e'; ?> !important;"><div class="card-body p-3"><div class="text-muted small">90+ days overdue</div><div style="font-weight:700;font-size:1.5rem;color:<?= (float)$tot['90_plus']>0?'#ef4444':'#22c55e'; ?>;">TZS <?= number_format((float)$tot['90_plus']); ?></div></div></div></div>
</div>

<?php $totalOut = max((float)$tot['outstanding'], 0.01); ?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-body p-3">
        <div class="text-muted small mb-2">Ageing distribution across all suppliers</div>
        <div class="d-flex" style="height:24px;border-radius:3px;overflow:hidden;">
            <?php foreach (['current','1_30','31_60','61_90','90_plus'] as $b):
                $val = (float)$tot[$b];
                if ($val <= 0) continue;
                $pct = ($val / $totalOut) * 100;
            ?>
                <div style="width:<?= $pct; ?>%;background:<?= $bucketColor($b); ?>;" title="<?= strtoupper(str_replace('_','-',$b)); ?>: <?= number_format($val); ?>"></div>
            <?php endforeach; ?>
        </div>
        <div class="d-flex justify-content-between mt-2 flex-wrap gap-2" style="font-size:.72rem;">
            <div><span class="badge" style="background:<?= $bucketColor('current'); ?>;">Current</span> <?= number_format((float)$tot['current']); ?></div>
            <div><span class="badge" style="background:<?= $bucketColor('1_30'); ?>;">1-30</span> <?= number_format((float)$tot['1_30']); ?></div>
            <div><span class="badge" style="background:<?= $bucketColor('31_60'); ?>;">31-60</span> <?= number_format((float)$tot['31_60']); ?></div>
            <div><span class="badge" style="background:<?= $bucketColor('61_90'); ?>;">61-90</span> <?= number_format((float)$tot['61_90']); ?></div>
            <div><span class="badge" style="background:<?= $bucketColor('90_plus'); ?>;">90+</span> <?= number_format((float)$tot['90_plus']); ?></div>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong>Per supplier · as at <?= htmlspecialchars($asAt); ?></strong></div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Supplier</th><th>Category</th>
                    <th class="text-end">LPOs</th>
                    <th class="text-end">Committed</th>
                    <th class="text-end">Paid</th>
                    <th class="text-end">Outstanding</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">Oldest</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $out  = (float)$r['total_outstanding'];
                    $bad  = (float)$r['bucket_61_90'] + (float)$r['bucket_90_plus'];
                    $oldest = (int)($r['oldest_days_past_due'] ?? 0);
                ?>
                    <tr style="<?= $bad > 0 ? 'background:#fef2f2;' : ''; ?>">
                        <td><a href="<?= PROOT; ?>SupplierStatement/view/<?= (int)$r['vendor_id']; ?>?as_at=<?= htmlspecialchars($asAt); ?>" class="text-decoration-none"><strong><?= htmlspecialchars($r['vendor_name']); ?></strong></a><div class="text-muted small"><?= htmlspecialchars($r['vendor_no']); ?><?php if ($r['vendor_tin']): ?> · TIN <?= htmlspecialchars($r['vendor_tin']); ?><?php endif; ?></div></td>
                        <td><span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($r['vendor_category'] ?: '—')); ?></span></td>
                        <td class="text-end text-muted"><?= (int)$r['lpo_count']; ?></td>
                        <td class="text-end"><?= number_format((float)$r['total_committed']); ?></td>
                        <td class="text-end text-muted"><?= number_format((float)$r['total_paid']); ?></td>
                        <td class="text-end" style="font-weight:700;color:<?= $out>0?'#f59e0b':'#22c55e'; ?>;"><?= number_format($out); ?></td>
                        <td class="text-end" style="color:<?= (float)$r['bucket_current']>0?$bucketColor('current'):'#cbd5e1'; ?>;"><?= (float)$r['bucket_current']>0 ? number_format((float)$r['bucket_current']) : '—'; ?></td>
                        <td class="text-end" style="color:<?= (float)$r['bucket_1_30']>0?$bucketColor('1_30'):'#cbd5e1'; ?>;"><?= (float)$r['bucket_1_30']>0 ? number_format((float)$r['bucket_1_30']) : '—'; ?></td>
                        <td class="text-end" style="color:<?= (float)$r['bucket_31_60']>0?$bucketColor('31_60'):'#cbd5e1'; ?>;"><?= (float)$r['bucket_31_60']>0 ? number_format((float)$r['bucket_31_60']) : '—'; ?></td>
                        <td class="text-end" style="color:<?= (float)$r['bucket_61_90']>0?$bucketColor('61_90'):'#cbd5e1'; ?>;"><?= (float)$r['bucket_61_90']>0 ? number_format((float)$r['bucket_61_90']) : '—'; ?></td>
                        <td class="text-end" style="color:<?= (float)$r['bucket_90_plus']>0?$bucketColor('90_plus'):'#cbd5e1'; ?>;font-weight:<?= (float)$r['bucket_90_plus']>0?600:400; ?>;"><?= (float)$r['bucket_90_plus']>0 ? number_format((float)$r['bucket_90_plus']) : '—'; ?></td>
                        <td class="text-end"><?= $oldest > 0 ? '<span class="badge bg-danger">' . $oldest . 'd</span>' : ($out>0 ? '<span class="badge bg-success">current</span>' : '—'); ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>SupplierStatement/view/<?= (int)$r['vendor_id']; ?>?as_at=<?= htmlspecialchars($asAt); ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($rows)): ?>
                    <tr><td colspan="13" class="text-center text-muted py-4">No suppliers with committed LPOs <?= $filter==='outstanding' ? 'and outstanding balance' : ''; ?>.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->end(); ?>
