<?php $this->start('body');
    $pv        = $this->data['pv'];
    $banks     = $this->data['banks']    ?? [];
    $payments  = $this->data['payments'] ?? [];
    $canSched  = !empty($this->data['can_sched']);
    $canRecord = !empty($this->data['can_record']);
    $sc = [
        'DRAFT'     => 'bg-light text-muted',
        'SCHEDULED' => 'bg-info text-white',
        'PAID'      => 'bg-success',
        'CANCELLED' => 'bg-secondary',
    ][$pv['status']] ?? 'bg-light text-muted';
?>

<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-money-check-alt me-2 text-muted"></i><?= htmlspecialchars($pv['pv_no']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">
            LPO <a href="<?= PROOT; ?>Lpos/view/<?= (int)$pv['lpo_id']; ?>"><?= htmlspecialchars($pv['lpo_no']); ?></a>
            <?php if (!empty($pv['project_no'])): ?> · <?= htmlspecialchars($pv['project_no']); ?><?php endif; ?>
            · <span class="badge <?= $sc; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($pv['status'])); ?></span>
        </div>
    </div>
    <a href="<?= PROOT; ?>PaymentVouchers" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> All PVs</a>
</div>

<div class="row g-3 mb-3">
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Amount</div><div style="font-weight:600;font-size:1.05rem;"><?= htmlspecialchars($pv['currency']); ?> <?= number_format((float)$pv['amount']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Payee</div><div style="font-weight:600;"><?= htmlspecialchars($pv['payee_name']); ?></div><div class="text-muted small"><?= htmlspecialchars($pv['payee_type']); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Prepared</div><div style="font-weight:600;"><?= htmlspecialchars($pv['prepared_by_name']); ?></div><div class="text-muted small"><?= htmlspecialchars($pv['prepared_on'] ?: ''); ?></div></div></div></div>
    <div class="col-md-3"><div class="card border-0 shadow-sm"><div class="card-body p-2"><div class="text-muted small">Bank / Ref</div><div style="font-weight:600;"><?= htmlspecialchars($pv['bank_account_name'] ?: '—'); ?></div><div class="text-muted small"><?= htmlspecialchars($pv['reference_no'] ?: '(no ref yet)'); ?></div></div></div></div>
</div>

<?php if ($pv['purpose']): ?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong>Purpose (from LPO)</strong></div>
    <div class="card-body p-3 text-muted"><?= nl2br(htmlspecialchars($pv['purpose'])); ?></div>
</div>
<?php endif; ?>

<?php if ($pv['status'] === 'DRAFT' && $canSched): ?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong><i class="fas fa-calendar-alt me-2 text-muted"></i>Schedule payment</strong></div>
    <div class="card-body p-3">
        <div class="row g-2 align-items-end">
            <div class="col-md-4">
                <label class="form-label">Scheduled date *</label>
                <input type="date" id="rsSchedDate" class="form-control" value="<?= htmlspecialchars(date('Y-m-d')); ?>">
            </div>
            <div class="col-md-5">
                <label class="form-label">Bank account</label>
                <select id="rsSchedBank" class="form-select">
                    <option value="0">— select —</option>
                    <?php foreach ($banks as $b): ?>
                        <option value="<?= (int)$b['c0d']; ?>"><?= htmlspecialchars($b['bank_name']); ?> · <?= htmlspecialchars($b['account_name']); ?> (<?= htmlspecialchars($b['currency']); ?>)</option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3 text-end">
                <button type="button" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;" onclick="rsSchedule(<?= (int)$pv['c0d']; ?>)">
                    <i class="fas fa-calendar-check me-1"></i> Schedule
                </button>
            </div>
            <div class="col-12">
                <label class="form-label">Note (optional)</label>
                <input type="text" id="rsSchedNote" class="form-control">
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<?php if ($pv['status'] === 'SCHEDULED' && $canRecord): ?>
<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong><i class="fas fa-hand-holding-usd me-2 text-muted"></i>Record payment</strong></div>
    <div class="card-body p-3">
        <div class="row g-2 align-items-end">
            <div class="col-md-3">
                <label class="form-label">Payment date *</label>
                <input type="date" id="rsPayDate" class="form-control" value="<?= htmlspecialchars(date('Y-m-d')); ?>">
            </div>
            <div class="col-md-3">
                <label class="form-label">Method</label>
                <select id="rsPayMethod" class="form-select">
                    <?php foreach (['BANK_TRANSFER','CHEQUE','CASH','MOBILE_MONEY'] as $m): ?>
                        <option value="<?= $m; ?>"><?= ucwords(strtolower(str_replace('_',' ',$m))); ?></option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-3">
                <label class="form-label">Reference / cheque no</label>
                <input type="text" id="rsPayRef" class="form-control" placeholder="e.g. TXN-4839221 or Chq-101">
            </div>
            <div class="col-md-3 text-end">
                <button type="button" class="btn btn-sm btn-primary" style="background:<?= BRAND_COLOR; ?>;border-color:<?= BRAND_COLOR; ?>;" onclick="rsRecordPayment(<?= (int)$pv['c0d']; ?>)">
                    <i class="fas fa-check me-1"></i> Record Payment
                </button>
            </div>
        </div>
    </div>
</div>
<?php endif; ?>

<?php if (!empty($payments)): ?>
<div class="card border-0 shadow-sm">
    <div class="card-header bg-white"><strong><i class="fas fa-receipt me-2 text-muted"></i>Payments recorded</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>Method</th><th>Reference</th><th>Bank</th><th class="text-end">Amount</th></tr>
            </thead>
            <tbody>
                <?php foreach ($payments 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"><?= htmlspecialchars(ucwords(strtolower(str_replace('_',' ',$p['payment_method'])))); ?></td>
                        <td><?= htmlspecialchars($p['reference_no']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($p['bank_name'] . ' · ' . $p['bank_account_name']); ?></td>
                        <td class="text-end"><?= htmlspecialchars($p['currency']); ?> <?= number_format((float)$p['amount']); ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>
<?php endif; ?>

<script>
function rsSchedule(pvId) {
    const dt   = document.getElementById('rsSchedDate').value;
    const bank = document.getElementById('rsSchedBank').value;
    const note = document.getElementById('rsSchedNote').value;
    if (!dt) { alert('Enter a scheduled date.'); return; }
    fetch('<?= PROOT; ?>PaymentVouchers/scheduleAj', {
        method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'pv_id=' + pvId + '&scheduled_on=' + encodeURIComponent(dt) + '&bank_account_id=' + bank + '&notes=' + encodeURIComponent(note)
    }).then(r=>r.json()).then(j=>{ alert(j.msg||(j.ok?'Scheduled':'Failed')); if (j.ok) location.reload(); });
}
function rsRecordPayment(pvId) {
    const dt   = document.getElementById('rsPayDate').value;
    const m    = document.getElementById('rsPayMethod').value;
    const ref  = document.getElementById('rsPayRef').value;
    if (!dt) { alert('Enter payment date.'); return; }
    if (!confirm('Record this payment? This will close the LPO.')) return;
    fetch('<?= PROOT; ?>PaymentVouchers/recordPaymentAj', {
        method: 'POST', headers: {'Content-Type': 'application/x-www-form-urlencoded'},
        body: 'pv_id=' + pvId + '&payment_date=' + encodeURIComponent(dt) + '&payment_method=' + m + '&reference_no=' + encodeURIComponent(ref)
    }).then(r=>r.json()).then(j=>{ alert(j.msg||(j.ok?'Paid':'Failed')); if (j.ok) location.reload(); });
}
</script>

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