<?= $this->extend('App\Modules\Frontend\Views\_layout') ?>

<?= $this->section('meta_title') ?>
Book: <?= esc($tour['title'] ?? 'Expedition') ?> | Mount Kenya Treks
<?= $this->endSection() ?>

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

<?php
// Define countries locally to prevent "Undefined variable" error
$countries = ['Afghanistan','Albania','Algeria','Andorra','Angola','Argentina','Armenia','Australia','Austria','Azerbaijan','Bahamas','Bahrain','Bangladesh','Barbados','Belarus','Belgium','Belize','Benin','Bhutan','Bolivia','Bosnia and Herzegovina','Botswana','Brazil','Brunei','Bulgaria','Burkina Faso','Burundi','Cabo Verde','Cambodia','Cameroon','Canada','Central African Republic','Chad','Chile','China','Colombia','Comoros','Congo','Costa Rica','Croatia','Cuba','Cyprus','Czechia','Denmark','Djibouti','Dominica','Dominican Republic','Ecuador','Egypt','El Salvador','Eritrea','Estonia','Eswatini','Ethiopia','Fiji','Finland','France','Gabon','Gambia','Georgia','Germany','Ghana','Greece','Guatemala','Guinea','Guyana','Haiti','Honduras','Hungary','Iceland','India','Indonesia','Iran','Iraq','Ireland','Israel','Italy','Jamaica','Japan','Jordan','Kazakhstan','Kenya','Kuwait','Kyrgyzstan','Laos','Latvia','Lebanon','Lesotho','Liberia','Libya','Lithuania','Luxembourg','Madagascar','Malawi','Malaysia','Maldives','Mali','Malta','Mauritania','Mauritius','Mexico','Moldova','Monaco','Mongolia','Montenegro','Morocco','Mozambique','Myanmar','Namibia','Nepal','Netherlands','New Zealand','Nicaragua','Niger','Nigeria','North Macedonia','Norway','Oman','Pakistan','Palestine','Panama','Papua New Guinea','Paraguay','Peru','Philippines','Poland','Portugal','Qatar','Romania','Russia','Rwanda','Saudi Arabia','Senegal','Serbia','Seychelles','Sierra Leone','Singapore','Slovakia','Slovenia','South Africa','South Korea','South Sudan','Spain','Sri Lanka','Sudan','Suriname','Sweden','Switzerland','Syria','Taiwan','Tajikistan','Tanzania','Thailand','Togo','Trinidad and Tobago','Tunisia','Turkey','Turkmenistan','Uganda','Ukraine','United Arab Emirates','United Kingdom','United States','Uruguay','Uzbekistan','Venezuela','Vietnam','Yemen','Zambia','Zimbabwe'];

// Safely handle images
$images = !empty($tour['images']) ? json_decode($tour['images'], true) : [];
$basePrice = (float)($tour['price_usd'] ?? 0);
?>

<div class="bg-[#F8FAFC] min-h-screen font-sans text-navy pb-32 lg:pb-16">
    <div class="bg-white border-b border-border sticky top-0 z-50 shadow-sm">
        <div class="container mx-auto px-6 py-4 flex items-center justify-between">
            <a href="<?= base_url('tours/' . ($tour['slug'] ?? '')) ?>" class="group flex items-center gap-2 text-muted hover:text-secondary transition-colors">
                <svg class="w-4 h-4 group-hover:-translate-x-1 transition-transform" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="3">
                    <path stroke-linecap="round" stroke-linejoin="round" d="M15 19l-7-7 7-7" />
                </svg>
                <span class="text-[10px] font-black uppercase tracking-widest text-navy">Back to Details</span>
            </a>

            <div class="flex items-center gap-6">
                <div class="flex items-center gap-2">
                    <span class="w-6 h-6 rounded-full bg-secondary flex items-center justify-center text-[10px] font-black text-white">1</span>
                    <span class="hidden sm:inline text-[10px] font-black uppercase tracking-widest">Details</span>
                </div>
                <div class="w-8 h-px bg-border"></div>
                <div class="flex items-center gap-2 opacity-40">
                    <span class="w-6 h-6 rounded-full bg-light border border-border flex items-center justify-center text-[10px] font-black">2</span>
                    <span class="hidden sm:inline text-[10px] font-black uppercase tracking-widest text-muted">Confirm</span>
                </div>
            </div>

            <div class="hidden md:flex items-center gap-2 px-3 py-1 bg-green-50 rounded-full border border-green-100">
                <span class="w-1.5 h-1.5 bg-green-500 rounded-full animate-pulse"></span>
                <span class="text-[9px] font-black uppercase tracking-widest text-green-700">Inventory Reserved</span>
            </div>
        </div>
    </div>

    <main class="container mx-auto px-6 py-12">
        <div class="grid grid-cols-1 lg:grid-cols-12 gap-12">
            
            <div class="lg:col-span-7">
                <header class="mb-10">
                    <h1 class="text-4xl font-black tracking-tight uppercase leading-none mb-3">Secure Your <span class="text-secondary">Spot</span></h1>
                    <p class="text-muted text-sm max-w-lg">Complete the registration below. Your KWS permits and logistics will be processed immediately after confirmation.</p>
                </header>

                <form action="<?= route_to('booking.store') ?>" method="post" id="bookingForm" class="space-y-8">
                    <?= csrf_field() ?>
                    <input type="hidden" name="tour_id" value="<?= esc($tour['id'] ?? '') ?>">

                    <div class="bg-white p-8 rounded-[2rem] border border-border shadow-sm">
                        <h2 class="text-[11px] font-black uppercase tracking-[0.3em] text-secondary mb-8 flex items-center gap-2">
                            <span class="w-2 h-2 bg-secondary rounded-full"></span>
                            Primary Climber
                        </h2>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-x-8 gap-y-6">
                            <div class="space-y-2">
                                <label for="client_name" class="text-[10px] font-black uppercase tracking-widest text-muted">Legal Full Name</label>
                                <input type="text" id="client_name" name="client_name" required placeholder="As shown on Passport"
                                    class="w-full bg-light/30 border border-border rounded-xl px-5 py-4 text-navy font-bold focus:bg-white focus:border-secondary focus:ring-4 focus:ring-secondary/5 transition-all outline-none">
                            </div>

                            <div class="space-y-2">
                                <label for="email" class="text-[10px] font-black uppercase tracking-widest text-muted">Email Address</label>
                                <input type="email" id="email" name="email" required placeholder="Expedition itinerary destination"
                                    class="w-full bg-light/30 border border-border rounded-xl px-5 py-4 text-navy font-bold focus:bg-white focus:border-secondary focus:ring-4 focus:ring-secondary/5 transition-all outline-none">
                            </div>

                            <div class="space-y-2">
                                <label for="phone" class="text-[10px] font-black uppercase tracking-widest text-muted">WhatsApp / Phone</label>
                                <input type="tel" id="phone" name="phone" required placeholder="+1 234..."
                                    class="w-full bg-light/30 border border-border rounded-xl px-5 py-4 text-navy font-bold focus:bg-white focus:border-secondary focus:ring-4 focus:ring-secondary/5 transition-all outline-none">
                            </div>

                            <div class="space-y-2">
                                <label for="country" class="text-[10px] font-black uppercase tracking-widest text-muted">Country of Origin</label>
                                <div class="relative">
                                    <select id="country" name="country" required class="w-full bg-light/30 border border-border rounded-xl px-5 py-4 text-navy font-bold focus:bg-white focus:border-secondary transition-all outline-none appearance-none">
                                        <option value="">Select Country</option>
                                        <?php foreach ($countries as $c): ?>
                                            <option value="<?= esc($c) ?>"><?= esc($c) ?></option>
                                        <?php endforeach; ?>
                                    </select>
                                    <div class="absolute right-4 top-1/2 -translate-y-1/2 pointer-events-none text-muted">
                                        <svg class="w-4 h-4" fill="none" viewBox="0 0 24 24" stroke="currentColor"><path d="M19 9l-7 7-7-7" /></svg>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="bg-white p-8 rounded-[2rem] border border-border shadow-sm">
                        <h2 class="text-[11px] font-black uppercase tracking-[0.3em] text-secondary mb-8 flex items-center gap-2">
                            <span class="w-2 h-2 bg-secondary rounded-full"></span>
                            Logistics Details
                        </h2>
                        
                        <div class="grid grid-cols-1 md:grid-cols-2 gap-8">
                            <div class="space-y-2">
                                <label for="travel_date" class="text-[10px] font-black uppercase tracking-widest text-muted">Preferred Start Date</label>
                                <input type="date" id="travel_date" name="travel_date" required 
                                    min="<?= date('Y-m-d', strtotime('+7 days')) ?>"
                                    class="w-full bg-light/30 border border-border rounded-xl px-5 py-4 text-navy font-bold focus:border-secondary transition-all outline-none">
                                <p class="text-[9px] text-muted font-medium italic">Minimum 7-day lead time required.</p>
                            </div>

                            <div class="space-y-2">
                                <label class="text-[10px] font-black uppercase tracking-widest text-muted">Number of Climbers</label>
                                <div class="flex items-center border border-border rounded-xl overflow-hidden bg-light/30">
                                    <button type="button" id="minusPax" class="px-5 py-4 hover:bg-white transition-colors border-r border-border text-navy font-black">-</button>
                                    <input type="number" id="pax" name="pax" value="1" min="1" max="12" readonly
                                        class="w-full bg-transparent text-center font-black text-navy border-none focus:ring-0">
                                    <button type="button" id="plusPax" class="px-5 py-4 hover:bg-white transition-colors border-l border-border text-navy font-black">+</button>
                                </div>
                            </div>
                        </div>
                    </div>

                    <button type="submit" class="hidden lg:flex w-full bg-primary hover:bg-secondary text-white font-black uppercase tracking-[0.2em] py-6 rounded-2xl transition-all shadow-xl hover:-translate-y-1 items-center justify-center gap-3">
                        Secure Expedition Details
                        <svg class="w-5 h-5" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="3">
                            <path stroke-linecap="round" stroke-linejoin="round" d="M13 7l5 5m0 0l-5 5m5-5H6" />
                        </svg>
                    </button>
                </form>
            </div>

            <div class="lg:col-span-5">
                <div class="sticky top-24 space-y-6">
                    <div class="bg-navy rounded-[2.5rem] overflow-hidden shadow-2xl text-white">
                        <div class="relative h-44 bg-navy-900">
                            <?php if (!empty($images) && isset($images[0])): ?>
                                <img src="<?= base_url('assets/uploads/tours/' . $images[0]) ?>" class="w-full h-full object-cover opacity-50">
                            <?php else: ?>
                                <div class="w-full h-full bg-primary/20 flex items-center justify-center text-[10px] uppercase font-bold tracking-widest">Expedition View</div>
                            <?php endif; ?>
                            <div class="absolute inset-0 bg-gradient-to-t from-navy via-navy/30 to-transparent"></div>
                            <div class="absolute bottom-6 left-8">
                                <p class="text-[9px] font-black uppercase tracking-[0.2em] text-secondary mb-1">Trip Summary</p>
                                <h3 class="text-xl font-black uppercase leading-tight"><?= esc($tour['title'] ?? 'Selected Tour') ?></h3>
                            </div>
                        </div>

                        <div class="p-8 space-y-4">
                            <div class="flex justify-between text-sm">
                                <span class="text-white/40 font-bold uppercase tracking-widest text-[10px]">Expedition Length</span>
                                <span class="font-black"><?= esc($tour['duration_days'] ?? '0') ?> Days</span>
                            </div>
                            <div class="flex justify-between text-sm">
                                <span class="text-white/40 font-bold uppercase tracking-widest text-[10px]">Per Climber Cost</span>
                                <span class="font-black text-secondary">USD <?= number_format($basePrice) ?></span>
                            </div>
                            
                            <div class="pt-6 border-t border-white/10 mt-6">
                                <div class="flex justify-between items-end">
                                    <div>
                                        <p class="text-[10px] font-black uppercase tracking-[0.2em] text-secondary mb-2">Grand Total (Incl. KWS Fees)</p>
                                        <div class="flex items-baseline gap-2">
                                            <span class="text-3xl font-black tabular-nums tracking-tighter" id="totalDisplay">0</span>
                                            <span class="text-sm font-bold text-white/40">USD</span>
                                        </div>
                                    </div>
                                    <div class="text-right">
                                        <p class="text-[8px] font-black text-white/20 uppercase tracking-widest leading-none">All Inclusive<br>Pricing</p>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    <div class="grid grid-cols-2 gap-4">
                        <div class="bg-white p-5 rounded-2xl border border-border flex flex-col gap-2">
                            <svg class="w-6 h-6 text-secondary" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path d="M9 12l2 2 4-4m5.618-4.016A11.955 11.955 0 0112 2.944a11.955 11.955 0 01-8.618 3.04A12.02 12.02 0 003 9c0 5.591 3.824 10.29 9 11.622 5.176-1.332 9-6.03 9-11.622 0-1.042-.133-2.052-.382-3.016z" /></svg>
                            <p class="text-[9px] font-black uppercase tracking-widest leading-tight text-navy">Certified KWS Logistics</p>
                        </div>
                        <div class="bg-white p-5 rounded-2xl border border-border flex flex-col gap-2">
                            <svg class="w-6 h-6 text-secondary" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2"><path d="M13 10V3L4 14h7v7l9-11h-7z" /></svg>
                            <p class="text-[9px] font-black uppercase tracking-widest leading-tight text-navy">AMREF Evacuation Cover</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </main>

    <div class="fixed bottom-0 left-0 right-0 lg:hidden z-[60] bg-white border-t border-border p-5 shadow-[0_-10px_40px_rgba(0,0,0,0.1)]">
        <div class="flex items-center gap-4 max-w-lg mx-auto">
            <div class="shrink-0">
                <p class="text-[8px] font-black text-muted uppercase tracking-widest leading-none mb-1">Estimated Total</p>
                <p class="text-xl font-black text-navy tabular-nums" id="mobileTotalDisplay">0</p>
            </div>
            <button form="bookingForm" type="submit" class="flex-1 bg-primary text-white font-black uppercase tracking-widest py-4 rounded-xl shadow-lg active:scale-95 transition-transform text-xs">
                Continue
            </button>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', () => {
    const paxInput = document.getElementById('pax');
    const plusBtn = document.getElementById('plusPax');
    const minusBtn = document.getElementById('minusPax');
    const totalDisplay = document.getElementById('totalDisplay');
    const mobileTotalDisplay = document.getElementById('mobileTotalDisplay');
    const basePrice = <?= $basePrice ?>;

    const updateTotal = () => {
        const pax = parseInt(paxInput.value) || 1;
        const total = basePrice * pax;
        const formatted = total.toLocaleString('en-US');
        totalDisplay.textContent = formatted;
        mobileTotalDisplay.textContent = 'USD ' + formatted;
    };

    plusBtn?.addEventListener('click', () => {
        if (parseInt(paxInput.value) < 12) {
            paxInput.value = parseInt(paxInput.value) + 1;
            updateTotal();
        }
    });

    minusBtn?.addEventListener('click', () => {
        if (parseInt(paxInput.value) > 1) {
            paxInput.value = parseInt(paxInput.value) - 1;
            updateTotal();
        }
    });

    updateTotal();
});
</script>

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