<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<?php
$tabs = [
    'all'      => 'All',
    'pending'  => 'Pending',
    'approved' => 'Approved',
    'denied'   => 'Denied',
];
$isManager = session('userRole') === 'manager';
?>

<div class="d-flex justify-content-between align-items-center mb-3">
    <p class="text-muted mb-0"><?= esc($pageSubtitle ?? '') ?></p>
    <a href="<?= site_url('expenses/new') ?>" class="btn btn-primary btn-sm"><i class="bi bi-plus-lg me-1"></i>Log Expense</a>
</div>

<?php if ($pendingCount > 0): ?>
<div class="alert alert-warning d-flex align-items-center gap-2 py-2 small">
    <i class="bi bi-hourglass-split"></i>
    <?= $pendingCount ?> expense<?= $pendingCount === 1 ? '' : 's' ?> awaiting manager approval.
</div>
<?php endif; ?>

<div class="card">
    <div class="card-header p-0">
        <ul class="nav nav-tabs border-0 px-2 pt-2">
            <?php foreach ($tabs as $key => $label): ?>
                <li class="nav-item"><a class="nav-link <?= $tab === $key ? 'active' : '' ?>" href="<?= site_url('expenses?tab=' . $key) ?>"><?= esc($label) ?></a></li>
            <?php endforeach; ?>
        </ul>
    </div>
    <div class="table-responsive">
        <table class="table mb-0 align-middle small">
            <thead><tr><th>#</th><th>Category</th><th>Description</th><th class="text-end">Amount</th><th>Date</th><th>Status</th><th>Logged By</th><th></th></tr></thead>
            <tbody>
            <?php if (empty($expenses)): ?>
                <tr><td colspan="8" class="text-center text-muted py-4">No expenses in this view.</td></tr>
            <?php endif; ?>
            <?php foreach ($expenses as $e):
                $status = $lookups->requestStatuses[$e->status] ?? ['label' => ucfirst($e->status), 'badge' => 'secondary'];
                $cat    = $lookups->expenseCategories[$e->category]['label'] ?? $e->category;
            ?>
                <tr>
                    <td class="fw-semibold">EX-<?= sprintf('%04d', $e->id) ?></td>
                    <td><?= esc($cat) ?></td>
                    <td><?= esc($e->description) ?></td>
                    <td class="text-end"><?= format_money($e->amount) ?></td>
                    <td class="text-muted"><?= esc(date('d M Y', strtotime($e->expense_date))) ?></td>
                    <td><span class="badge bg-<?= $status['badge'] ?>"><?= esc($status['label']) ?></span></td>
                    <td class="text-muted"><?= esc($e->created_by_name ?: '—') ?></td>
                    <td class="text-end">
                        <div class="d-flex justify-content-end gap-1">
                            <?php if ($isManager && $e->status === 'pending'): ?>
                                <form action="<?= site_url('expenses/' . $e->id . '/approve') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-success" title="Approve"><i class="bi bi-check2"></i></button>
                                </form>
                                <form action="<?= site_url('expenses/' . $e->id . '/deny') ?>" method="post" class="d-inline">
                                    <?= csrf_field() ?>
                                    <button type="submit" class="btn btn-sm btn-outline-danger" title="Deny"><i class="bi bi-x-lg"></i></button>
                                </form>
                            <?php endif; ?>
                            <a href="<?= site_url('expenses/' . $e->id) ?>" class="btn btn-sm btn-outline-secondary"><i class="bi bi-eye"></i></a>
                        </div>
                    </td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?= $this->endSection() ?>
