<?php
/**
 * @var object $income
 * @var array $categories
 * @var string $categoryId
 * @var string $fromDate
 * @var string $toDate
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-chart-line me-2 text-gold"></i>Income</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 Income</h6>
        </div>
        <div class="card-header-right">
            <?php if (can('accounting_income_create')): ?>
                <a href="<?= url('income/create') ?>" class="btn btn-sm btn-gold">
                    <i class="fas fa-plus me-1"></i>Add Income
                </a>
            <?php endif; ?>
        </div>
    </div>
    <div class="card-body">
        <form method="GET" action="<?= url('income') ?>" 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('income') ?>" 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 width="80" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($income->data)): ?>
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">
                                <i class="fas fa-coins fa-2x mb-2"></i>
                                <p class="mb-0">No income records found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($income->currentPage - 1) * $income->perPage + 1; ?>
                        <?php foreach ($income->data as $inc): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><?= formatDate($inc->date) ?></td>
                                <td><span class="badge bg-success"><?= e($inc->category_name) ?></span></td>
                                <td><?= e(truncate($inc->description, 50)) ?></td>
                                <td class="text-end text-success fw-bold"><?= currency($inc->amount) ?></td>
                                <td><?= e(ucfirst(str_replace('_', ' ', $inc->payment_method))) ?></td>
                                <td class="text-center">
                                    <span class="text-muted">-</span>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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