<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-xl text-gray-800 leading-tight">
            {{ __('Edit Payment') }}
        </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" x-data="editPaymentForm()">
                    
                    @if($errors->any())
                        <div class="bg-red-100 border-l-4 border-red-500 text-red-700 p-4 mb-4">
                            <ul class="list-disc list-inside">
                                @foreach($errors->all() as $error)
                                    <li>{{ $error }}</li>
                                @endforeach
                            </ul>
                        </div>
                    @endif

                    {{-- Trip Info (Read Only) --}}
                    <div class="bg-blue-50 border border-blue-200 rounded-lg p-4 mb-6">
                        <div class="flex justify-between items-center">
                            <div>
                                <p class="text-xs text-blue-500 font-medium">Trip Details</p>
                                <p class="font-bold text-blue-800">{{ $payment->trip->location }}</p>
                                <p class="text-sm text-blue-600">
                                    {{ $payment->trip->date->format('d M Y') }} • 
                                    {{ $payment->trip->vehicle->vehicle_number ?? 'N/A' }} • 
                                    {{ $payment->trip->driver->driver_name ?? 'N/A' }}
                                </p>
                            </div>
                            <div class="text-right">
                                <p class="text-xs text-blue-500">Rent Amount</p>
                                <p class="text-xl font-black text-blue-700">₹{{ number_format($payment->trip->rent_amount, 2) }}</p>
                            </div>
                        </div>
                    </div>

                    {{-- Current Status Badge --}}
                    <div class="mb-4 text-center">
                        <span class="px-4 py-1.5 text-sm font-bold rounded-full uppercase
                            {{ $payment->status == 'completed' ? 'bg-green-100 text-green-700' : ($payment->status == 'partial' ? 'bg-yellow-100 text-yellow-700' : 'bg-red-100 text-red-700') }}">
                            Current: {{ $payment->status }}
                        </span>
                    </div>

                    <form action="{{ route('payments.update', $payment) }}" method="POST">
                        @csrf
                        @method('PUT')
                        
                        <div class="space-y-4">
                            {{-- Advance & Paid --}}
                            <div class="grid grid-cols-2 gap-4">
                                <div>
                                    <label class="block text-sm font-bold text-gray-700">Advance Received (₹)</label>
                                    <input type="number" step="0.01" name="advance" x-model.number="advance" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 font-semibold" min="0">
                                </div>
                                <div>
                                    <label class="block text-sm font-bold text-gray-700">Amount Paid (₹)</label>
                                    <input type="number" step="0.01" name="paid" x-model.number="paid" class="mt-1 block w-full rounded-md border-gray-300 shadow-sm focus:border-blue-300 focus:ring focus:ring-blue-200 font-bold text-green-600" min="0" required>
                                </div>
                            </div>

                            {{-- Live Balance Calculation --}}
                            <div class="rounded-lg p-4" :class="balance > 0 ? 'bg-red-50 border border-red-200' : 'bg-green-50 border border-green-200'">
                                <div class="grid grid-cols-3 gap-4 text-center">
                                    <div>
                                        <p class="text-xs text-gray-500">Rent</p>
                                        <p class="font-bold text-gray-800">₹{{ number_format($payment->trip->rent_amount, 2) }}</p>
                                    </div>
                                    <div>
                                        <p class="text-xs text-gray-500">Total Paid</p>
                                        <p class="font-bold text-green-600">₹<span x-text="totalPaid.toLocaleString('en-IN')"></span></p>
                                    </div>
                                    <div>
                                        <p class="text-xs font-semibold" :class="balance > 0 ? 'text-red-600' : 'text-green-600'">Balance</p>
                                        <p class="text-xl font-black" :class="balance > 0 ? 'text-red-600' : 'text-green-600'">₹<span x-text="Math.abs(balance).toLocaleString('en-IN')"></span></p>
                                    </div>
                                </div>
                                <p class="text-xs text-center mt-2" :class="balance > 0 ? 'text-red-500' : 'text-green-500'">
                                    <span x-show="balance > 0">⏳ Status will be set to <strong>Partial</strong></span>
                                    <span x-show="balance <= 0">✅ Status will be set to <strong>Completed</strong></span>
                                </p>
                            </div>

                            {{-- Payment Method --}}
                            <div>
                                <label class="block text-sm font-bold text-gray-700">Payment Method</label>
                                <select name="method" 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="Cash" {{ $payment->method == 'Cash' ? 'selected' : '' }}>Cash</option>
                                    <option value="UPI" {{ $payment->method == 'UPI' ? 'selected' : '' }}>UPI</option>
                                    <option value="Bank Transfer" {{ $payment->method == 'Bank Transfer' ? 'selected' : '' }}>Bank Transfer</option>
                                </select>
                            </div>
                        </div>

                        {{-- Save --}}
                        <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 Payment
                            </button>
                        </div>
                        <div class="h-20 sm:hidden"></div>
                    </form>
                </div>
            </div>
        </div>
    </div>

    <script>
        function editPaymentForm() {
            return {
                rentAmount: {{ $payment->trip->rent_amount }},
                advance: {{ old('advance', $payment->advance) }},
                paid: {{ old('paid', $payment->paid) }},
                get totalPaid() { return (this.advance || 0) + (this.paid || 0); },
                get balance() { return this.rentAmount - this.totalPaid; },
            }
        }
    </script>
</x-app-layout>
