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

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

<?php
$statusPill = static function (string $status): string {
    $map = ['posted' => '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 = $receipt['status'];
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Receipt',
    'workspaceTitle'        => $receipt['number'],
    'workspaceDesc'         => ($receipt['company_name'] ?? 'Customer') . ' · ' . ($receipt['receipt_date'] ?? ''),
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-money-bill-transfer"></i>
        <h2 class="enterprise-settings-card__title">Receipt 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($receipt['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">Customer</dt>
            <dd class="col-sm-3"><?= esc($receipt['company_name'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Invoice</dt>
            <dd class="col-sm-3"><?= ! empty($receipt['invoice_number']) ? '<code>' . esc($receipt['invoice_number']) . '</code>' : '—' ?></dd>
            <dt class="col-sm-3 text-muted">Receipt Date</dt>
            <dd class="col-sm-3"><?= esc($receipt['receipt_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Amount</dt>
            <dd class="col-sm-3"><?= number_format((float) $receipt['amount'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Method</dt>
            <dd class="col-sm-3"><?= esc($receipt['method'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Posted At</dt>
            <dd class="col-sm-3"><?= esc($receipt['posted_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($receipt['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/receipts') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/receipts/' . $receipt['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/receipts/' . $receipt['id'] . '/post') ?>" method="post" class="d-inline" onsubmit="return confirm('Post this receipt?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Post Receipt</button>
            </form>
            <form action="<?= base_url('accounting/receipts/' . $receipt['id'] . '/cancel') ?>" method="post" class="d-inline" onsubmit="return confirm('Cancel this receipt?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-secondary">Cancel</button>
            </form>
        <?php endif; ?>
    </div>
</article>

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