<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Edit Trip') }}
        </h2>
    </x-slot>

    <div class="py-6">
        <div class="max-w-3xl mx-auto sm:px-6 lg:px-8">
            <div class="bg-white shadow-sm sm:rounded-lg">
                <div class="p-6 text-gray-900">
                    
                    @if($errors->any())
                        <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-4">
                            <ul>
                                @foreach($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    <form action="{{ route('trips.update', $trip) }}" method="POST">
                        @csrf
                        @method('PUT')
                        @php $isDriver = auth()->user()->hasRole('Driver'); @endphp
                        
                        <div class="space-y-4">
                            <div>
                                <label class="block text-sm font-bold text-gray-700">Date</label>
                                <input type="date" name="date" value="{{ old('date', $trip->date->format('Y-m-d')) }}" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 {{ $isDriver ? 'bg-gray-100 cursor-not-allowed' : '' }}" {{ $isDriver ? 'readonly' : '' }}>
                            </div>

                            <div>
                                <label class="block text-sm font-bold text-gray-700">Location</label>
                                <input type="text" name="location" value="{{ old('location', $trip->location) }}" placeholder="e.g. Chennai to Madurai" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 {{ $isDriver ? 'bg-gray-100 cursor-not-allowed' : '' }}" {{ $isDriver ? 'readonly' : '' }}>
                            </div>

                            <div>
                                <label class="block text-sm font-bold text-gray-700">Vehicle</label>
                                <select name="vehicle_id" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 {{ $isDriver ? 'bg-gray-100 pointer-events-none' : '' }}" {{ $isDriver ? 'tabindex="-1"' : '' }}>
                                    <option value="">Select Vehicle</option>
                                    @foreach($vehicles as $v)
                                        <option value="{{ $v->id }}" {{ $trip->vehicle_id == $v->id ? 'selected' : '' }}>{{ $v->vehicle_number }}</option>
                                    @endforeach
                                </select>
                            </div>

                            <div>
                                <label class="block text-sm font-bold text-gray-700">Driver</label>
                                @if(isset($linkedDriver) && $linkedDriver)
                                    <input type="hidden" name="driver_id" value="{{ $linkedDriver->id }}">
                                    <div class="mt-1 block w-full rounded-md border border-gray-300 bg-gray-50 px-3 py-2 text-gray-700 font-semibold">
                                        {{ $linkedDriver->driver_name }}
                                    </div>
                                @else
                                    <select name="driver_id" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200">
                                        <option value="">Select Driver</option>
                                        @foreach($drivers as $d)
                                            <option value="{{ $d->id }}" {{ $trip->driver_id == $d->id ? 'selected' : '' }}>{{ $d->driver_name }}</option>
                                        @endforeach
                                    </select>
                                @endif
                            </div>

                            <div>
                                <label class="block text-sm font-bold text-gray-700">Trip Type</label>
                                <select name="trip_type" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 {{ $isDriver ? 'bg-gray-100 pointer-events-none' : '' }}" {{ $isDriver ? 'tabindex="-1"' : '' }}>
                                    <option value="rent" {{ $trip->trip_type == 'rent' ? 'selected' : '' }}>Rent</option>
                                    <option value="own" {{ $trip->trip_type == 'own' ? 'selected' : '' }}>Own</option>
                                    <option value="empty" {{ $trip->trip_type == 'empty' ? 'selected' : '' }}>Empty</option>
                                </select>
                            </div>

                            <!-- KM Tracking -->
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-bold text-gray-700">Open KM</label>
                                    <input type="number" name="open_km" value="{{ old('open_km', $trip->open_km) }}" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm">
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-gray-700">Close KM</label>
                                    <input type="number" name="close_km" value="{{ old('close_km', $trip->close_km) }}" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm">
                                </div>
                            </div>

                            <!-- Hour Tracking -->
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-bold text-gray-700">Open Hour</label>
                                    <input type="time" name="open_hour" value="{{ old('open_hour', $trip->open_hour) }}" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm">
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-gray-700">Close Hour</label>
                                    <input type="time" name="close_hour" value="{{ old('close_hour', $trip->close_hour) }}" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm">
                                </div>
                            </div>

                            <!-- Financials -->
                            <div>
                                <label class="block text-sm font-bold text-gray-700">Rent Amount (₹)</label>
                                <input type="number" step="0.01" name="rent_amount" value="{{ old('rent_amount', $trip->rent_amount) }}" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm font-bold text-green-600 {{ $isDriver ? 'bg-gray-100 cursor-not-allowed' : '' }}" {{ $isDriver ? 'readonly' : '' }}>
                            </div>

                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-bold text-gray-700">Fuel (Litres)</label>
                                    <input type="number" step="0.01" name="fuel_litre" value="{{ old('fuel_litre', $trip->fuel_litre) }}" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm {{ $isDriver ? 'bg-gray-100 cursor-not-allowed' : '' }}" {{ $isDriver ? 'readonly' : '' }}>
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-gray-700">Diesel Price</label>
                                    <input type="number" step="0.01" name="diesel_price" value="{{ old('diesel_price', $trip->diesel_price) }}" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm {{ $isDriver ? 'bg-gray-100 cursor-not-allowed' : '' }}" {{ $isDriver ? 'readonly' : '' }}>
                                </div>
                            </div>
                        </div>

                        <div class="fixed bottom-0 left-0 w-full p-4 bg-white border-t sm:relative sm:bg-transparent sm:border-0 sm:p-0 sm:mt-6 sm:pb-0 z-50">
                            <button type="submit" class="w-full bg-green-600 text-white font-bold text-lg py-4 rounded-lg shadow-lg hover:bg-green-700 active:bg-green-800 transition">
                                Update Trip
                            </button>
                        </div>
                        <div class="h-20 sm:hidden"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>
