<?= $this->extend('layouts/main') ?>
<?= $this->section('content') ?>

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card mb-3">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-person" style="color: var(--brand);"></i> Guest &amp; Stay
            </div>
            <div class="card-body">
                <div class="row g-3">
                    <div class="col-md-6">
                        <div class="text-muted small text-uppercase fw-bold mb-1">Room</div>
                        <div class="fw-semibold"><?= esc($booking->room_number) ?></div>
                        <div class="text-muted small"><?= esc($booking->room_type_name) ?></div>
                    </div>
                    <div class="col-md-6">
                        <div class="text-muted small text-uppercase fw-bold mb-1">Stay</div>
                        <div class="fw-semibold"><?= esc(date('d M', strtotime($booking->check_in_date))) ?> &rarr; <?= esc(date('d M Y', strtotime($booking->check_out_date))) ?></div>
                    </div>
                </div>
            </div>
        </div>

        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-cash-coin" style="color: var(--brand);"></i> Record Payment
            </div>
            <div class="card-body">
                <form action="<?= site_url('checkout/' . $booking->id . '/payment') ?>" method="post" class="row g-2 align-items-end">
                    <?= csrf_field() ?>
                    <div class="col-md-5">
                        <label class="form-label small fw-semibold">Amount</label>
                        <input type="number" step="0.01" min="0.01" class="form-control" name="amount" required value="<?= esc($balance) ?>">
                    </div>
                    <div class="col-md-5">
                        <label class="form-label small fw-semibold">Method</label>
                        <select class="form-select" name="method" required>
                            <?php foreach ($lookups->manualPaymentMethods() as $key => $meta): ?>
                                <option value="<?= esc($key) ?>"><?= esc($meta['label']) ?></option>
                            <?php endforeach; ?>
                        </select>
                    </div>
                    <div class="col-md-2">
                        <button type="submit" class="btn btn-outline-primary w-100">Add</button>
                    </div>
                </form>
            </div>
        </div>
    </div>

    <div class="col-lg-5">
        <div class="card">
            <div class="card-header">Checkout Summary</div>
            <div class="card-body">
                <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Total Charges</span><span class="fw-semibold"><?= format_money($total) ?></span></div>
                <div class="d-flex justify-content-between small mb-2"><span class="text-muted">Amount Paid</span><span class="fw-semibold"><?= format_money($paid) ?></span></div>
                <hr>
                <div class="d-flex justify-content-between mb-3">
                    <span class="fw-semibold">Balance Due</span>
                    <span class="fw-bold fs-5 <?= (float) $balance > 0 ? 'text-danger' : 'text-success' ?>"><?= format_money($balance) ?></span>
                </div>

                <form action="<?= site_url('checkout/' . $booking->id) ?>" method="post" onsubmit="return confirm('Complete checkout?');">
                    <?= csrf_field() ?>
                    <button type="submit" class="btn btn-success w-100" <?= (float) $balance > 0 ? 'disabled' : '' ?>>
                        <i class="bi bi-check2-circle me-1"></i>Complete Checkout
                    </button>
                    <?php if ((float) $balance > 0): ?>
                        <div class="form-text text-danger mt-2">Settle the balance in full before completing checkout.</div>
                    <?php endif; ?>
                </form>

                <?php if ($folio): ?>
                    <a href="<?= site_url('folios/' . $folio->id) ?>" class="small text-decoration-none d-inline-block mt-3"><i class="bi bi-arrow-up-right me-1"></i>View Full Folio</a>
                <?php endif; ?>
            </div>
        </div>
    </div>
</div>

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