<?php
/**
 * @var object $items
 * @var string $status
 */
?>
<div class="page-title mb-4">
    <h5><i class="fas fa-coins me-2 text-gold"></i>Petty Cash</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">Petty Cash Requests</h6>
        </div>
        <div class="card-header-right">
            <button type="button" class="btn btn-sm btn-gold" data-bs-toggle="modal" data-bs-target="#addPettyCashModal">
                <i class="fas fa-plus me-1"></i>New Request
            </button>
        </div>
    </div>
    <div class="card-body">
        <div class="row mb-3">
            <div class="col-md-3">
                <select class="form-select form-select-sm bg-transparent" onchange="window.location.href=this.value">
                    <option value="<?= url('accounting/petty-cash') ?>">All Status</option>
                    <option value="pending" <?= $status === 'pending' ? 'selected' : '' ?>>Pending</option>
                    <option value="approved" <?= $status === 'approved' ? 'selected' : '' ?>>Approved</option>
                    <option value="rejected" <?= $status === 'rejected' ? 'selected' : '' ?>>Rejected</option>
                </select>
            </div>
        </div>

        <div class="table-responsive">
            <table class="table table-dark table-hover mb-0">
                <thead>
                    <tr>
                        <th width="50">#</th>
                        <th>Date</th>
                        <th>Description</th>
                        <th class="text-end">Amount</th>
                        <th>Requested By</th>
                        <th>Status</th>
                        <th width="120" class="text-center">Actions</th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($items->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 petty cash requests found</p>
                            </td>
                        </tr>
                    <?php else: ?>
                        <?php $i = ($items->currentPage - 1) * $items->perPage + 1; ?>
                        <?php foreach ($items->data as $item): ?>
                            <tr>
                                <td><?= $i++ ?></td>
                                <td><?= formatDate($item->transaction_date) ?></td>
                                <td><?= e($item->description) ?></td>
                                <td class="text-end fw-bold"><?= currency($item->amount) ?></td>
                                <td><?= e($item->first_name . ' ' . $item->last_name) ?></td>
                                <td>
                                    <span class="badge bg-<?= statusBadge($item->status) ?>">
                                        <?= e(ucfirst($item->status)) ?>
                                    </span>
                                </td>
                                <td class="text-center">
                                    <?php if ($item->status === 'pending' && can('accounting_pettycash_approve')): ?>
                                        <button type="button" class="btn btn-sm btn-outline-success btn-approve"
                                                data-id="<?= $item->id ?>" title="Approve">
                                            <i class="fas fa-check"></i>
                                        </button>
                                    <?php else: ?>
                                        <span class="text-muted">-</span>
                                    <?php endif; ?>
                                </td>
                            </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>

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

<div class="modal fade" id="addPettyCashModal" tabindex="-1">
    <div class="modal-dialog">
        <div class="modal-content bg-dark">
            <div class="modal-header border-secondary">
                <h6 class="modal-title"><i class="fas fa-plus-circle text-gold me-2"></i>New Petty Cash Request</h6>
                <button type="button" class="btn-close btn-close-white" data-bs-dismiss="modal"></button>
            </div>
            <form method="POST" action="<?= url('accounting/petty-cash/store') ?>">
                <?= csrf_field() ?>
                <div class="modal-body">
                    <div class="mb-3">
                        <label class="form-label">Description</label>
                        <textarea name="description" class="form-control bg-transparent" rows="2" required maxlength="500"></textarea>
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Amount</label>
                        <input type="number" step="0.01" name="amount" class="form-control bg-transparent" required min="0">
                    </div>
                    <div class="mb-3">
                        <label class="form-label">Date</label>
                        <input type="date" name="date" class="form-control bg-transparent" value="<?= date('Y-m-d') ?>" required>
                    </div>
                </div>
                <div class="modal-footer border-secondary">
                    <button type="button" class="btn btn-outline-secondary" data-bs-dismiss="modal">Cancel</button>
                    <button type="submit" class="btn btn-gold">Submit Request</button>
                </div>
            </form>
        </div>
    </div>
</div>


