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

<div class="d-flex justify-content-between align-items-start mb-3">
    <div>
        <h1 class="h3 mb-1">Booking #<?= $booking->id ?></h1>
        <span class="badge bg-<?= esc($lookups->bookingStatuses[$booking->status]['badge'] ?? 'secondary') ?>">
            <?= esc($lookups->bookingStatuses[$booking->status]['label'] ?? $booking->status) ?>
        </span>
    </div>
</div>

<?php if ($pendingDeposit): ?>
<div class="card shadow-sm mb-3">
    <div class="card-body">
        <h2 class="h6">Deposit Payment Pending</h2>
        <p class="small text-muted mb-2">This booking is held until the <?= format_money($pendingDeposit->amount) ?> deposit is paid. Send the guest this link:</p>
        <div class="input-group input-group-sm mb-2">
            <input type="text" class="form-control" value="<?= esc($pendingDeposit->checkout_url) ?>" readonly onclick="this.select()">
        </div>
        <p class="small text-muted mb-0">Reference: <?= esc($pendingDeposit->client_reference) ?> · <span class="badge bg-warning text-dark">Awaiting payment</span></p>
    </div>
</div>
<?php endif; ?>

<div class="row">
    <div class="col-lg-8 mb-3">
        <div class="card shadow-sm">
            <div class="card-header p-0">
                <ul class="nav nav-tabs border-0 px-2 pt-2" id="bookingTabs">
                    <li class="nav-item"><a class="nav-link active" data-bs-toggle="tab" href="#tab-reservation">Reservation Details</a></li>
                    <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-guest">Guest Details</a></li>
                    <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-payment">Payment Info</a></li>
                    <li class="nav-item"><a class="nav-link" data-bs-toggle="tab" href="#tab-notes">Notes</a></li>
                </ul>
            </div>
            <div class="card-body">
                <div class="tab-content">
                    <div class="tab-pane fade show active" id="tab-reservation">
                        <div class="row row-cols-2 g-3 small">
                            <div class="col"><span class="text-muted">Confirmation #</span><br><span class="fw-semibold">BK-<?= sprintf('%04d', $booking->id) ?></span></div>
                            <div class="col"><span class="text-muted">Source</span><br><span class="fw-semibold"><?= esc(ucfirst(str_replace('_', ' ', $booking->source))) ?></span></div>
                            <div class="col"><span class="text-muted">Room</span><br><span class="fw-semibold"><a href="<?= site_url('rooms/' . $booking->room_id) ?>" class="text-decoration-none"><?= esc($booking->room_number) ?></a> (<?= esc($booking->room_type_name) ?>)</span></div>
                            <div class="col"><span class="text-muted">Rate</span><br><span class="fw-semibold"><?= format_money($booking->rate_applied) ?> / night</span></div>
                            <div class="col"><span class="text-muted">Check-in</span><br><span class="fw-semibold"><?= esc(date('d M Y', strtotime($booking->check_in_date))) ?></span></div>
                            <div class="col"><span class="text-muted">Check-out</span><br><span class="fw-semibold"><?= esc(date('d M Y', strtotime($booking->check_out_date))) ?></span></div>
                            <div class="col"><span class="text-muted">Nights</span><br><span class="fw-semibold"><?= $nights ?></span></div>
                            <div class="col"><span class="text-muted">Created</span><br><span class="fw-semibold"><?= esc(date('d M Y, g:i a', strtotime($booking->created_at))) ?></span></div>
                        </div>
                    </div>

                    <div class="tab-pane fade" id="tab-guest">
                        <?php if ($guest): ?>
                        <div class="row row-cols-2 g-3 small">
                            <div class="col"><span class="text-muted">Name</span><br><a href="<?= site_url('guests/' . $guest->id) ?>" class="fw-semibold text-decoration-none"><?= esc($guest->full_name) ?></a></div>
                            <div class="col"><span class="text-muted">Phone</span><br><span class="fw-semibold"><?= esc($guest->phone) ?></span></div>
                            <div class="col"><span class="text-muted">Email</span><br><span class="fw-semibold"><?= esc($guest->email ?: '—') ?></span></div>
                            <div class="col"><span class="text-muted">Nationality</span><br><span class="fw-semibold"><?= esc($guest->nationality ?: '—') ?></span></div>
                            <div class="col"><span class="text-muted">ID Type</span><br><span class="fw-semibold"><?= esc($lookups->guestIdTypes[$guest->id_type]['label'] ?? '—') ?></span></div>
                            <div class="col"><span class="text-muted">ID Number</span><br><span class="fw-semibold"><?= esc($guest->id_number ?: '—') ?></span></div>
                        </div>
                        <a href="<?= site_url('guests/' . $guest->id) ?>" class="btn btn-sm btn-outline-primary mt-3">View Full Guest Profile</a>
                        <?php else: ?>
                            <p class="text-muted small mb-0">Guest record not found.</p>
                        <?php endif; ?>
                    </div>

                    <div class="tab-pane fade" id="tab-payment">
                        <?php if (! $folio): ?>
                            <p class="text-muted small mb-0">No folio yet — charges and payments begin at check-in.</p>
                        <?php else: ?>
                            <div class="row row-cols-3 g-3 small mb-3">
                                <div class="col"><span class="text-muted">Total Charges</span><br><span class="fw-semibold"><?= format_money($folioTotal) ?></span></div>
                                <div class="col"><span class="text-muted">Amount Paid</span><br><span class="fw-semibold"><?= format_money($folioPaid) ?></span></div>
                                <div class="col"><span class="text-muted">Balance Due</span><br><span class="fw-semibold <?= (float) $folioBalance > 0 ? 'text-danger' : 'text-success' ?>"><?= format_money($folioBalance) ?></span></div>
                            </div>
                            <h3 class="h6">Charges</h3>
                            <table class="table table-sm mb-3">
                                <thead class="table-light"><tr><th>Date</th><th>Description</th><th class="text-end">Amount</th></tr></thead>
                                <tbody>
                                <?php if (empty($charges)): ?>
                                    <tr><td colspan="3" class="text-center text-muted py-2">No charges yet.</td></tr>
                                <?php endif; ?>
                                <?php foreach ($charges as $item): ?>
                                    <tr>
                                        <td class="small"><?= esc($item->item_date) ?></td>
                                        <td class="small"><?= esc($item->description) ?></td>
                                        <td class="text-end small"><?= format_money($item->amount) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                            <h3 class="h6">Payments</h3>
                            <table class="table table-sm mb-0">
                                <thead class="table-light"><tr><th>When</th><th>Method</th><th class="text-end">Amount</th></tr></thead>
                                <tbody>
                                <?php if (empty($payments)): ?>
                                    <tr><td colspan="3" class="text-center text-muted py-2">No payments yet.</td></tr>
                                <?php endif; ?>
                                <?php foreach ($payments as $payment): ?>
                                    <tr class="<?= $payment->is_refund ? 'text-danger' : '' ?>">
                                        <td class="small"><?= esc($payment->paid_at) ?></td>
                                        <td class="small"><?= esc($lookups->paymentMethods[$payment->method]['label'] ?? $payment->method) ?></td>
                                        <td class="text-end small"><?= $payment->is_refund ? '&minus; ' : '' ?><?= format_money($payment->amount) ?></td>
                                    </tr>
                                <?php endforeach; ?>
                                </tbody>
                            </table>
                        <?php endif; ?>
                    </div>

                    <div class="tab-pane fade" id="tab-notes">
                        <p class="mb-0 small"><?= $booking->notes ? nl2br(esc($booking->notes)) : '<span class="text-muted">No notes on this booking.</span>' ?></p>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <?php if ($folio): ?>
        <div class="card shadow-sm mb-3">
            <div class="card-header">Charges Summary</div>
            <div class="card-body">
                <div class="d-flex justify-content-between small mb-1"><span>Total Charges</span><strong><?= format_money($folioTotal) ?></strong></div>
                <div class="d-flex justify-content-between small mb-1"><span>Amount Paid</span><strong><?= format_money($folioPaid) ?></strong></div>
                <hr class="my-2">
                <div class="d-flex justify-content-between">
                    <span>Balance Due</span>
                    <strong class="<?= (float) $folioBalance > 0 ? 'text-danger' : 'text-success' ?>"><?= format_money($folioBalance) ?></strong>
                </div>
            </div>
        </div>
        <?php endif; ?>

        <div class="card shadow-sm">
            <div class="card-header">Quick Actions</div>
            <div class="card-body d-flex flex-column gap-2">
                <?php if ($folio): ?>
                    <a href="<?= site_url('folios/' . $folio->id) ?>" class="btn btn-primary btn-sm">View Folio</a>
                <?php endif; ?>
                <?php if (in_array($booking->status, ['confirmed', 'pending_deposit', 'checked_in'], true)): ?>
                    <a href="<?= site_url('bookings/' . $booking->id . '/change-room') ?>" class="btn btn-outline-primary btn-sm">Change Room</a>
                <?php endif; ?>
                <?php if (in_array($booking->status, ['confirmed', 'pending_deposit'], true)): ?>
                    <form action="<?= site_url('bookings/' . $booking->id . '/cancel') ?>" method="post" onsubmit="return confirm('Cancel this booking?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-danger btn-sm w-100">Cancel Booking</button>
                    </form>
                    <form action="<?= site_url('bookings/' . $booking->id . '/no-show') ?>" method="post" onsubmit="return confirm('Mark this booking as a no-show?');">
                        <?= csrf_field() ?>
                        <button type="submit" class="btn btn-outline-dark btn-sm w-100">Mark No-Show</button>
                    </form>
                <?php endif; ?>
                <a href="<?= site_url('bookings/calendar') ?>" class="btn btn-outline-secondary btn-sm">Back to Calendar</a>
            </div>
        </div>
    </div>
</div>

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