<?php if (isset($component)) { $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54 = $component; } ?>
<?php if (isset($attributes)) { $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54 = $attributes; } ?>
<?php $component = App\View\Components\AppLayout::resolve([] + (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag ? $attributes->all() : [])); ?>
<?php $component->withName('app-layout'); ?>
<?php if ($component->shouldRender()): ?>
<?php $__env->startComponent($component->resolveView(), $component->data()); ?>
<?php if (isset($attributes) && $attributes instanceof Illuminate\View\ComponentAttributeBag): ?>
<?php $attributes = $attributes->except(\App\View\Components\AppLayout::ignoredParameterNames()); ?>
<?php endif; ?>
<?php $component->withAttributes([]); ?>
     <?php $__env->slot('header', null, []); ?> 
        <h2 class="font-semibold text-xl text-slate-900 leading-tight">
            <?php echo e(__('New Trip')); ?>

        </h2>
     <?php $__env->endSlot(); ?>

    <div class="py-6" x-data="tripForm()">
        <div class="max-w-3xl mx-auto px-4 sm:px-6 lg:px-8">
            <div class="bg-white border border-slate-200 rounded-xl shadow-sm">
                <div class="p-6 text-slate-900">
                    
                    <?php if($errors->any()): ?>
                        <div class="bg-red-100  text-red-700 p-4 mb-4">
                            <ul>
                                <?php $__currentLoopData = $errors->all(); $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $error): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                    <li><?php echo e($error); ?></li>
                                <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                            </ul>
                        </div>
                    <?php endif; ?>

                    <form action="<?php echo e(route('trips.store')); ?>" method="POST">
                        <?php echo csrf_field(); ?>
                        
                        <div class="space-y-4">
                            <div>
                                <label class="block text-sm font-bold text-slate-700">Trip Type</label>
                                <select name="trip_type" x-model="trip_type" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-500 focus:ring-1 focus:ring-offset-0">
                                    <option value="rent">Rent</option>
                                    <option value="own">Own</option>
                                    <option value="empty">Empty</option>
                                </select>
                            </div>

                            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">From Date</label>
                                    <input type="date" name="date" value="<?php echo e(old('date', date('Y-m-d'))); ?>" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-500 focus:ring-1 focus:ring-offset-0">
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">To Date</label>
                                    <input type="date" name="to_date" value="<?php echo e(old('to_date')); ?>" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-500 focus:ring-1 focus:ring-offset-0">
                                </div>
                            </div>

                             <div>
                                 <label class="block text-sm font-bold text-slate-700">Location</label>
                                 <input type="text" name="location" value="<?php echo e(old('location')); ?>" placeholder="e.g. Chennai to Madurai" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-500 focus:ring-1 focus:ring-offset-0">
                             </div>

                             <div>
                                 <label class="block text-sm font-bold text-slate-700">User Name</label>
                                 <div class="mt-1 block w-full rounded-md border border-slate-200 text-sm bg-slate-50/50 px-3 py-2 text-slate-700 font-semibold italic">
                                     <?php echo e(auth()->user()->name); ?>

                                 </div>
                                 <input type="hidden" name="user_name" value="<?php echo e(auth()->user()->name); ?>">
                             </div>

                            <div>
                                <label class="block text-sm font-bold text-slate-700">Vehicle</label>
                                <?php if(isset($linkedVehicle) && $linkedVehicle): ?>
                                    <input type="hidden" name="vehicle_id" value="<?php echo e($linkedVehicle->id); ?>" x-ref="vehicle_id">
                                    <div class="mt-1 block w-full rounded-md border border-slate-200 text-sm bg-slate-50/50 px-3 py-2 text-slate-700 font-semibold">
                                        <?php echo e($linkedVehicle->vehicle_number); ?>

                                    </div>
                                <?php else: ?>
                                    <select name="vehicle_id" @change="fetchLastTrip($event.target.value)" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-500 focus:ring-1 focus:ring-offset-0">
                                        <option value="">Select Vehicle</option>
                                        <?php $__currentLoopData = $vehicles; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $v): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                            <option value="<?php echo e($v->id); ?>"><?php echo e($v->vehicle_number); ?></option>
                                        <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                    </select>
                                <?php endif; ?>
                            </div>

                            <div>
                                <label class="block text-sm font-bold text-slate-700">Driver</label>
                                <select name="driver_id" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-500 focus:ring-1 focus:ring-offset-0">
                                    <option value="">Select Driver</option>
                                    <?php $__currentLoopData = $drivers; $__env->addLoop($__currentLoopData); foreach($__currentLoopData as $d): $__env->incrementLoopIndices(); $loop = $__env->getLastLoop(); ?>
                                        <option value="<?php echo e($d->id); ?>"><?php echo e($d->driver_name); ?></option>
                                    <?php endforeach; $__env->popLoop(); $loop = $__env->getLastLoop(); ?>
                                </select>
                            </div>



                            <!-- KM Tracking -->
                            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">Open KM</label>
                                    <input type="number" min="0" name="open_km" x-model="open_km" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm">
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">Close KM</label>
                                    <input type="number" min="0" name="close_km" x-model="close_km" value="<?php echo e(old('close_km')); ?>" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm">
                                </div>
                            </div>

                            <div x-show="open_km || close_km" class="bg-blue-50 p-3 rounded-md border border-blue-100 mt-2">
                                <p class="text-sm font-bold text-blue-800">Total KM: <span x-text="calculateTotalKM()"></span> KM</p>
                            </div>

                            <!-- Hour Tracking -->
                            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">Open Hour</label>
                                    <input type="number" min="0" step="0.01" name="open_hour" x-model="open_hour" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-500 focus:ring-1 focus:ring-offset-0">
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">Close Hour</label>
                                    <input type="number" min="0" step="0.01" name="close_hour" x-model="close_hour" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm focus:border-indigo-500 focus:ring focus:ring-indigo-500 focus:ring-1 focus:ring-offset-0">
                                </div>
                            </div>

                            <div x-show="open_hour || close_hour" class="bg-blue-50 p-3 rounded-md border border-blue-100 mt-2">
                                <p class="text-sm font-bold text-blue-800">Running Time: <span x-text="calculateTotalHours()"></span> Hours</p>
                            </div>

                            <!-- Financials -->
                            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">Rent Amount (₹)</label>
                                    <input type="number" min="0" step="0.01" name="rent_amount" x-model="rent_amount" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm font-bold text-green-600">
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">Padi Kaasu (படி காசு)</label>
                                    <input type="number" min="0" step="0.01" name="padi_kaasu" x-model="padi_kaasu" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm font-bold text-indigo-600">
                                </div>
                            </div>
                            
                            <div x-show="rent_amount || padi_kaasu" class="bg-green-50 p-3 rounded-md border border-green-100 mt-2">
                                <p class="text-sm font-bold text-green-800">Total Amount: ₹<span x-text="calculateTotalAmount()"></span></p>
                            </div>

                            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 mt-4">
                                <div class="col-span-1 sm:col-span-2">
                                    <label class="block text-sm font-bold text-slate-700">Work</label>
                                    <input type="text" name="work" value="<?php echo e(old('work')); ?>" placeholder="Describe work" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm">
                                </div>
                            </div>

                            <div class="grid grid-cols-1 sm:grid-cols-2 gap-4 mt-4">
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">Fuel (Litres)</label>
                                    <input type="number" min="0" step="0.01" name="fuel_litre" value="<?php echo e(old('fuel_litre')); ?>" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm">
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-slate-700">Diesel Price</label>
                                    <input type="number" min="0" step="0.01" name="diesel_price" value="<?php echo e(old('diesel_price')); ?>" class="mt-1 block w-full rounded-md border-slate-200 text-sm shadow-sm">
                                </div>
                            </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-indigo-600 text-white font-bold text-lg py-4 rounded-lg shadow-lg hover:bg-indigo-700 active:bg-indigo-800 transition">
                                Save Trip
                            </button>
                        </div>
                        <div class="h-20 sm:hidden"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        function tripForm() {
            const lastTripData = <?php echo json_encode($lastTripData, 15, 512) ?>;
            const initialVehicle = '<?php echo e($linkedVehicle ? $linkedVehicle->id : old('vehicle_id')); ?>';
            const initOpenKm = initialVehicle && lastTripData[initialVehicle] ? lastTripData[initialVehicle].close_km : '';
            const initOpenHr = initialVehicle && lastTripData[initialVehicle] ? lastTripData[initialVehicle].close_hour : 0;

            return {
                vehicle_id: initialVehicle,
                trip_type: '<?php echo e(old('trip_type', 'rent')); ?>',
                open_km: '<?php echo e(old('open_km')); ?>' || initOpenKm,
                close_km: '<?php echo e(old('close_km')); ?>',
                open_hour: '<?php echo e(old('open_hour')); ?>' || initOpenHr,
                close_hour: '<?php echo e(old('close_hour', 0)); ?>',
                rent_amount: '<?php echo e(old('rent_amount')); ?>',
                padi_kaasu: '<?php echo e(old('padi_kaasu')); ?>',
                diesel_price: '<?php echo e(old('diesel_price')); ?>',
                fuel_litre: '<?php echo e(old('fuel_litre')); ?>',
                work: '<?php echo e(old('work')); ?>',
                
                init() {
                    // Auto-fetch if vehicle is already linked (Vehicle Login)
                    if (document.querySelector('input[name="vehicle_id"]')) {
                        this.fetchLastTrip(document.querySelector('input[name="vehicle_id"]').value);
                    } else {
                        // For Admin/Staff, check if a vehicle is already selected
                        let select = document.querySelector('select[name="vehicle_id"]');
                        if (select && select.value && !this.open_km && !this.open_hour) {
                            this.fetchLastTrip(select.value);
                        }
                        // Watch for vehicle selection changes
                        this.$watch('vehicle_id', value => {
                            if (value) this.fetchLastTrip(value);
                        });
                    }
                },


                calculateTotalHours() {
                    let total = (parseFloat(this.close_hour) || 0) - (parseFloat(this.open_hour) || 0);
                    return total > 0 ? total.toFixed(2) : 0;
                },

                calculateTotalKM() {
                    let total = (parseFloat(this.close_km) || 0) - (parseFloat(this.open_km) || 0);
                    return total > 0 ? total.toFixed(2) : 0;
                },

                calculateTotalAmount() {
                    let total = (parseFloat(this.rent_amount) || 0) + (parseFloat(this.padi_kaasu) || 0);
                    return total > 0 ? total.toFixed(2) : 0;
                },

                fetchLastTrip(vehicleId) {
                    if (!vehicleId) return;
                    
                    fetch(`/api/vehicles/${vehicleId}/last-trip`)
                        .then(res => res.json())
                        .then(data => {
                            this.open_km = data.close_km;
                            this.open_hour = data.close_hour;
                        })
                        .catch(err => console.error(err));
                }
            }
        }
    </script>
 <?php echo $__env->renderComponent(); ?>
<?php endif; ?>
<?php if (isset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $attributes = $__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__attributesOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php if (isset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54)): ?>
<?php $component = $__componentOriginal9ac128a9029c0e4701924bd2d73d7f54; ?>
<?php unset($__componentOriginal9ac128a9029c0e4701924bd2d73d7f54); ?>
<?php endif; ?>
<?php /**PATH /Users/roni/Documents/Vicky/vms/resources/views/trips/create.blade.php ENDPATH**/ ?>