<?= $this->extend('layouts/enterprise') ?>

<?= $this->section('content') ?>

<?php
$statusPill = static function (string $status): string {
    $map = ['approved' => 'active', 'draft' => 'draft', 'cancelled' => 'inactive'];
    $class = $map[$status] ?? 'default';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
$st = $expense['status'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Expense',
    'workspaceTitle'        => $expense['number'],
    'workspaceDesc'         => ($expense['account_label'] ?? '') . ($expense['payee'] ? ' — ' . $expense['payee'] : ''),
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-wallet"></i>
        <h2 class="enterprise-settings-card__title">Expense Summary</h2>
    </div>
    <div class="p-4">
        <dl class="row mb-0">
            <dt class="col-sm-3 text-muted">Number</dt>
            <dd class="col-sm-3"><code><?= esc($expense['number']) ?></code></dd>
            <dt class="col-sm-3 text-muted">Status</dt>
            <dd class="col-sm-3"><?= $statusPill($st) ?></dd>
            <dt class="col-sm-3 text-muted">Date</dt>
            <dd class="col-sm-3"><?= esc($expense['expense_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Amount</dt>
            <dd class="col-sm-3"><?= number_format((float) $expense['amount'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Expense Account</dt>
            <dd class="col-sm-3"><?= esc($expense['account_label'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Pay From</dt>
            <dd class="col-sm-3"><?= esc($expense['pay_from_label'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Cost Center</dt>
            <dd class="col-sm-3"><?= esc($expense['cost_center_label'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Payee</dt>
            <dd class="col-sm-3"><?= esc($expense['payee'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Reference</dt>
            <dd class="col-sm-3"><?= esc($expense['reference'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Journal</dt>
            <dd class="col-sm-3">
                <?php if (! empty($expense['journal_entry_id'])): ?>
                    <a href="<?= base_url('accounting/journal-entries/' . $expense['journal_entry_id']) ?>">#<?= (int) $expense['journal_entry_id'] ?></a>
                <?php else: ?>
                    —
                <?php endif; ?>
            </dd>
            <dt class="col-sm-3 text-muted">Approved At</dt>
            <dd class="col-sm-3"><?= esc($expense['approved_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($expense['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/expenses') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/expenses/' . $expense['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/expenses/' . $expense['id'] . '/approve') ?>" method="post" class="d-inline" onsubmit="return confirm('Approve this expense?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Approve Expense</button>
            </form>
            <form action="<?= base_url('accounting/expenses/' . $expense['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this expense?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-danger">Delete</button>
            </form>
        <?php endif; ?>
    </div>
</article>

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