<hr class="my-4 text-muted">
<table class="table table-borderless table-hover align-middle">
    <tbody>
        <?php foreach($expenses as $ex): ?>
        <tr>
            <td style="width: 80px;" class="align-middle">
                <div class="text-muted small fw-bold lh-sm text-center bg-light rounded p-2 border">
                    <i class="fa-regular fa-calendar mb-1 d-block opacity-50"></i>
                    <?= date('M d', strtotime($ex->expense_date)) ?>
                </div>
            </td>
            <td>
                <div class="fw-bold text-dark fs-6"><?= htmlspecialchars($ex->item_name ?: '—') ?></div>
                <?php
                // Find category color
                $catColor = '#6366f1';
                foreach(array_merge($expenseCategories ?? [], $incomeCategories ?? []) as $catItem) {
                    if(isset($catItem->name) && $catItem->name === $ex->category) {
                        $catColor = $catItem->color ?? '#6366f1';
                        break;
                    }
                }
                ?>
                <small class="text-muted"><span class="badge fw-normal" style="background: <?= htmlspecialchars($catColor) ?>22; color: <?= htmlspecialchars($catColor) ?>; border: 1px solid <?= htmlspecialchars($catColor) ?>44;"><i class="fa-solid fa-tag me-1 opacity-75"></i><?= htmlspecialchars($ex->category) ?></span></small>
            </td>
            <td class="text-end fw-bold text-danger fs-5">-<?= number_format($ex->amount, 2) ?></td>
            <td class="text-end">
                <a href="<?= BASE_URL ?>/finance/edit/finance_expenses/<?= $ex->id ?>" class="text-secondary me-2"><i class="fa-solid fa-pen"></i></a>
                <a href="<?= BASE_URL ?>/finance/delete/finance_expenses/<?= $ex->id ?>" class="text-danger" onclick="return confirm('تأكيد الحذف؟');"><i class="fa-solid fa-trash"></i></a>
            </td>
        </tr>
        <?php endforeach; ?>
    </tbody>
</table>

<!-- Pagination for Expenses -->
<?php if (isset($expensePagination) && $expensePagination['total_pages'] > 1): ?>
<nav aria-label="Expense pagination">
  <ul class="pagination pagination-sm justify-content-center mt-3">
    <li class="page-item <?= ($expensePagination['current_page'] <= 1) ? 'disabled' : '' ?>">
      <a class="page-link" href="javascript:void(0)" onclick="fetchPaginatedTransactions('expense', <?= $expensePagination['current_page'] - 1 ?>)">السابق</a>
    </li>
    <?php for($i=1; $i<=$expensePagination['total_pages']; $i++): ?>
    <li class="page-item <?= ($i == $expensePagination['current_page']) ? 'active' : '' ?>">
      <a class="page-link" href="javascript:void(0)" onclick="fetchPaginatedTransactions('expense', <?= $i ?>)"><?= $i ?></a>
    </li>
    <?php endfor; ?>
    <li class="page-item <?= ($expensePagination['current_page'] >= $expensePagination['total_pages']) ? 'disabled' : '' ?>">
      <a class="page-link" href="javascript:void(0)" onclick="fetchPaginatedTransactions('expense', <?= $expensePagination['current_page'] + 1 ?>)">التالي</a>
    </li>
  </ul>
</nav>
<?php endif; ?>
