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

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

<?php
$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' => 'Edit Reconciliation',
    'workspaceTitle'        => 'Update Reconciliation',
    'workspaceDesc'         => $recon['number'] . ' · ' . $bankLabel,
]) ?>

<article class="enterprise-settings-card mb-4" style="max-width:1100px;">
    <div class="enterprise-settings-card__head">
        <i class="fa-solid fa-scale-balanced"></i>
        <h2 class="enterprise-settings-card__title">Reconciliation Details</h2>
    </div>

    <?php if (session()->getFlashdata('errors')): ?>
        <div class="alert alert-danger py-2 small">
            <ul class="mb-0 ps-3">
                <?php foreach ((array) session()->getFlashdata('errors') as $err): ?>
                    <li><?= esc(is_array($err) ? implode(', ', $err) : $err) ?></li>
                <?php endforeach; ?>
            </ul>
        </div>
    <?php endif; ?>

    <form action="<?= base_url('accounting/bank-reconciliations/' . $recon['id']) ?>" method="post" novalidate>
        <?= csrf_field() ?>
        <input type="hidden" name="lines_submitted" value="1">
        <div class="p-4 pb-0">
            <p class="text-muted small mb-3">Bank: <strong><?= esc($bankLabel) ?></strong> · Book balance: <strong><?= number_format((float) $recon['book_balance'], 2) ?></strong></p>
            <?= $this->include('Modules\Accounting\Views\bank_reconciliations\_form', ['includeBank' => false]) ?>
        </div>

        <div class="px-4 pb-3">
            <h3 class="h6 mb-2">Statement Lines</h3>
            <p class="text-muted small">Check items that appear on the bank statement.</p>
            <div class="table-responsive">
                <table class="enterprise-data-table">
                    <thead>
                        <tr>
                            <th style="width:3rem;">Cleared</th>
                            <th>Date</th>
                            <th>Description</th>
                            <th class="text-end">Amount</th>
                        </tr>
                    </thead>
                    <tbody>
                        <?php foreach ($lines as $line): ?>
                            <tr>
                                <td>
                                    <input type="checkbox" name="lines[<?= (int) $line['id'] ?>]" value="1"
                                           <?= (int) $line['is_cleared'] ? 'checked' : '' ?>>
                                </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-4">No journal lines for this period.</td></tr>
                        <?php endif; ?>
                    </tbody>
                </table>
            </div>
            <div class="row g-3 mt-2 small">
                <div class="col-md-4">Cleared total: <strong><?= number_format($clearedSum, 2) ?></strong></div>
                <div class="col-md-4">Uncleared total: <strong><?= number_format($unclearedSum, 2) ?></strong></div>
                <div class="col-md-4">Adjusted book: <strong><?= number_format($adjusted, 2) ?></strong>
                    <?php if (abs($diff) > 0.01): ?>
                        <span class="text-danger">(diff <?= number_format($diff, 2) ?>)</span>
                    <?php else: ?>
                        <span class="text-success">(matched)</span>
                    <?php endif; ?>
                </div>
            </div>
        </div>

        <div class="enterprise-settings-card__footer gap-2">
            <a href="<?= base_url('accounting/bank-reconciliations/' . $recon['id']) ?>" class="btn btn-outline-secondary">Cancel</a>
            <button type="submit" class="btn btn-primary">Save Changes</button>
        </div>
    </form>
</article>

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