<?= $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 = $voucher['status'];
?>

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

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-money-check-dollar"></i>
        <h2 class="enterprise-settings-card__title">Payment Voucher 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($voucher['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($voucher['voucher_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Amount</dt>
            <dd class="col-sm-3"><?= number_format((float) $voucher['amount'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Debit Account</dt>
            <dd class="col-sm-3"><?= esc($voucher['account_label'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Pay From</dt>
            <dd class="col-sm-3"><?= esc($voucher['pay_from_label'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Cost Center</dt>
            <dd class="col-sm-3"><?= esc($voucher['cost_center_label'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Payee</dt>
            <dd class="col-sm-3"><?= esc($voucher['payee'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Reference</dt>
            <dd class="col-sm-3"><?= esc($voucher['reference'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Journal</dt>
            <dd class="col-sm-3">
                <?php if (! empty($voucher['journal_entry_id'])): ?>
                    <a href="<?= base_url('accounting/journal-entries/' . $voucher['journal_entry_id']) ?>">#<?= (int) $voucher['journal_entry_id'] ?></a>
                <?php else: ?>
                    —
                <?php endif; ?>
            </dd>
            <dt class="col-sm-3 text-muted">Posted At</dt>
            <dd class="col-sm-3"><?= esc($voucher['posted_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($voucher['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/payment-vouchers') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/payment-vouchers/' . $voucher['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/payment-vouchers/' . $voucher['id'] . '/post') ?>" method="post" class="d-inline" onsubmit="return confirm('Post this payment voucher?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Post Voucher</button>
            </form>
            <form action="<?= base_url('accounting/payment-vouchers/' . $voucher['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this payment voucher?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-danger">Delete</button>
            </form>
        <?php endif; ?>
    </div>
</article>

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