<?php $this->start('body');
    $rows         = $this->data['rows'] ?? [];
    $filterStatus = (string)($this->data['filter_status'] ?? '');
    $statusChip = [
        'DRAFT'     => ['#F1F5F9','#334155','Draft'],
        'SUBMITTED' => ['#EEF2F6','#7A8BA1','Awaiting FM/Exec'],
        'APPROVED'  => ['#EAF2E8','#3F5C43','Approved · ready to disburse'],
        'DECLINED'  => ['#F5EAE1','#C48579','Declined'],
        'PAID'      => ['#EAF2E8','#3F5C43','Paid'],
        'CANCELLED' => ['#F1F5F9','#334155','Cancelled'],
    ];
    $filters = ['' => 'All', 'DRAFT' => 'Draft', 'SUBMITTED' => 'Submitted', 'APPROVED' => 'Approved', 'DECLINED' => 'Declined', 'PAID' => 'Paid'];
?>

<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>Payment schedules</h5>
        <div class="text-muted small mt-1" style="font-size:.72rem;">
            Batched payables — Accountant assembles cash LPOs into runs, FM + Exec approve the whole batch.
        </div>
    </div>
    <?php if (UserGroups::can('PaymentSchedules','queue')): ?>
        <a href="<?= PROOT; ?>PaymentSchedules/queue" class="btn btn-sm" style="background:#7A9E7E;border-color:#7A9E7E;color:#fff;font-weight:600;">
            <i class="fas fa-plus me-1"></i>Build new schedule
        </a>
    <?php endif; ?>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-body p-2" style="background:#F8FAFC;">
        <?php foreach ($filters as $k => $lbl):
            $active = ($k === $filterStatus);
        ?>
            <a href="<?= PROOT; ?>PaymentSchedules<?= $k ? '?status=' . $k : ''; ?>" class="btn btn-sm me-1 mb-1"
               style="background:<?= $active ? '#334155' : '#fff'; ?>;color:<?= $active ? '#fff' : '#334155'; ?>;border:1px solid #E2E8F0;font-weight:<?= $active ? 600 : 400; ?>;font-size:.68rem;">
                <?= htmlspecialchars($lbl); ?>
            </a>
        <?php endforeach; ?>
    </div>
</div>

<div class="card border-0 shadow-sm">
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th>Schedule #</th>
                    <th>Status</th>
                    <th>Created by</th>
                    <th class="text-end" style="width:80px;">Items</th>
                    <th class="text-end" style="width:150px;">Total</th>
                    <th style="width:120px;">Scheduled</th>
                    <th style="width:120px;">Created</th>
                    <th></th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($rows as $r):
                    $sc = $statusChip[$r['status']] ?? ['#F1F5F9','#334155',$r['status']];
                ?>
                    <tr>
                        <td class="small"><a href="<?= PROOT; ?>PaymentSchedules/view/<?= (int)$r['c0d']; ?>" class="text-decoration-none" style="color:#7A8BA1;font-weight:600;"><?= htmlspecialchars($r['schedule_no']); ?></a></td>
                        <td><span class="badge" style="background:<?= $sc[0]; ?>;color:<?= $sc[1]; ?>;border:1px solid <?= $sc[1]; ?>;font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars($sc[2]); ?></span></td>
                        <td class="small"><?= htmlspecialchars($r['created_by_name']); ?>
                            <div class="text-muted" style="font-size:.62rem;text-transform:capitalize;"><?= htmlspecialchars(strtolower((string)$r['created_by_group'])); ?></div>
                        </td>
                        <td class="text-end small"><?= (int)$r['item_count']; ?></td>
                        <td class="text-end small" style="font-weight:600;color:#334155;"><?= htmlspecialchars($r['currency']); ?> <?= number_format((float)$r['total_amount']); ?></td>
                        <td class="small text-muted" style="font-size:.62rem;"><?= !empty($r['scheduled_date']) ? htmlspecialchars(date('d M Y', strtotime($r['scheduled_date']))) : '—'; ?></td>
                        <td class="small text-muted" style="font-size:.62rem;"><?= htmlspecialchars(date('d M · H:i', strtotime($r['created_on']))); ?></td>
                        <td class="text-end">
                            <a href="<?= PROOT; ?>PaymentSchedules/view/<?= (int)$r['c0d']; ?>" 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="8" class="text-center text-muted py-4" style="font-size:.72rem;">No schedules match this filter.</td></tr>
                <?php endif; ?>
            </tbody>
        </table>
    </div>
</div>

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