<?php $this->start('body');
    $types   = $this->data['types'] ?? [];
    $needsPv = $this->data['needs_pv'] ?? [];
    $hasPv   = $this->data['has_pv'] ?? [];
    $group     = strtolower($_SESSION['user_group_name'] ?? '');
    $canPrepPv = in_array($group, ['accountant','administrator'], true);
    $bucket = function (array $rows, string $emptyMsg, bool $showPrepBtn) use ($types, $canPrepPv) {
        if (empty($rows)) {
            echo '<tr><td colspan="8" class="text-center text-muted py-4">' . htmlspecialchars($emptyMsg) . '</td></tr>';
            return;
        }
        foreach ($rows as $r) {
            $t   = $types[$r['lpo_type']] ?? ['label'=>$r['lpo_type'],'icon'=>'fas fa-file'];
            $cls = ['APPROVED'=>'bg-success','ISSUED'=>'bg-primary'][$r['cash_out_status']] ?? 'bg-light text-muted';
            echo '<tr>';
            echo '<td><a href="' . PROOT . 'Lpos/view/' . (int)$r['c0d'] . '" class="text-decoration-none"><strong>' . htmlspecialchars($r['lpo_no']) . '</strong></a></td>';
            echo '<td><i class="' . $t['icon'] . ' me-1 text-muted"></i>' . htmlspecialchars($t['label']) . '</td>';
            echo '<td class="text-muted small">' . htmlspecialchars($r['project_no'] ?: '(central)') . '</td>';
            echo '<td>' . htmlspecialchars($r['payee_name'] ?: '—') . '</td>';
            echo '<td class="text-end">' . htmlspecialchars($r['currency']) . ' ' . number_format((float)$r['amount']) . '</td>';
            echo '<td class="text-muted small" style="max-width:260px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;" title="' . htmlspecialchars($r['purpose']) . '">' . htmlspecialchars($r['purpose']) . '</td>';
            echo '<td><span class="badge ' . $cls . '" style="text-transform:capitalize;">' . htmlspecialchars(strtolower(str_replace('_',' ',$r['cash_out_status']))) . '</span></td>';
            echo '<td class="text-end">';
            if ($showPrepBtn && $canPrepPv) {
                echo '<button type="button" class="btn btn-sm btn-primary me-1" style="background:' . BRAND_COLOR . ';border-color:' . BRAND_COLOR . ';" onclick="rsPrepPv(' . (int)$r['c0d'] . ')" title="Draft a PV"><i class="fas fa-file-invoice-dollar me-1"></i>Prep PV</button>';
            }
            echo '<a href="' . PROOT . 'Lpos/view/' . (int)$r['c0d'] . '" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-right"></i></a>';
            echo '</td>';
            echo '</tr>';
        }
    };
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-money-bill-wave me-2 text-muted"></i>Payables · unified queue</h5>
    <div class="text-muted small">
        <span class="badge bg-warning text-dark me-1"><?= count($needsPv); ?></span> needs PV ·
        <span class="badge bg-info text-white me-1"><?= count($hasPv); ?></span> PV drafted
    </div>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
        <strong><i class="fas fa-file-invoice-dollar me-2 text-muted"></i>Awaiting PV preparation</strong>
        <span class="badge bg-warning text-dark"><?= count($needsPv); ?></span>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th>LPO #</th><th>Type</th><th>Project</th><th>Payee</th><th class="text-end">Amount</th><th>Purpose</th><th>Status</th><th></th></tr>
            </thead>
            <tbody>
                <?php $bucket($needsPv, 'Nothing waiting on you right now.', true); ?>
            </tbody>
        </table>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
        <strong><i class="fas fa-file-alt me-2 text-muted"></i>PV drafted · awaiting schedule or payment</strong>
        <span class="badge bg-info text-white"><?= count($hasPv); ?></span>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr><th>LPO #</th><th>Type</th><th>Project</th><th>Payee</th><th class="text-end">Amount</th><th>Purpose</th><th>Status</th><th></th></tr>
            </thead>
            <tbody>
                <?php $bucket($hasPv, 'No drafted PVs in flight.', false); ?>
            </tbody>
        </table>
    </div>
</div>

<script>
function rsPrepPv(lpoId) {
    if (!confirm('Draft a Payment Voucher against this LPO?')) return;
    fetch('<?= PROOT; ?>PaymentVouchers/prepAj', {
        method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'lpo_id=' + encodeURIComponent(lpoId)
    }).then(r=>r.json()).then(j=>{
        if (!j.ok) { alert(j.msg||'Failed'); return; }
        window.location = '<?= PROOT; ?>PaymentVouchers/view/' + j.id;
    });
}
</script>

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