<?php
if (!isset($creditPaymentData)) {
    $creditPaymentData = [];
}
$creditPaymentsTableTotal = 0;
foreach ($creditPaymentData as $cp) {
    $creditPaymentsTableTotal += (float)($cp['amount'] ?? 0);
}
?>

<table class="table table-striped">
    <thead>
        <tr>
            <th>Date</th>
            <th>Credit No.</th>
            <th>Customer</th>
            <th>Amount</th>
            <th>Method</th>
            <th>Credit Status</th>
        </tr>
    </thead>
    <tbody>
        <?php if (!empty($creditPaymentData)): ?>
            <?php foreach ($creditPaymentData as $cp): ?>
                <tr>
                    <td><?php echo htmlspecialchars(date('Y-m-d H:i', strtotime($cp['payment_date'] ?? 'now'))); ?></td>
                    <td><?php echo htmlspecialchars($cp['credit_number'] ?? ''); ?></td>
                    <td><?php echo htmlspecialchars($cp['customer_name'] ?? ''); ?></td>
                    <td>MWK <?php echo number_format((float)($cp['amount'] ?? 0), 2); ?></td>
                    <td><?php echo htmlspecialchars(ucfirst($cp['payment_method'] ?? '')); ?></td>
                    <td><?php echo htmlspecialchars(ucwords(str_replace('_', ' ', $cp['status'] ?? ''))); ?></td>
                </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr>
                <td colspan="6" class="text-center">No unverified credit sale payments found.</td>
            </tr>
        <?php endif; ?>
    </tbody>
    <?php if (!empty($creditPaymentData)): ?>
    <tfoot>
        <tr>
            <td colspan="3" class="text-end"><strong>Total Credit Sale Payments</strong></td>
            <td><strong>MWK <?php echo number_format($creditPaymentsTableTotal, 2); ?></strong></td>
            <td colspan="2"></td>
        </tr>
    </tfoot>
    <?php endif; ?>
</table>
