<?php
if (!isset($orderPaymentData)) {
    $orderPaymentData = [];
}
$weekStartStrTbl = isset($currentWeekStart) ? $currentWeekStart->format('Y-m-d') : null;
$weekEndStrTbl = isset($currentWeekEnd) ? $currentWeekEnd->format('Y-m-d') : null;

$orderPaymentsTableTotal = 0;
$orderPaymentsPriorTotal = 0;
foreach ($orderPaymentData as $op) {
    $opDay = date('Y-m-d', strtotime($op['payment_date'] ?? 'now'));
    $opIsPrior = $weekStartStrTbl && ($opDay < $weekStartStrTbl || $opDay > $weekEndStrTbl);
    if ($opIsPrior) {
        $orderPaymentsPriorTotal += (float)($op['amount'] ?? 0);
    } else {
        $orderPaymentsTableTotal += (float)($op['amount'] ?? 0);
    }
}
?>

<table class="table table-striped">
    <thead>
        <tr>
            <th>Date</th>
            <th>Order #</th>
            <th>Customer</th>
            <th>Amount</th>
            <th>Method</th>
            <th>Type</th>
            <th>Assigned Code</th>
        </tr>
    </thead>
    <tbody>
        <?php if (!empty($orderPaymentData)): ?>
            <?php foreach ($orderPaymentData as $op): ?>
                <?php
                $opDay = date('Y-m-d', strtotime($op['payment_date'] ?? 'now'));
                $opIsPrior = $weekStartStrTbl && ($opDay < $weekStartStrTbl || $opDay > $weekEndStrTbl);
                ?>
                <tr class="<?php echo $opIsPrior ? 'text-muted' : ''; ?>">
                    <td><?php echo htmlspecialchars(date('Y-m-d H:i', strtotime($op['payment_date'] ?? 'now'))); ?></td>
                    <td><?php echo htmlspecialchars($op['order_number'] ?? ''); ?></td>
                    <td><?php echo htmlspecialchars($op['customer_name'] ?? ''); ?></td>
                    <td>MWK <?php echo number_format((float)($op['amount'] ?? 0), 2); ?></td>
                    <td><?php echo htmlspecialchars(ucfirst($op['payment_method'] ?? '')); ?></td>
                    <td>
                        <?php echo !empty($op['is_initial']) ? 'Deposit' : 'Payment'; ?>
                        <?php if ($opIsPrior): ?>
                            <span class="badge bg-secondary" title="Belongs to a prior report; shown for reference only and not counted in this week's total.">Prior report</span>
                        <?php endif; ?>
                    </td>
                    <td><?php echo !empty($op['assigned_code']) ? htmlspecialchars($op['assigned_code']) : '<span class="text-muted">—</span>'; ?></td>
                </tr>
            <?php endforeach; ?>
        <?php else: ?>
            <tr>
                <td colspan="7" class="text-center">No unverified order payments found.</td>
            </tr>
        <?php endif; ?>
    </tbody>
    <?php if (!empty($orderPaymentData)): ?>
    <tfoot>
        <tr>
            <td colspan="3" class="text-end"><strong>Total Order Payments (this week)</strong></td>
            <td><strong>MWK <?php echo number_format($orderPaymentsTableTotal, 2); ?></strong></td>
            <td colspan="3"></td>
        </tr>
        <?php if ($orderPaymentsPriorTotal > 0): ?>
        <tr class="text-muted">
            <td colspan="3" class="text-end">Prior report(s), reference only (not counted)</td>
            <td>MWK <?php echo number_format($orderPaymentsPriorTotal, 2); ?></td>
            <td colspan="3"></td>
        </tr>
        <?php endif; ?>
    </tfoot>
    <?php endif; ?>
</table>
