<?php ob_start(); ?>
<div class="container-fluid py-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <h2><?= $project ? 'Expenses — ' . htmlspecialchars($project['name']) : 'All Expenses' ?></h2>
    </div>

    <div class="row mb-4">
        <div class="col-md-4">
            <form method="GET" action="<?= APP_URL ?>/finance/expenses" class="d-flex gap-2">
                <select name="project_id" class="form-select" onchange="this.form.submit()">
                    <option value="">All Projects</option>
                    <?php foreach ($projects ?? [] as $p): ?>
                    <option value="<?= $p['id'] ?>" <?= (int)$projectId === (int)$p['id'] ? 'selected' : '' ?>><?= htmlspecialchars($p['name']) ?></option>
                    <?php endforeach; ?>
                </select>
            </form>
        </div>
    </div>

    <?php if ($project): ?>
    <div class="card mb-4">
        <div class="card-header">
            <h6 class="mb-0">Record Expense</h6>
        </div>
        <div class="card-body">
            <form action="<?= APP_URL ?>/finance/expenses/create" method="POST" class="row g-3">
                <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                <input type="hidden" name="project_id" value="<?= $projectId ?>">
                <div class="col-md-4">
                    <label class="form-label">Description</label>
                    <input type="text" name="description" class="form-control" required>
                </div>
                <div class="col-md-2">
                    <label class="form-label">Amount</label>
                    <input type="number" name="amount" step="0.01" class="form-control" required>
                </div>
                <div class="col-md-2">
                    <label class="form-label">Date</label>
                    <input type="date" name="expense_date" value="<?= date('Y-m-d') ?>" class="form-control" required>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Budget Item (optional)</label>
                    <select name="budget_item_id" class="form-select">
                        <option value="">None</option>
                        <?php foreach ($budgetItems ?? [] as $bi): ?>
                        <option value="<?= $bi['id'] ?>"><?= htmlspecialchars($bi['item_name']) ?></option>
                        <?php endforeach; ?>
                    </select>
                </div>
                <div class="col-md-4">
                    <label class="form-label">Vendor</label>
                    <input type="text" name="vendor_name" class="form-control">
                </div>
                <div class="col-md-4">
                    <label class="form-label">Reference Number</label>
                    <input type="text" name="reference_number" class="form-control">
                </div>
                <div class="col-md-4">
                    <label class="form-label">Notes</label>
                    <input type="text" name="notes" class="form-control">
                </div>
                <div class="col-12">
                    <button type="submit" class="btn btn-primary">Save Expense</button>
                </div>
            </form>
        </div>
    </div>
    <?php endif; ?>

    <div class="card">
        <div class="card-header">
            <h6 class="mb-0">Expense History</h6>
        </div>
        <div class="table-responsive">
            <table class="table align-items-center mb-0">
                <thead>
                    <tr>
                        <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Date</th>
                        <?php if (!$project): ?><th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Project</th><?php endif; ?>
                        <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Description</th>
                        <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Budget Item</th>
                        <th class="text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Vendor</th>
                        <th class="text-end text-uppercase text-secondary text-xxs font-weight-bolder opacity-7">Amount</th>
                        <th></th>
                    </tr>
                </thead>
                <tbody>
                    <?php if (empty($expenses)): ?>
                    <tr><td colspan="7" class="text-center py-4">No expenses recorded yet.</td></tr>
                    <?php else: ?>
                        <?php foreach ($expenses as $e): ?>
                        <tr>
                            <td><?= htmlspecialchars($e['expense_date']) ?></td>
                            <?php if (!$project): ?><td><?= htmlspecialchars($e['project_name'] ?? '') ?></td><?php endif; ?>
                            <td><?= htmlspecialchars($e['description']) ?></td>
                            <td><?= htmlspecialchars($e['budget_item_name'] ?? '-') ?></td>
                            <td><?= htmlspecialchars($e['vendor_name'] ?? '-') ?></td>
                            <td class="text-end"><?= formatCurrency((float)$e['amount'], $e['currency'] ?? DEFAULT_CURRENCY) ?></td>
                            <td class="text-end">
                                <form action="<?= APP_URL ?>/finance/expenses/<?= $e['id'] ?>/delete" method="POST" onsubmit="return confirm('Delete this expense?');">
                                    <input type="hidden" name="csrf_token" value="<?= htmlspecialchars($_SESSION[CSRF_TOKEN_NAME] ?? '') ?>">
                                    <button type="submit" class="btn btn-sm btn-link text-danger">Delete</button>
                                </form>
                            </td>
                        </tr>
                        <?php endforeach; ?>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>
<?php
$content = ob_get_clean();
require ROOT_PATH . '/modules/shared/views/layouts/main.php';
?>
