<?php
require_once __DIR__ . '/config/config.php';
$pageTitle = 'Cars for Sale';
include __DIR__ . '/includes/header.php';
?>

<style>
 body { background-color: #f3f4f6; }
 .card-3d { transition: transform 0.3s ease, box-shadow 0.3s ease; }
 .card-3d:hover { transform: translateY(-8px); box-shadow: 0 20px 40px rgba(33, 21, 148, 0.15); }
 #loanCalculatorSection { transition: all 0.5s ease-in-out; }
</style>

<section class="bg-gradient-to-br from-primary-blue to-blue-900 text-white py-16 md:py-20">
    <div class="container mx-auto px-4 text-center">
        <h1 class="text-4xl md:text-5xl font-bold mb-4">Cars for Sale</h1>
        <p class="text-lg md:text-xl text-gray-100">Find your perfect vehicle with flexible financing options</p>
    </div>
</section>

<section class="container mx-auto px-4 py-8">
    <div class="bg-white rounded-2xl p-6 shadow-lg">
        <div class="grid md:grid-cols-4 gap-4">
            <div>
                <label class="block text-sm font-semibold text-gray-700 mb-2">Price Range</label>
                <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-blue">
                    <option>All Prices</option>
                    <option>Under 500K</option>
                    <option>500K - 1M</option>
                    <option>1M - 2M</option>
                    <option>Above 2M</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-semibold text-gray-700 mb-2">Year</label>
                <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-blue">
                    <option>All Years</option>
                    <option>2023-2024</option>
                    <option>2020-2022</option>
                    <option>2017-2019</option>
                    <option>2015-2016</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-semibold text-gray-700 mb-2">Make</label>
                <select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-blue">
                    <option>All Makes</option>
                    <option>Toyota</option>
                    <option>Nissan</option>
                    <option>Honda</option>
                    <option>Mazda</option>
                    <option>Subaru</option>
                </select>
            </div>
            <div class="flex items-end">
                <button class="w-full bg-primary-blue text-white py-2 rounded-lg font-semibold hover:bg-opacity-90 transition">
                    Search
                </button>
            </div>
        </div>
    </div>
</section>

<section class="container mx-auto px-4 py-8 pb-16">
    <div class="grid md:grid-cols-2 lg:grid-cols-3 gap-6">
        <div class="card-3d bg-white rounded-2xl overflow-hidden shadow-lg">
            <div class="relative aspect-video bg-gray-200">
                <img src="assets/images/Toyota-Corolla-Fielder.jpg" alt="Toyota Fielder" class="w-full h-full object-cover">
                <div class="absolute top-3 right-3 bg-primary-green text-white px-3 py-1 rounded-full text-xs font-semibold">Featured</div>
            </div>
            <div class="p-5">
                <h3 class="font-bold text-xl text-gray-800 mb-2">Toyota Fielder</h3>
                <p class="text-sm text-gray-500 mb-3">Year: 2018</p>
                <div class="space-y-2 mb-4">
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                        </svg>
                        <span>1.5L Engine</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Automatic Transmission</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Low Mileage, Excellent Condition</span>
                    </div>
                </div>
                <p class="text-3xl md:text-4xl font-bold text-primary-blue mb-1">KES 50,000<span class="text-lg">/month</span></p>
                <p class="text-xs text-gray-500 mb-4">Total: KES 1,200,000 • 24 months</p>
                <button onclick="openBuyModal('Toyota Fielder', 'KES 1,200,000')" class="w-full bg-primary-blue text-white py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">
                    Buy Now
                </button>
            </div>
        </div>

        <div class="card-3d bg-white rounded-2xl overflow-hidden shadow-lg">
            <div class="relative aspect-video bg-gray-200">
                <img src="assets/images/Nissan-note.jpeg" alt="Nissan Note" class="w-full h-full object-cover">
            </div>
            <div class="p-5">
                <h3 class="font-bold text-xl text-gray-800 mb-2">Nissan Note</h3>
                <p class="text-sm text-gray-500 mb-3">Year: 2019</p>
                <div class="space-y-2 mb-4">
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                        </svg>
                        <span>1.2L Engine</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>CVT Transmission</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Fuel Efficient, City Driving</span>
                    </div>
                </div>
                <p class="text-3xl md:text-4xl font-bold text-primary-blue mb-1">KES 39,583<span class="text-lg">/month</span></p>
                <p class="text-xs text-gray-500 mb-4">Total: KES 950,000 • 24 months</p>
                <button onclick="openBuyModal('Nissan Note', 'KES 950,000')" class="w-full bg-primary-blue text-white py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">
                    Buy Now
                </button>
            </div>
        </div>

        <div class="card-3d bg-white rounded-2xl overflow-hidden shadow-lg">
            <div class="relative aspect-video bg-gray-200">
                <img src="assets/images/honda-fit.jpg" alt="Honda Fit" class="w-full h-full object-cover">
            </div>
            <div class="p-5">
                <h3 class="font-bold text-xl text-gray-800 mb-2">Honda Fit</h3>
                <p class="text-sm text-gray-500 mb-3">Year: 2017</p>
                <div class="space-y-2 mb-4">
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                        </svg>
                        <span>1.3L Engine</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Automatic Transmission</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Spacious Interior, Reliable</span>
                    </div>
                </div>
                <p class="text-3xl md:text-4xl font-bold text-primary-blue mb-1">KES 35,417<span class="text-lg">/month</span></p>
                <p class="text-xs text-gray-500 mb-4">Total: KES 850,000 • 24 months</p>
                <button onclick="openBuyModal('Honda Fit', 'KES 850,000')" class="w-full bg-primary-blue text-white py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">
                    Buy Now
                </button>
            </div>
        </div>

        <div class="card-3d bg-white rounded-2xl overflow-hidden shadow-lg">
            <div class="relative aspect-video bg-gray-200">
                <img src="assets/images/mazda demio.jpeg" alt="Mazda Demio" class="w-full h-full object-cover">
            </div>
            <div class="p-5">
                <h3 class="font-bold text-xl text-gray-800 mb-2">Mazda Demio</h3>
                <p class="text-sm text-gray-500 mb-3">Year: 2016</p>
                <div class="space-y-2 mb-4">
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                        </svg>
                        <span>1.3L Engine</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Automatic Transmission</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Compact Design, Easy Parking</span>
                    </div>
                </div>
                <p class="text-3xl md:text-4xl font-bold text-primary-blue mb-1">KES 32,500<span class="text-lg">/month</span></p>
                <p class="text-xs text-gray-500 mb-4">Total: KES 780,000 • 24 months</p>
                <button onclick="openBuyModal('Mazda Demio', 'KES 780,000')" class="w-full bg-primary-blue text-white py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">
                    Buy Now
                </button>
            </div>
        </div>

        <div class="card-3d bg-white rounded-2xl overflow-hidden shadow-lg">
            <div class="relative aspect-video bg-gray-200">
                <img src="assets/images/Subaru Impreza.jpeg" alt="Subaru Impreza" class="w-full h-full object-cover">
                <div class="absolute top-3 right-3 bg-primary-green text-white px-3 py-1 rounded-full text-xs font-semibold">New Arrival</div>
            </div>
            <div class="p-5">
                <h3 class="font-bold text-xl text-gray-800 mb-2">Subaru Impreza</h3>
                <p class="text-sm text-gray-500 mb-3">Year: 2020</p>
                <div class="space-y-2 mb-4">
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                        </svg>
                        <span>2.0L Engine</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>AWD, Sport Mode</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Premium Features</span>
                    </div>
                </div>
                <p class="text-3xl md:text-4xl font-bold text-primary-blue mb-1">KES 75,000<span class="text-lg">/month</span></p>
                <p class="text-xs text-gray-500 mb-4">Total: KES 1,800,000 • 24 months</p>
                <button onclick="openBuyModal('Subaru Impreza', 'KES 1,800,000')" class="w-full bg-primary-blue text-white py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">
                    Buy Now
                </button>
            </div>
        </div>

        <div class="card-3d bg-white rounded-2xl overflow-hidden shadow-lg">
            <div class="relative aspect-video bg-gray-200">
                <img src="assets/images/Toyota Vitz.jpeg" alt="Toyota Vitz" class="w-full h-full object-cover">
            </div>
            <div class="p-5">
                <h3 class="font-bold text-xl text-gray-800 mb-2">Toyota Vitz</h3>
                <p class="text-sm text-gray-500 mb-3">Year: 2015</p>
                <div class="space-y-2 mb-4">
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 10V3L4 14h7v7l9-11h-7z"/>
                        </svg>
                        <span>1.0L Engine</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M12 8v4l3 3m6-3a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Manual Transmission</span>
                    </div>
                    <div class="flex items-center gap-2 text-sm text-gray-600">
                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 12l2 2 4-4m6 2a9 9 0 11-18 0 9 9 0 0118 0z"/>
                        </svg>
                        <span>Economical, Perfect First Car</span>
                    </div>
                </div>
                <p class="text-3xl md:text-4xl font-bold text-primary-blue mb-1">KES 27,083<span class="text-lg">/month</span></p>
                <p class="text-xs text-gray-500 mb-4">Total: KES 650,000 • 24 months</p>
                <button onclick="openBuyModal('Toyota Vitz', 'KES 650,000')" class="w-full bg-primary-blue text-white py-3 rounded-lg font-semibold hover:bg-opacity-90 transition">
                    Buy Now
                </button>
            </div>
        </div>
    </div>
</section>

<section class="bg-gradient-to-br from-primary-blue to-blue-900 py-16">
    <div class="container mx-auto px-4 text-center">
        <h2 class="text-3xl md:text-4xl font-bold text-white mb-4">Need Financing?</h2>
        <p class="text-lg text-gray-100 mb-8 max-w-2xl mx-auto">Get pre-approved for a car loan in minutes. Flexible payment plans available!</p>
        <button onclick="toggleCalculator()" class="inline-block bg-primary-green text-white px-8 py-4 rounded-full font-semibold hover:bg-opacity-90 transition shadow-xl">
            Calculate Your Loan
        </button>
    </div>
</section>

<section id="loanCalculatorSection" class="bg-gradient-to-br from-soft-gray to-white py-12 hidden">
    <div class="container mx-auto px-4">
        <div class="text-center mb-8">
            <h2 class="text-3xl md:text-4xl font-bold text-primary-blue mb-3">Loan Calculator</h2>
            <p class="text-gray-600">Calculate your loan repayment instantly</p>
        </div>
        <div class="max-w-3xl mx-auto">
            <div class="bg-white rounded-2xl p-6 md:p-8 shadow-xl">
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="space-y-6">
                        <div>
                            <div class="flex justify-between items-center mb-2">
                                <label class="text-sm font-semibold text-gray-700">Loan Amount</label>
                                <span class="text-lg font-bold text-primary-blue" id="loanAmountDisplay">KES 1,000</span>
                            </div>
                            <input type="range" id="loanAmount" min="1000" max="5000000" step="1000" value="1000"
                                   class="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-primary-blue"
                                   oninput="updateLoanCalculator()">
                            <div class="flex justify-between text-xs text-gray-500 mt-1">
                                <span>1K</span>
                                <span>5M</span>
                            </div>
                        </div>
                        <div>
                            <div class="flex justify-between items-center mb-2">
                                <label class="text-sm font-semibold text-gray-700">Loan Period</label>
                                <span class="text-lg font-bold text-primary-green" id="loanPeriodDisplay">12 Months</span>
                            </div>
                            <input type="range" id="loanPeriod" min="1" max="24" value="12"
                                   class="w-full h-2 bg-gray-200 rounded-lg appearance-none cursor-pointer accent-primary-green"
                                   oninput="updateLoanCalculator()">
                            <div class="flex justify-between text-xs text-gray-500 mt-1">
                                <span>1 Month</span>
                                <span>24 Months</span>
                            </div>
                        </div>
                        <input type="hidden" id="interestRate" value="4">
                    </div>
                    <div class="bg-gradient-to-br from-primary-blue/5 to-primary-green/5 rounded-xl p-6 flex flex-col justify-center">
                        <div class="space-y-4">
                            <div>
                                <p class="text-xs text-gray-600 mb-1">Monthly Payment</p>
                                <p class="text-2xl md:text-3xl font-bold text-primary-blue" id="monthlyPayment">KES 0</p>
                            </div>
                            <div class="border-t pt-3">
                                <p class="text-xs text-gray-600 mb-1">Total Repayment</p>
                                <p class="text-xl font-bold text-gray-800" id="totalRepayment">KES 0</p>
                            </div>
                            <div>
                                <p class="text-xs text-gray-600 mb-1">Interest (4% Flat Rate)</p>
                                <p class="text-lg font-semibold text-primary-green" id="totalInterest">KES 0</p>
                            </div>
                        </div>
                    </div>
                </div>
                <button onclick="openLoanApplicationModal()" 
                        class="w-full mt-6 bg-gradient-to-r from-primary-blue to-primary-green text-white px-8 py-3 rounded-full font-bold hover:opacity-90 transition shadow-lg hover:shadow-xl">
                    Apply for This Loan
                </button>
                <button onclick="toggleCalculator()" 
                        class="w-full mt-3 bg-gray-200 text-gray-700 px-8 py-3 rounded-full font-semibold hover:bg-gray-300 transition">
                    Hide Calculator
                </button>
            </div>
        </div>
    </div>
</section>

<div id="loanApplicationModal" class="fixed inset-0 bg-black bg-opacity-50 z-50 hidden flex items-center justify-center p-4">
    <div class="bg-white rounded-2xl max-w-md w-full p-6 md:p-8 relative">
        <button onclick="closeLoanApplicationModal()" class="absolute top-4 right-4 text-gray-400 hover:text-gray-600 transition">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
            </svg>
        </button>
        <h3 class="text-2xl font-bold text-primary-blue mb-2">Apply for Loan</h3>
        <p class="text-sm text-gray-600 mb-6">Complete your application details</p>
        <form id="loanApplicationForm" class="space-y-4">
            <div class="bg-gray-50 rounded-lg p-4 space-y-2">
                <div class="flex justify-between text-sm">
                    <span class="text-gray-600">Loan Amount:</span>
                    <span class="font-bold text-primary-blue" id="modalLoanAmount">KES 0</span>
                </div>
                <div class="flex justify-between text-sm">
                    <span class="text-gray-600">Period:</span>
                    <span class="font-bold text-primary-green" id="modalLoanPeriod">0 Months</span>
                </div>
                <div class="flex justify-between text-sm">
                    <span class="text-gray-600">Monthly Payment:</span>
                    <span class="font-bold text-gray-800" id="modalMonthlyPayment">KES 0</span>
                </div>
            </div>
            <div>
                <label for="modalApplicantName" class="block text-sm font-semibold text-gray-700 mb-2">Full Name *</label>
                <input type="text" id="modalApplicantName" name="fullName" required
                       class="w-full px-4 py-3 border-2 border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-blue focus:border-transparent transition"
                       placeholder="Enter your full name">
            </div>
            <div>
                <label for="modalApplicantPhone" class="block text-sm font-semibold text-gray-700 mb-2">Phone Number *</label>
                <input type="tel" id="modalApplicantPhone" name="phoneNumber" required
                       class="w-full px-4 py-3 border-2 border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-blue focus:border-transparent transition"
                       placeholder="+254 700 000 000">
            </div>
            <button type="submit" 
                    class="w-full bg-gradient-to-r from-primary-blue to-primary-green text-white px-8 py-3 rounded-full font-bold hover:opacity-90 transition shadow-lg hover:shadow-xl">
                Submit Application
            </button>
        </form>
    </div>
</div>

<div id="buyModal" class="fixed inset-0 bg-black bg-opacity-50 z-50 hidden flex items-center justify-center p-4">
    <div class="bg-white rounded-2xl max-w-md w-full p-6 md:p-8 relative">
        <button onclick="closeBuyModal()" class="absolute top-4 right-4 text-gray-400 hover:text-gray-600 transition">
            <svg class="w-6 h-6" fill="none" stroke="currentColor" viewBox="0 0 24 24">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"/>
            </svg>
        </button>
        <h3 class="text-2xl font-bold text-primary-blue mb-2">Purchase Inquiry</h3>
        <p class="text-sm text-gray-600 mb-6">Enter your details to proceed with purchase</p>
        <form id="buyForm" class="space-y-4">
            <div class="bg-gray-50 rounded-lg p-4 space-y-2">
                <div class="flex justify-between text-sm">
                    <span class="text-gray-600">Car:</span>
                    <span class="font-bold text-primary-blue" id="modalCarName">-</span>
                </div>
                <div class="flex justify-between text-sm">
                    <span class="text-gray-600">Price:</span>
                    <span class="font-bold text-primary-green" id="modalCarPrice">KES 0</span>
                </div>
            </div>
            <div>
                <label for="buyerName" class="block text-sm font-semibold text-gray-700 mb-2">Full Name *</label>
                <input type="text" id="buyerName" name="fullName" required
                       class="w-full px-4 py-3 border-2 border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-blue focus:border-transparent transition"
                       placeholder="Enter your full name">
            </div>
            <div>
                <label for="buyerPhone" class="block text-sm font-semibold text-gray-700 mb-2">Phone Number *</label>
                <input type="tel" id="buyerPhone" name="phoneNumber" required
                       class="w-full px-4 py-3 border-2 border-gray-200 rounded-lg focus:outline-none focus:ring-2 focus:ring-primary-blue focus:border-transparent transition"
                       placeholder="+254 700 000 000">
            </div>
            <button type="submit" 
                    class="w-full bg-gradient-to-r from-primary-blue to-blue-600 text-white px-8 py-3 rounded-full font-bold hover:opacity-90 transition shadow-lg hover:shadow-xl">
                Submit Inquiry
            </button>
        </form>
    </div>
</div>

<script>
function toggleCalculator() {
    const calculator = document.getElementById('loanCalculatorSection');
    calculator.classList.toggle('hidden');
    if (!calculator.classList.contains('hidden')) {
        calculator.scrollIntoView({ behavior: 'smooth', block: 'start' });
    }
}

function updateLoanCalculator() {
    const loanAmount = parseFloat(document.getElementById('loanAmount').value);
    const loanPeriod = parseFloat(document.getElementById('loanPeriod').value);
    // Flat 4% monthly interest on principal
    const monthlyInterest = loanAmount * 0.04;
    const totalInterest = monthlyInterest * loanPeriod;
    const totalRepayment = loanAmount + totalInterest;
    const monthlyPayment = totalRepayment / loanPeriod;
    document.getElementById('loanAmountDisplay').textContent = 'KES ' + loanAmount.toLocaleString('en-KE');
    document.getElementById('loanPeriodDisplay').textContent = loanPeriod + ' Month' + (loanPeriod > 1 ? 's' : '');
    document.getElementById('monthlyPayment').textContent = 'KES ' + monthlyPayment.toLocaleString('en-KE', {maximumFractionDigits: 0});
    document.getElementById('totalInterest').textContent = 'KES ' + totalInterest.toLocaleString('en-KE', {maximumFractionDigits: 0});
    document.getElementById('totalRepayment').textContent = 'KES ' + totalRepayment.toLocaleString('en-KE', {maximumFractionDigits: 0});
}

window.addEventListener('DOMContentLoaded', function() {
    updateLoanCalculator();
});

function openLoanApplicationModal() {
    const loanAmount = parseFloat(document.getElementById('loanAmount').value);
    const loanPeriod = parseFloat(document.getElementById('loanPeriod').value);
    const monthlyPayment = document.getElementById('monthlyPayment').textContent;
    document.getElementById('modalLoanAmount').textContent = 'KES ' + loanAmount.toLocaleString('en-KE');
    document.getElementById('modalLoanPeriod').textContent = loanPeriod + ' Month' + (loanPeriod > 1 ? 's' : '');
    document.getElementById('modalMonthlyPayment').textContent = monthlyPayment;
    document.getElementById('loanApplicationModal').classList.remove('hidden');
    document.body.style.overflow = 'hidden';
}

function closeLoanApplicationModal() {
    document.getElementById('loanApplicationModal').classList.add('hidden');
    document.body.style.overflow = 'auto';
}

document.addEventListener('DOMContentLoaded', function() {
    const loanForm = document.getElementById('loanApplicationForm');
    if (loanForm) {
        loanForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const loanAmount = parseFloat(document.getElementById('loanAmount').value);
            const loanPeriod = parseFloat(document.getElementById('loanPeriod').value);
            const applicantName = document.getElementById('modalApplicantName').value;
            const applicantPhone = document.getElementById('modalApplicantPhone').value;
            const monthlyPayment = document.getElementById('monthlyPayment').textContent;
            alert('Loan Application Submitted!\n\n' +
                'Name: ' + applicantName + '\n' +
                'Phone: ' + applicantPhone + '\n' +
                'Loan Amount: KES ' + loanAmount.toLocaleString('en-KE') + '\n' +
                'Period: ' + loanPeriod + ' months\n' +
                'Monthly Payment: ' + monthlyPayment + '\n\n' +
                'Our team will contact you shortly!');
            closeLoanApplicationModal();
            loanForm.reset();
        });
    }
});

function openBuyModal(carName, carPrice) {
    document.getElementById('modalCarName').textContent = carName;
    document.getElementById('modalCarPrice').textContent = carPrice;
    document.getElementById('buyModal').classList.remove('hidden');
    document.body.style.overflow = 'hidden';
}

function closeBuyModal() {
    document.getElementById('buyModal').classList.add('hidden');
    document.body.style.overflow = 'auto';
}

document.addEventListener('DOMContentLoaded', function() {
    const buyForm = document.getElementById('buyForm');
    if (buyForm) {
        buyForm.addEventListener('submit', function(e) {
            e.preventDefault();
            const buyerName = document.getElementById('buyerName').value;
            const buyerPhone = document.getElementById('buyerPhone').value;
            const carName = document.getElementById('modalCarName').textContent;
            const carPrice = document.getElementById('modalCarPrice').textContent;
            alert('Purchase Inquiry Submitted!\n\n' +
                'Car: ' + carName + '\n' +
                'Price: ' + carPrice + '\n' +
                'Name: ' + buyerName + '\n' +
                'Phone: ' + buyerPhone + '\n\n' +
                'Our sales team will contact you shortly!');
            closeBuyModal();
            buyForm.reset();
        });
    }
});

window.addEventListener('click', function(event) {
    const loanModal = document.getElementById('loanApplicationModal');
    const buyModal = document.getElementById('buyModal');
    if (event.target === loanModal) closeLoanApplicationModal();
    if (event.target === buyModal) closeBuyModal();
});
</script>

<?php include __DIR__ . '/includes/footer.php'; ?>
