<?php $this->start('body');
    $v         = $this->data['vendor'];
    $asAt      = $this->data['as_at'];
    $lpos      = $this->data['lpos']    ?? [];
    $buckets   = $this->data['buckets'] ?? [];
    $pmts      = $this->data['payments'] ?? [];
    $totCommit = (float)($this->data['total_committed']   ?? 0);
    $totPaid   = (float)($this->data['total_paid']        ?? 0);
    $totOut    = (float)($this->data['total_outstanding'] ?? 0);
    $oldest    = (int)  ($this->data['oldest_days']       ?? 0);

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

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-file-invoice me-2 text-muted"></i><?= htmlspecialchars($v['vendor_name']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">
            <?= htmlspecialchars($v['vendor_no']); ?>
            <?php if ($v['vendor_tin']):      ?> · TIN <code class="text-body"><?= htmlspecialchars($v['vendor_tin']); ?></code><?php endif; ?>
            <?php if ($v['vendor_category']): ?> · <span style="text-transform:capitalize;"><?= htmlspecialchars($v['vendor_category']); ?></span><?php endif; ?>
            · as at <strong><?= htmlspecialchars($asAt); ?></strong>
        </div>
    </div>
    <a href="<?= PROOT; ?>SupplierStatement?as_at=<?= htmlspecialchars($asAt); ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> All suppliers</a>
</div>

<form method="get" action="<?= PROOT; ?>SupplierStatement/view/<?= (int)$v['c0d']; ?>" 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-4"><label class="form-label">Statement date (as at)</label><input type="date" name="as_at" value="<?= htmlspecialchars($asAt); ?>" class="form-control form-control-sm"></div>
            <div class="col-md-8 text-end"><button type="submit" class="btn btn-sm btn-outline-secondary"><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</div><div style="font-weight:700;font-size:1.4rem;color:#3b82f6;">TZS <?= number_format($totCommit); ?></div><div class="text-muted small mt-1"><?= count($lpos); ?> LPO(s)</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.4rem;color:#22c55e;">TZS <?= number_format($totPaid); ?></div><div class="text-muted small mt-1"><?= count($pmts); ?> payment(s) shown</div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= $totOut>0?'#f59e0b':'#22c55e'; ?> !important;"><div class="card-body p-3"><div class="text-muted small">AP outstanding</div><div style="font-weight:700;font-size:1.6rem;color:<?= $totOut>0?'#f59e0b':'#22c55e'; ?>;">TZS <?= number_format($totOut); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm h-100" style="border-left:4px solid <?= $oldest>90?'#ef4444':($oldest>0?'#f59e0b':'#22c55e'); ?> !important;"><div class="card-body p-3"><div class="text-muted small">Oldest days past due</div><div style="font-weight:700;font-size:1.6rem;color:<?= $oldest>90?'#ef4444':($oldest>0?'#f59e0b':'#22c55e'); ?>;"><?= $oldest > 0 ? $oldest . 'd' : '—'; ?></div><div class="text-muted small mt-1"><?= $oldest>90?'critical':($oldest>0?'past due':'current'); ?></div></div></div></div>
</div>

<?php $sumBuckets = max(array_sum($buckets), 0.01); ?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong>Ageing distribution</strong></div>
    <div class="card-body p-3">
        <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)$buckets[$b];
                if ($val <= 0) continue;
                $pct = ($val / $sumBuckets) * 100;
            ?>
                <div style="width:<?= $pct; ?>%;background:<?= $bucketColor($b); ?>;" title="<?= $bucketLabel[$b]; ?>: <?= number_format($val); ?>"></div>
            <?php endforeach; ?>
        </div>
        <div class="row g-2 mt-3">
            <?php foreach (['current','1_30','31_60','61_90','90_plus'] as $b):
                $val = (float)$buckets[$b];
                $pct = ($val / $sumBuckets) * 100;
            ?>
                <div class="col">
                    <div style="border-left:3px solid <?= $bucketColor($b); ?>;padding-left:8px;">
                        <div class="text-muted small"><?= $bucketLabel[$b]; ?></div>
                        <div style="font-weight:600;"><?= $val > 0 ? number_format($val) : '—'; ?></div>
                        <?php if ($val > 0): ?><div class="text-muted small"><?= number_format($pct, 1); ?>% of outstanding</div><?php endif; ?>
                    </div>
                </div>
            <?php endforeach; ?>
        </div>
    </div>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong>LPOs</strong></div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>LPO #</th><th>Requested</th><th>Effective due</th><th>Project</th>
                    <th class="text-end">Amount</th><th class="text-end">Paid</th><th class="text-end">Outstanding</th>
                    <th class="text-end">Days past due</th><th>Bucket</th><th>Status</th><th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($lpos as $l):
                    $bucket = $l['bucket'];
                    $days   = (int)$l['days_past_due'];
                    $out    = (float)$l['outstanding'];
                    $sc     = ['APPROVED'=>'bg-success','ISSUED'=>'bg-primary','PAID'=>'bg-secondary'][$l['cash_out_status']] ?? 'bg-light text-muted';
                ?>
                    <tr style="<?= $bucket==='90_plus' && $out>0 ? 'background:#fef2f2;' : ($bucket==='61_90' && $out>0 ? 'background:#fff7ed;' : ''); ?>">
                        <td><a href="<?= PROOT; ?>Lpos/view/<?= (int)$l['c0d']; ?>" class="text-decoration-none"><strong><?= htmlspecialchars($l['lpo_no']); ?></strong></a></td>
                        <td class="text-muted small"><?= htmlspecialchars($l['requested_date']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($l['effective_due']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($l['project_no'] ?: '(central)'); ?></td>
                        <td class="text-end"><?= number_format((float)$l['amount']); ?></td>
                        <td class="text-end text-muted"><?= number_format((float)$l['paid']); ?></td>
                        <td class="text-end" style="font-weight:600;color:<?= $out>0?'#f59e0b':'#22c55e'; ?>;"><?= number_format($out); ?></td>
                        <td class="text-end"><?= $out > 0 && $days > 0 ? '<strong>' . $days . 'd</strong>' : ($out > 0 ? 'current' : '—'); ?></td>
                        <td>
                            <?php if ($out > 0): ?>
                                <span class="badge" style="background:<?= $bucketColor($bucket); ?>;color:#fff;"><?= $bucketLabel[$bucket]; ?></span>
                            <?php else: ?>
                                <span class="badge bg-success"><i class="fas fa-check me-1"></i>Paid</span>
                            <?php endif; ?>
                        </td>
                        <td><span class="badge <?= $sc; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($l['cash_out_status'])); ?></span></td>
                        <td class="text-end"><a href="<?= PROOT; ?>Lpos/view/<?= (int)$l['c0d']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
                <?php if (empty($lpos)): ?>
                    <tr><td colspan="11" class="text-center text-muted py-3">No committed LPOs for this supplier.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

<?php if (!empty($pmts)): ?>
<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong><i class="fas fa-hand-holding-usd me-2 text-muted"></i>Recent payments</strong></div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th>Payment #</th><th>Date</th><th>PV</th><th>LPO</th><th>Method</th><th>Reference</th><th class="text-end">Amount</th><th></th></tr>
            </thead>
            <tbody>
                <?php foreach ($pmts as $p): ?>
                    <tr>
                        <td><strong><?= htmlspecialchars($p['payment_no']); ?></strong></td>
                        <td class="text-muted small"><?= htmlspecialchars($p['payment_date']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($p['pv_no']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($p['lpo_no']); ?></td>
                        <td class="text-muted"><?= htmlspecialchars(ucwords(strtolower(str_replace('_',' ',$p['payment_method'])))); ?></td>
                        <td class="text-muted"><?= htmlspecialchars($p['reference_no']); ?></td>
                        <td class="text-end" style="font-weight:600;"><?= number_format((float)$p['amount']); ?></td>
                        <td class="text-end"><a href="<?= PROOT; ?>Lpos/view/<?= (int)$p['lpo_id']; ?>" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

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