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

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

<?php
$statusPill = static function (string $status): string {
    $map = [
        'cleared'    => 'active',
        'registered' => 'draft',
        'issued'     => 'draft',
        'bounced'    => 'inactive',
        'cancelled'  => 'inactive',
    ];
    $class = $map[$status] ?? 'default';
    return '<span class="enterprise-status-pill enterprise-status-pill--' . esc($class) . '">' . esc(ucfirst($status)) . '</span>';
};
$st        = $cheque['status'];
$canEdit   = in_array($st, ['registered', 'issued'], true);
$canClear  = in_array($st, ['registered', 'issued'], true);
$canBounce = in_array($st, ['registered', 'issued'], true);
$canCancel = in_array($st, ['registered', 'issued', 'bounced'], true);
$canDelete = in_array($st, ['registered', 'issued'], true);

$bankLabel = ! empty($cheque['bank_account_code'])
    ? ($cheque['bank_account_code'] . ' · ' . ($cheque['bank_account_name'] ?? ''))
    : '—';
$cashLabel = ! empty($cheque['cash_account_code'])
    ? ($cheque['cash_account_code'] . ' · ' . ($cheque['cash_account_name'] ?? ''))
    : '—';
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Cheque',
    'workspaceTitle'        => $cheque['number'],
    'workspaceDesc'         => ucfirst($cheque['direction']) . ' · ' . $cheque['party_name'],
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-money-check"></i>
        <h2 class="enterprise-settings-card__title">Cheque 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($cheque['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">Cheque #</dt>
            <dd class="col-sm-3"><?= esc($cheque['cheque_number']) ?></dd>
            <dt class="col-sm-3 text-muted">Direction</dt>
            <dd class="col-sm-3"><?= esc(ucfirst($cheque['direction'])) ?></dd>
            <dt class="col-sm-3 text-muted">Party</dt>
            <dd class="col-sm-3"><?= esc($cheque['party_name']) ?></dd>
            <dt class="col-sm-3 text-muted">Date</dt>
            <dd class="col-sm-3"><?= esc($cheque['cheque_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Amount</dt>
            <dd class="col-sm-3"><?= number_format((float) $cheque['amount'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Bank Account</dt>
            <dd class="col-sm-3"><?= esc($bankLabel) ?></dd>
            <dt class="col-sm-3 text-muted">Cash / Offset</dt>
            <dd class="col-sm-3"><?= esc($cashLabel) ?></dd>
            <dt class="col-sm-3 text-muted">Cleared At</dt>
            <dd class="col-sm-3"><?= esc($cheque['cleared_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Journal</dt>
            <dd class="col-sm-3">
                <?php if (! empty($cheque['journal_entry_id'])): ?>
                    <a href="<?= base_url('accounting/journal-entries/' . $cheque['journal_entry_id']) ?>">#<?= (int) $cheque['journal_entry_id'] ?></a>
                <?php else: ?>
                    —
                <?php endif; ?>
            </dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($cheque['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/cheques') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($canEdit): ?>
            <a href="<?= base_url('accounting/cheques/' . $cheque['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
        <?php endif; ?>
        <?php if ($canClear): ?>
            <form action="<?= base_url('accounting/cheques/' . $cheque['id'] . '/clear') ?>" method="post" class="d-inline" onsubmit="return confirm('Clear this cheque and post the journal?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Clear</button>
            </form>
        <?php endif; ?>
        <?php if ($canBounce): ?>
            <form action="<?= base_url('accounting/cheques/' . $cheque['id'] . '/bounce') ?>" method="post" class="d-inline" onsubmit="return confirm('Mark as bounced?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-warning">Bounce</button>
            </form>
        <?php endif; ?>
        <?php if ($canCancel): ?>
            <form action="<?= base_url('accounting/cheques/' . $cheque['id'] . '/cancel') ?>" method="post" class="d-inline" onsubmit="return confirm('Cancel this cheque?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-secondary">Cancel</button>
            </form>
        <?php endif; ?>
        <?php if ($canDelete): ?>
            <form action="<?= base_url('accounting/cheques/' . $cheque['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this cheque?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-danger">Delete</button>
            </form>
        <?php endif; ?>
    </div>
</article>

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