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

<div class="row g-3">
    <div class="col-lg-7">
        <div class="card h-100">
            <div class="card-header d-flex justify-content-between align-items-center">
                <span><i class="bi bi-box-arrow-in-right me-2" style="color: var(--brand);"></i>Upcoming Arrivals</span>
                <a href="<?= site_url('bookings?tab=due_in') ?>" class="small text-decoration-none">View All</a>
            </div>
            <div class="list-group list-group-flush">
                <?php if (empty($arrivals)): ?>
                    <div class="list-group-item text-center text-muted py-5">No arrivals scheduled for today.</div>
                <?php endif; ?>
                <?php foreach ($arrivals as $booking):
                    $nights = (int) max(1, (strtotime($booking->check_out_date) - strtotime($booking->check_in_date)) / 86400);
                ?>
                    <div class="list-group-item d-flex align-items-center gap-3">
                        <div class="user-avatar" style="width:36px;height:36px;font-size:0.8rem;"><?= esc(strtoupper(mb_substr($booking->guest_name, 0, 1))) ?></div>
                        <div class="flex-grow-1">
                            <div class="fw-semibold small"><?= esc($booking->guest_name) ?></div>
                            <div class="text-muted" style="font-size: 0.78rem;"><?= esc($booking->room_type_name) ?> · Room <?= esc($booking->room_number) ?> · <?= $nights ?> night<?= $nights > 1 ? 's' : '' ?></div>
                        </div>
                        <span class="badge bg-light text-dark border">BK-<?= sprintf('%04d', $booking->id) ?></span>
                        <a href="<?= site_url('checkin/' . $booking->id) ?>" class="btn btn-sm btn-primary">Check In</a>
                    </div>
                <?php endforeach; ?>
            </div>
        </div>
    </div>

    <div class="col-lg-5">
        <div class="card">
            <div class="card-header d-flex align-items-center gap-2">
                <i class="bi bi-search" style="color: var(--brand);"></i> Find Reservation
            </div>
            <div class="card-body">
                <p class="text-muted small mb-3">Search by confirmation number (e.g. BK-0001), or pick a guest from Upcoming Arrivals.</p>
                <form action="<?= site_url('checkin') ?>" method="get" class="d-flex gap-2">
                    <input type="text" class="form-control" name="q" placeholder="Confirmation number" value="<?= esc($searchQuery ?? '') ?>">
                    <button type="submit" class="btn btn-primary"><i class="bi bi-search"></i></button>
                </form>
                <?php if (! empty($searchError)): ?>
                    <div class="alert alert-warning small mt-3 mb-0"><?= esc($searchError) ?></div>
                <?php endif; ?>
            </div>
        </div>

        <div class="card mt-3">
            <div class="card-body">
                <div class="d-flex align-items-center gap-2 mb-2">
                    <i class="bi bi-plus-circle" style="color: var(--brand);"></i>
                    <span class="fw-semibold small">New Walk-in Guest?</span>
                </div>
                <p class="text-muted small mb-3">Walk-ins go through the normal booking flow — pick today's date and "Walk-in" as the source.</p>
                <a href="<?= site_url('bookings/new') ?>" class="btn btn-outline-primary btn-sm w-100">New Reservation</a>
            </div>
        </div>
    </div>
</div>

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