<?php
/**
 * @var object $expenses
 * @var array $categories
 * @var string $categoryId
 * @var string $fromDate
 * @var string $toDate
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-wallet me-2 text-gold"></i>Expenses</h5>
</div>

<div class="glass-card p-0">
    <div class="card-header">
        <div class="card-header-left">
            <i class="fas fa-list card-icon"></i>
            <h6 class="card-title">All Expenses</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('accounting_expenses_create')): ?>
                <a href="<?= url('expenses/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Add Expense
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('expenses') ?>" class="row mb-3 g-2">
            <div class="col-md-3">
                <select name="category_id" class="form-select form-select-sm bg-transparent">
                    <option value="">All Categories</option>
                    <?php foreach ($categories as $cat): ?>
                        <option value="<?= $cat->id ?>" <?= $categoryId == $cat->id ? 'selected' : '' ?>>
                            <?= e($cat->name) ?>
                        </option>
                    <?php endforeach; ?>
                </select>
            </div>
            <div class="col-md-2">
                <input type="date" name="from_date" class="form-control form-control-sm bg-transparent"
                       value="<?= e($fromDate) ?>" placeholder="From">
            </div>
            <div class="col-md-2">
                <input type="date" name="to_date" class="form-control form-control-sm bg-transparent"
                       value="<?= e($toDate) ?>" placeholder="To">
            </div>
            <div class="col-md-2">
                <button type="submit" class="btn btn-sm btn-gold w-100">
                    <i class="fas fa-filter me-1"></i>Filter
                </button>
            </div>
            <div class="col-md-3 text-end">
                <a href="<?= url('expenses') ?>" class="btn btn-sm btn-outline-secondary">
                    <i class="fas fa-times me-1"></i>Clear
                </a>
            </div>
        </form>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Date</th>
                        <th>Category</th>
                        <th>Description</th>
                        <th class="text-end">Amount</th>
                        <th>Payment Method</th>
                        <th>Reference</th>
                        <th class="text-center">Receipt</th>
                        <th width="100" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($expenses->data)): ?>
                        <tr>
                            <td colspan="9" class="text-center text-muted py-4">
                                <i class="fas fa-receipt fa-2x mb-2"></i>
                                <p class="mb-0">No expenses found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($expenses->currentPage - 1) * $expenses->perPage + 1; ?>
                        <?php foreach ($expenses->data as $expense): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><?= formatDate($expense->date) ?></td>
                                <td><span class="badge bg-info"><?= e($expense->category_name) ?></span></td>
                                <td><?= e(truncate($expense->description, 50)) ?></td>
                                <td class="text-end text-danger fw-bold"><?= currency($expense->amount) ?></td>
                                <td><?= e(ucfirst(str_replace('_', ' ', $expense->payment_method))) ?></td>
                                <td><code><?= e($expense->reference_number ?? '-') ?></code></td>
                                <td class="text-center">
                                    <?php if (!empty($expense->receipt)): ?>
                                        <a href="<?= uploadUrl($expense->receipt) ?>" target="_blank" class="btn btn-sm btn-outline-info" title="View Receipt">
                                            <i class="fas fa-file"></i>
                                        </a>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                                <td class="text-center">
                                    <div class="btn-group btn-group-sm">
                                        <?php if (can('accounting_expenses_edit')): ?>
                                            <a href="<?= url('expenses/edit/' . $expense->id) ?>" class="btn btn-outline-gold" title="Edit">
                                                <i class="fas fa-edit"></i>
                                            </a>
                                        <?php endif; ?>
                                        <?php if (can('accounting_expenses_delete')): ?>
                                            <button type="button" class="btn btn-outline-danger btn-delete"
                                                    data-id="<?= $expense->id ?>" title="Delete">
                                                <i class="fas fa-trash"></i>
                                            </button>
                                        <?php endif; ?>
                                    </div>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

        <?php if ($expenses->totalPages > 1): ?>
            <div class="card-footer d-flex justify-content-between align-items-center">
                <div class="text-muted small">
                    Showing <?= (($expenses->currentPage - 1) * $expenses->perPage) + 1 ?>
                    to <?= min($expenses->currentPage * $expenses->perPage, $expenses->total) ?>
                    of <?= $expenses->total ?> entries
                </div>
                <?= paginateLinks($expenses, url('expenses')) ?>
            </div>
        <?php endif; ?>
    </div>
</div>


