<?php include __DIR__ . '/../../layouts/header.php'; ?>
<?php include __DIR__ . '/../../layouts/sidebar.php'; ?>

<div class="d-flex justify-content-between align-items-center mb-4">
    <h2 class="mb-0"><?= __('payments') ?></h2>
</div>

<div class="table-container">
    <div class="table-responsive">
        <table class="table align-middle">
            <thead>
                <tr>
                    <th><?= __('id') ?></th>
                    <th><?= __('payer') ?></th>
                    <th><?= __('related_type') ?></th>
                    <th><?= __('amount') ?></th>
                    <th><?= __('method') ?></th>
                    <th><?= __('date') ?></th>
                    <th><?= __('status') ?></th>
                </tr>
            </thead>
            <tbody>
                <?php if (empty($payments)): ?>
                    <tr>
                        <td colspan="7" class="text-center py-5 text-muted"><?= __('no_data_found') ?></td>
                    </tr>
                <?php else: ?>
                    <?php foreach ($payments as $payment): ?>
                        <tr>
                            <td class="text-secondary fw-semibold">
                                #<?= htmlspecialchars($payment['id']) ?>
                                <?php if($payment['transaction_reference']): ?>
                                    <div style="font-size: 11px;" class="text-muted"><?= htmlspecialchars($payment['transaction_reference']) ?></div>
                                <?php endif; ?>
                            </td>
                            <td>
                                <span class="fw-bold text-dark">
                                    <i class="fas <?= $payment['payer_type'] === 'customer' ? 'fa-user-circle' : 'fa-building' ?> text-secondary" style="<?= isRTL() ? 'margin-left: 4px;' : 'margin-right: 4px;' ?>"></i>
                                    <?= htmlspecialchars($payment['payer_name']) ?>
                                </span>
                            </td>
                            <td>
                                <span class="badge badge-light text-dark border fw-normal">
                                    <?= htmlspecialchars(ucfirst($payment['related_type'])) ?> #<?= htmlspecialchars($payment['related_id']) ?>
                                </span>
                            </td>
                            <td><span class="fw-bold text-dark"><?= htmlspecialchars($payment['amount']) ?></span> <span class="text-secondary" style="font-size: 12px;"><?= htmlspecialchars($payment['currency']) ?></span></td>
                            <td><?= htmlspecialchars($payment['payment_method'] ?? '-') ?></td>
                            <td class="text-secondary"><?= date('M d, Y h:i A', strtotime($payment['created_at'])) ?></td>
                            <td>
                                <?php if ($payment['status'] === 'paid'): ?>
                                    <span class="badge badge-success"><?= __('active') ?></span>
                                <?php elseif ($payment['status'] === 'failed' || $payment['status'] === 'refunded'): ?>
                                    <span class="badge badge-danger"><?= htmlspecialchars(ucfirst($payment['status'])) ?></span>
                                <?php else: ?>
                                    <span class="badge badge-warning"><?= htmlspecialchars(ucfirst($payment['status'])) ?></span>
                                <?php endif; ?>
                            </td>
                        </tr>
                    <?php endforeach; ?>
                <?php endif; ?>
            </tbody>
        </table>
    </div>

    <?php if ($totalPages > 1): ?>
    <div class="d-flex justify-content-end mt-4">
        <ul class="pagination mb-0 gap-2">
            <li class="page-item <?= $page <= 1 ? 'disabled' : '' ?>">
                <a class="page-link rounded-circle border-0 text-secondary bg-light" href="?page=payments&p=<?= $page - 1 ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><i class="fas <?= isRTL() ? 'fa-chevron-right' : 'fa-chevron-left' ?>"></i></a>
            </li>
            <?php for ($i = 1; $i <= $totalPages; $i++): ?>
                <li class="page-item <?= $i == $page ? 'active' : '' ?>">
                    <a class="page-link rounded-circle border-0 <?= $i == $page ? 'bg-primary text-white' : 'text-secondary bg-light' ?>" href="?page=payments&p=<?= $i ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><?= $i ?></a>
                </li>
            <?php endfor; ?>
            <li class="page-item <?= $page >= $totalPages ? 'disabled' : '' ?>">
                <a class="page-link rounded-circle border-0 text-secondary bg-light" href="?page=payments&p=<?= $page + 1 ?>" style="width:40px; height:40px; display:flex; align-items:center; justify-content:center;"><i class="fas <?= isRTL() ? 'fa-chevron-left' : 'fa-chevron-right' ?>"></i></a>
            </li>
        </ul>
    </div>
    <?php endif; ?>
</div>

<?php include __DIR__ . '/../../layouts/footer.php'; ?>
