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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Transfer',
    'workspaceTitle'        => $transfer['number'],
    'workspaceDesc'         => ($transfer['from_label'] ?? '') . ' → ' . ($transfer['to_label'] ?? ''),
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-right-left"></i>
        <h2 class="enterprise-settings-card__title">Transfer 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($transfer['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($transfer['transfer_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Amount</dt>
            <dd class="col-sm-3"><?= number_format((float) $transfer['amount'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">From</dt>
            <dd class="col-sm-3"><?= esc($transfer['from_label'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">To</dt>
            <dd class="col-sm-3"><?= esc($transfer['to_label'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Reference</dt>
            <dd class="col-sm-3"><?= esc($transfer['reference'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Journal</dt>
            <dd class="col-sm-3">
                <?php if (! empty($transfer['journal_entry_id'])): ?>
                    <a href="<?= base_url('accounting/journal-entries/' . $transfer['journal_entry_id']) ?>">#<?= (int) $transfer['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($transfer['posted_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($transfer['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/bank-transfers') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/bank-transfers/' . $transfer['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/bank-transfers/' . $transfer['id'] . '/post') ?>" method="post" class="d-inline" onsubmit="return confirm('Post this transfer?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Post Transfer</button>
            </form>
            <form action="<?= base_url('accounting/bank-transfers/' . $transfer['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this transfer?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-danger">Delete</button>
            </form>
        <?php endif; ?>
    </div>
</article>

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