<?php
require_once __DIR__ . '/../../includes/functions.php';
require_once __DIR__ . '/../../includes/accounting.php';
requireLogin();

$id = (int)($_GET['id'] ?? 0);
$stmt = $pdo->prepare("SELECT je.*, u.name as created_by_name FROM journal_entries je LEFT JOIN users u ON u.id=je.created_by WHERE je.id=?");
$stmt->execute([$id]);
$je = $stmt->fetch();
if (!$je) { setFlash('danger','Entry not found.'); header('Location: journal.php'); exit; }

$lines = $pdo->prepare("SELECT jl.*, a.account_code, a.account_name, a.account_type FROM journal_lines jl JOIN accounts a ON a.id=jl.account_id WHERE jl.journal_id=? ORDER BY jl.debit DESC");
$lines->execute([$id]);
$lines = $lines->fetchAll();

$pageTitle = 'Journal Entry ' . $je['entry_no'];
require_once __DIR__ . '/../../includes/header.php';
?>
<div class="page-header no-print">
    <div><div class="eyebrow">Accounts</div><h2 class="mb-0"><?= sanitize($je['entry_no']) ?></h2></div>
    <div class="flex gap-8">
        <a href="journal.php" class="btn"><?= icon('arrow-left') ?> Back</a>
        <button class="btn" onclick="window.print()"><?= icon('download') ?> Print</button>
    </div>
</div>

<div class="card" style="max-width:750px;margin:0 auto;">
    <div class="flex-between" style="margin-bottom:16px;">
        <div>
            <h3 style="margin:0;"><?= sanitize($je['entry_no']) ?></h3>
            <div class="text-sm text-muted">Date: <?= formatDate($je['entry_date']) ?> &nbsp;|&nbsp; Type: <span class="badge"><?= ucfirst($je['entry_type']) ?></span></div>
            <?php if ($je['reference_type']): ?>
            <div class="text-sm text-muted">Reference: <?= sanitize(ucfirst($je['reference_type'])) ?> #<?= sanitize($je['reference_id']) ?></div>
            <?php endif; ?>
        </div>
        <div class="text-right">
            <div class="text-sm text-muted">Created by: <?= sanitize($je['created_by_name'] ?? '-') ?></div>
            <div class="text-sm text-muted"><?= formatDateTime($je['created_at']) ?></div>
        </div>
    </div>

    <div style="background:var(--color-surface-2);padding:10px 14px;border-radius:var(--radius-md);margin-bottom:16px;">
        <strong>Narration:</strong> <?= sanitize($je['narration']) ?>
    </div>

    <div class="table-wrap">
        <table>
            <thead>
                <tr>
                    <th>Account Code</th>
                    <th>Account Name</th>
                    <th>Line Note</th>
                    <th class="text-right">Debit</th>
                    <th class="text-right">Credit</th>
                </tr>
            </thead>
            <tbody>
            <?php foreach ($lines as $line): ?>
            <tr>
                <td class="mono"><?= sanitize($line['account_code']) ?></td>
                <td><?= sanitize($line['account_name']) ?></td>
                <td class="text-sm text-muted"><?= sanitize($line['narration'] ?? '') ?></td>
                <td class="num mono"><?= $line['debit'] > 0 ? formatMoney($line['debit']) : '-' ?></td>
                <td class="num mono"><?= $line['credit'] > 0 ? formatMoney($line['credit']) : '-' ?></td>
            </tr>
            <?php endforeach; ?>
            </tbody>
            <tfoot>
                <tr>
                    <td colspan="3" style="font-weight:700;text-align:right;">Total</td>
                    <td class="num mono"><strong><?= formatMoney($je['total_debit']) ?></strong></td>
                    <td class="num mono"><strong><?= formatMoney($je['total_credit']) ?></strong></td>
                </tr>
            </tfoot>
        </table>
    </div>

    <div class="flex-between mt-16 text-sm text-faint">
        <span>Balanced: <?= abs($je['total_debit'] - $je['total_credit']) < 0.01 ? '✅ Yes' : '❌ No' ?></span>
        <span>Entry No: <?= sanitize($je['entry_no']) ?></span>
    </div>
</div>

<?php require_once __DIR__ . '/../../includes/footer.php'; ?>
