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

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

<?php
$statusPill = static function (string $status): string {
    $map = ['completed' => '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 = $recon['status'];
$bankLabel = ! empty($recon['bank_account_code'])
    ? ($recon['bank_account_code'] . ' · ' . ($recon['bank_account_name'] ?? ''))
    : '—';

$unclearedSum = 0.0;
$clearedSum   = 0.0;
foreach ($lines as $line) {
    if ((int) $line['is_cleared']) {
        $clearedSum += (float) $line['amount'];
    } else {
        $unclearedSum += (float) $line['amount'];
    }
}
$adjusted = round((float) $recon['book_balance'] - $unclearedSum, 2);
$diff     = round($adjusted - (float) $recon['statement_ending_balance'], 2);
?>

<?= view('partials/workspace_hero', [
    'workspaceCrumbPill'    => 'Accounting',
    'workspaceCrumbCurrent' => 'Reconciliation',
    'workspaceTitle'        => $recon['number'],
    'workspaceDesc'         => $bankLabel . ' · ' . ($recon['statement_date'] ?? ''),
]) ?>

<article class="enterprise-settings-card mb-4">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-scale-balanced"></i>
        <h2 class="enterprise-settings-card__title">Reconciliation 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($recon['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">Bank Account</dt>
            <dd class="col-sm-3"><?= esc($bankLabel) ?></dd>
            <dt class="col-sm-3 text-muted">Statement Date</dt>
            <dd class="col-sm-3"><?= esc($recon['statement_date']) ?></dd>
            <dt class="col-sm-3 text-muted">Statement Ending</dt>
            <dd class="col-sm-3"><?= number_format((float) $recon['statement_ending_balance'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Book Balance</dt>
            <dd class="col-sm-3"><?= number_format((float) $recon['book_balance'], 2) ?></dd>
            <dt class="col-sm-3 text-muted">Adjusted Book</dt>
            <dd class="col-sm-3">
                <?= number_format($adjusted, 2) ?>
                <?php if (abs($diff) > 0.01): ?>
                    <span class="text-danger small">(diff <?= number_format($diff, 2) ?>)</span>
                <?php else: ?>
                    <span class="text-success small">(matched)</span>
                <?php endif; ?>
            </dd>
            <dt class="col-sm-3 text-muted">Completed At</dt>
            <dd class="col-sm-3"><?= esc($recon['completed_at'] ?: '—') ?></dd>
            <dt class="col-sm-3 text-muted">Notes</dt>
            <dd class="col-sm-9"><?= esc($recon['notes'] ?: '—') ?></dd>
        </dl>
    </div>
    <div class="enterprise-settings-card__footer gap-2">
        <a href="<?= base_url('accounting/bank-reconciliations') ?>" class="btn btn-outline-secondary">Back</a>
        <?php if ($st === 'draft'): ?>
            <a href="<?= base_url('accounting/bank-reconciliations/' . $recon['id'] . '/edit') ?>" class="btn btn-outline-secondary">Edit</a>
            <form action="<?= base_url('accounting/bank-reconciliations/' . $recon['id'] . '/complete') ?>" method="post" class="d-inline" onsubmit="return confirm('Complete this reconciliation?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-primary">Complete</button>
            </form>
            <form action="<?= base_url('accounting/bank-reconciliations/' . $recon['id'] . '/delete') ?>" method="post" class="d-inline" onsubmit="return confirm('Delete this reconciliation?');">
                <?= csrf_field() ?>
                <button type="submit" class="btn btn-outline-danger">Delete</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">
            Cleared <?= number_format($clearedSum, 2) ?> · Uncleared <?= number_format($unclearedSum, 2) ?>
        </p>
    </div>
    <div class="enterprise-data-panel__body">
        <div class="table-responsive">
            <table class="enterprise-data-table">
                <thead>
                    <tr>
                        <th>Cleared</th>
                        <th>Date</th>
                        <th>Description</th>
                        <th class="text-end">Amount</th>
                    </tr>
                </thead>
                <tbody>
                    <?php foreach ($lines as $line): ?>
                        <tr>
                            <td><?= (int) $line['is_cleared'] ? 'Yes' : 'No' ?></td>
                            <td><?= esc($line['txn_date'] ?: '—') ?></td>
                            <td><?= esc($line['description'] ?: '—') ?></td>
                            <td class="text-end"><?= number_format((float) $line['amount'], 2) ?></td>
                        </tr>
                    <?php endforeach; ?>
                    <?php if (count($lines) === 0): ?>
                        <tr><td colspan="4" class="text-center text-muted py-5">No lines.</td></tr>
                    <?php endif; ?>
                </tbody>
            </table>
        </div>
    </div>
</div>

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