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

<?php
$status = $lookups->requestStatuses[$expense->status] ?? ['label' => ucfirst($expense->status), 'badge' => 'secondary'];
$cat    = $lookups->expenseCategories[$expense->category]['label'] ?? $expense->category;
?>

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-0">EX-<?= sprintf('%04d', $expense->id) ?> — <?= esc($expense->description) ?></h1>
        <p class="text-muted mb-0">
            <?= esc($cat) ?> · <span class="badge bg-<?= $status['badge'] ?>"><?= esc($status['label']) ?></span>
        </p>
    </div>
    <a href="<?= site_url('expenses') ?>" class="btn btn-outline-secondary btn-sm">Back to Expenses</a>
</div>

<div class="row">
    <div class="col-lg-8 mb-3">
        <div class="card shadow-sm mb-3">
            <div class="card-body">
                <div class="row row-cols-2 g-3 small">
                    <div class="col"><span class="text-muted">Amount</span><br><span class="fw-semibold fs-5"><?= format_money($expense->amount) ?></span></div>
                    <div class="col"><span class="text-muted">Date</span><br><span class="fw-semibold"><?= esc(date('d M Y', strtotime($expense->expense_date))) ?></span></div>
                    <div class="col"><span class="text-muted">Logged By</span><br><span class="fw-semibold"><?= esc($expense->created_by_name ?: '—') ?></span></div>
                    <div class="col"><span class="text-muted">Logged</span><br><span class="fw-semibold"><?= esc(date('d M Y, g:i a', strtotime($expense->created_at))) ?></span></div>
                </div>
                <?php if ($expense->notes): ?>
                    <hr>
                    <div class="text-muted small mb-1">Notes</div>
                    <p class="mb-0"><?= nl2br(esc($expense->notes)) ?></p>
                <?php endif; ?>
            </div>
        </div>

        <?php if ($expense->status !== 'pending'): ?>
        <div class="card shadow-sm">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-<?= $expense->status === 'approved' ? 'check2-circle text-success' : 'x-circle text-danger' ?>"></i> Review
            </div>
            <div class="card-body">
                <p class="mb-1"><?= esc($status['label']) ?> by <?= esc($expense->reviewed_by_name ?: '—') ?></p>
                <span class="text-muted small"><?= esc(date('d M Y, g:i a', strtotime($expense->reviewed_at))) ?></span>
            </div>
        </div>
        <?php endif; ?>
    </div>

    <?php if (session('userRole') === 'manager' && $expense->status === 'pending'): ?>
    <div class="col-lg-4">
        <div class="card shadow-sm">
            <div class="card-header">Workflow</div>
            <div class="card-body d-flex flex-column gap-2">
                <form action="<?= site_url('expenses/' . $expense->id . '/approve') ?>" method="post">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success btn-sm w-100"><i class="bi bi-check2 me-1"></i>Approve</button>
                </form>
                <form action="<?= site_url('expenses/' . $expense->id . '/deny') ?>" method="post">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-outline-danger btn-sm w-100"><i class="bi bi-x-lg me-1"></i>Deny</button>
                </form>
            </div>
        </div>
    </div>
    <?php endif; ?>
</div>

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