<?php $this->start('body');
    $je    = $this->data['je'];
    $lines = $this->data['lines'] ?? [];
    $sc    = ['POSTED'=>'bg-success','REVERSED'=>'bg-secondary'][$je['status']] ?? 'bg-light text-muted';
?>

<div class="d-flex align-items-center justify-content-between mb-3">
    <div>
        <h5 class="mb-0" style="font-weight:600;"><i class="fas fa-file-alt me-2 text-muted"></i><?= htmlspecialchars($je['je_no']); ?></h5>
        <div class="text-muted" style="font-size:.72rem;">
            <?= htmlspecialchars($je['entry_date']); ?> ·
            <span class="badge bg-light text-muted" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($je['source_type'])); ?></span> ·
            <?= htmlspecialchars($je['source_ref']); ?> ·
            <span class="badge <?= $sc; ?>" style="text-transform:capitalize;"><?= htmlspecialchars(strtolower($je['status'])); ?></span>
        </div>
    </div>
    <a href="<?= PROOT; ?>Journals" class="btn btn-sm btn-outline-secondary"><i class="fas fa-arrow-left me-1"></i> All journals</a>
</div>

<div class="card border-0 shadow-sm mb-3">
    <div class="card-header bg-white"><strong>Narrative</strong></div>
    <div class="card-body p-3 text-muted"><?= nl2br(htmlspecialchars($je['narrative'])); ?></div>
</div>

<div class="card border-0 shadow-sm">
    <div class="card-header bg-white d-flex align-items-center justify-content-between">
        <strong><i class="fas fa-balance-scale me-2 text-muted"></i>Journal lines</strong>
        <div class="text-muted small">
            Total DR <strong><?= number_format((float)$je['total_debit']); ?></strong> ·
            Total CR <strong><?= number_format((float)$je['total_credit']); ?></strong>
            <?php if (abs((float)$je['total_debit'] - (float)$je['total_credit']) < 0.005): ?>
                <span class="badge bg-success ms-2">Balanced</span>
            <?php else: ?>
                <span class="badge bg-danger ms-2">Out of balance</span>
            <?php endif; ?>
        </div>
    </div>
    <div class="table-responsive">
        <table class="table table-sm mb-0 align-middle">
            <thead class="table-light">
                <tr>
                    <th style="width:40px;">#</th>
                    <th>Account</th><th>Narrative</th>
                    <th>Project</th><th>LPO</th><th>Bank</th>
                    <th class="text-end">Debit</th><th class="text-end">Credit</th>
                </tr>
            </thead>
            <tbody>
                <?php foreach ($lines as $ln): ?>
                    <tr>
                        <td class="text-muted"><?= (int)$ln['line_no']; ?></td>
                        <td><code class="text-body"><?= htmlspecialchars($ln['account_code']); ?></code> · <?= htmlspecialchars($ln['account_name']); ?></td>
                        <td class="text-muted small"><?= htmlspecialchars($ln['narrative']); ?></td>
                        <td class="text-muted small"><?= (int)$ln['project_id'] ? '#' . (int)$ln['project_id'] : '—'; ?></td>
                        <td class="text-muted small"><?= (int)$ln['lpo_id'] ? '<a href="' . PROOT . 'Lpos/view/' . (int)$ln['lpo_id'] . '">#' . (int)$ln['lpo_id'] . '</a>' : '—'; ?></td>
                        <td class="text-muted small"><?= (int)$ln['bank_account_id'] ? '#' . (int)$ln['bank_account_id'] : '—'; ?></td>
                        <td class="text-end"><?= (float)$ln['debit']  > 0 ? number_format((float)$ln['debit'])  : '—'; ?></td>
                        <td class="text-end"><?= (float)$ln['credit'] > 0 ? number_format((float)$ln['credit']) : '—'; ?></td>
                    </tr>
                <?php endforeach; ?>
            </tbody>
        </table>
    </div>
</div>

<?php $this->end(); ?>
