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

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

<?php
$statusPill = static function (string $status): string {
    $map = ['active' => 'active', 'draft' => 'draft', 'closed' => 'inactive'];
    $class = $map[$status] ?? 'default';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
$st = $budget['status'];
$accountLabel = ! empty($budget['account_code'])
    ? $budget['account_code'] . ' — ' . ($budget['account_name'] ?? '')
    : '—';
$centerLabel = ! empty($budget['cost_center_code'])
    ? $budget['cost_center_code'] . ' — ' . ($budget['cost_center_name'] ?? '')
    : '—';
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Budget',
    'workspaceTitle'        => $budget['number'],
    'workspaceDesc'         => $budget['name'] ?? '',
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-chart-pie"></i>
        <h2 class="enterprise-settings-card__title">Budget 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($budget['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">Name</dt>
            <dd class="col-sm-3"><?= esc($budget['name']) ?></dd>
            <dt class="col-sm-3 text-muted">Fiscal Year</dt>
            <dd class="col-sm-3"><?= esc((string) $budget['fiscal_year']) ?></dd>
            <dt class="col-sm-3 text-muted">Period</dt>
            <dd class="col-sm-3"><?= esc($budget['period_from']) ?> → <?= esc($budget['period_to']) ?></dd>
            <dt class="col-sm-3 text-muted">Amount</dt>
            <dd class="col-sm-3"><?= number_format((float) $budget['amount'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Account</dt>
            <dd class="col-sm-3"><?= esc($accountLabel) ?></dd>
            <dt class="col-sm-3 text-muted">Cost Center</dt>
            <dd class="col-sm-3"><?= esc($centerLabel) ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($budget['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/budgets') ?>" class="btn btn-outline-secondary">Back</a>
        <a href="<?= base_url('accounting/budgets/' . $budget['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
        <?php if ($st === 'draft'): ?>
            <form action="<?= base_url('accounting/budgets/' . $budget['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this budget?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-danger">Delete</button>
            </form>
        <?php endif; ?>
    </div>
</article>

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