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

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Journal',
    'workspaceTitle'        => $journal['number'],
    'workspaceDesc'         => $journal['description'] ?: 'Journal entry detail.',
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-book"></i>
        <h2 class="enterprise-settings-card__title">Entry 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($journal['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($journal['entry_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Period</dt>
            <dd class="col-sm-3"><?= esc($journal['fiscal_year_code'] ?? '—') ?></dd>
            <dt class="col-sm-3 text-muted">Reference</dt>
            <dd class="col-sm-3"><?= esc($journal['reference'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Posted At</dt>
            <dd class="col-sm-3"><?= esc($journal['posted_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Description</dt>
            <dd class="col-sm-9"><?= esc($journal['description'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/journal-entries') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/journal-entries/' . $journal['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/journal-entries/' . $journal['id'] . '/post') ?>" method="post" class="d-inline" onsubmit="return confirm('Post this journal entry?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Post Entry</button>
            </form>
        <?php elseif ($st === 'posted'): ?>
            <form action="<?= base_url('accounting/journal-entries/' . $journal['id'] . '/unpost') ?>" method="post" class="d-inline" onsubmit="return confirm('Unpost this journal entry?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-secondary">Unpost</button>
            </form>
        <?php endif; ?>
    </div>
</article>

<div class="enterprise-data-panel">
    <div class="enterprise-data-panel__head">
        <h2 class="enterprise-data-panel__title">Lines</h2>
        <p class="enterprise-data-panel__subtitle">Debit <?= number_format((float) $journal['total_debit'], 2) ?> · Credit <?= number_format((float) $journal['total_credit'], 2) ?></p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Account</th>
                        <th>Description</th>
                        <th class="text-end">Debit</th>
                        <th class="text-end">Credit</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($lines as $line): ?>
                        <tr>
                            <td><code><?= esc($line['account_code'] ?? '') ?></code> <?= esc($line['account_name'] ?? '') ?></td>
                            <td><?= esc($line['description'] ?: '—') ?></td>
                            <td class="text-end"><?= (float) $line['debit'] > 0 ? number_format((float) $line['debit'], 2) : '—' ?></td>
                            <td class="text-end"><?= (float) $line['credit'] > 0 ? number_format((float) $line['credit'], 2) : '—' ?></td>
                        </tr>
                    <?php endforeach; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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