<x-app-layout>
    <x-slot name="header">
        <div class="flex justify-between items-center">
            <h2 class="font-semibold text-xl text-gray-800 leading-tight">
                {{ __('Payments') }}
            </h2>
            <a href="{{ route('payments.create') }}" class="bg-blue-600 text-white px-4 py-2 rounded shadow hover:bg-blue-700 transition">+ Add Payment</a>
        </div>
    </x-slot>

    <div class="py-6">
        <div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
            
            @if(session('success'))
                <div class="bg-green-100 border-l-4 border-green-500 text-green-700 p-4 mb-4 rounded shadow-sm" role="alert">
                    <p>{{ session('success') }}</p>
                </div>
            @endif

            @if($payments->isEmpty())
                <div class="bg-white rounded-lg shadow-md p-8 text-center mx-4 sm:mx-0">
                    <h3 class="text-lg font-semibold text-gray-600">No payments recorded yet</h3>
                    <p class="text-gray-400 mt-1">Start by adding a payment for a trip.</p>
                    <a href="{{ route('payments.create') }}" class="mt-4 inline-block bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700 transition">Record First Payment</a>
                </div>
            @else
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-4 px-4 sm:px-0">
                    @foreach($payments as $payment)
                        <div class="bg-white rounded-lg shadow-md p-4 border-l-4 {{ $payment->status == 'completed' ? 'border-green-500' : ($payment->status == 'partial' ? 'border-yellow-500' : 'border-red-500') }}">
                            
                            {{-- Header --}}
                            <div class="flex justify-between items-start mb-3">
                                <div>
                                    <span class="text-xs text-gray-400">{{ $payment->created_at->format('d M Y') }}</span>
                                    <h3 class="font-bold text-gray-800">{{ $payment->trip->location ?? 'Trip #'.$payment->trip_id }}</h3>
                                    <p class="text-xs text-gray-500">{{ $payment->trip->vehicle->vehicle_number ?? '' }} • {{ $payment->trip->driver->driver_name ?? '' }}</p>
                                    <p class="text-xs text-gray-400">{{ $payment->trip->date?->format('d M Y') }}</p>
                                </div>
                                <span class="px-2 py-1 text-xs 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') }}">
                                    {{ $payment->status }}
                                </span>
                            </div>
                            
                            {{-- Trip Rent --}}
                            <div class="bg-gray-50 rounded-lg p-3 mb-3">
                                <div class="flex justify-between items-center">
                                    <span class="text-sm text-gray-500">Trip Rent</span>
                                    <span class="text-lg font-black text-gray-800">₹{{ number_format($payment->trip->rent_amount, 2) }}</span>
                                </div>
                            </div>

                            {{-- Payment Breakdown --}}
                            <div class="grid grid-cols-3 gap-2 text-center mb-3">
                                <div class="bg-blue-50 rounded p-2">
                                    <p class="text-xs text-blue-600 font-medium">Advance</p>
                                    <p class="font-bold text-blue-700">₹{{ number_format($payment->advance, 0) }}</p>
                                </div>
                                <div class="bg-green-50 rounded p-2">
                                    <p class="text-xs text-green-600 font-medium">Paid</p>
                                    <p class="font-bold text-green-700">₹{{ number_format($payment->paid, 0) }}</p>
                                </div>
                                <div class="bg-{{ $payment->balance > 0 ? 'red' : 'gray' }}-50 rounded p-2">
                                    <p class="text-xs text-{{ $payment->balance > 0 ? 'red' : 'gray' }}-600 font-medium">Balance</p>
                                    <p class="font-bold text-{{ $payment->balance > 0 ? 'red' : 'gray' }}-700">₹{{ number_format($payment->balance, 0) }}</p>
                                </div>
                            </div>

                            {{-- Method --}}
                            @if($payment->method)
                                <p class="text-xs text-gray-400 mb-3">Via: {{ $payment->method }}</p>
                            @endif

                            {{-- Actions --}}
                            <div class="flex gap-2 border-t pt-3">
                                <a href="{{ route('payments.edit', $payment) }}" class="flex-1 text-center text-sm font-medium px-3 py-1.5 rounded bg-blue-50 text-blue-700 hover:bg-blue-100 transition">
                                    ✏️ Edit
                                </a>
                                <form action="{{ route('payments.destroy', $payment) }}" method="POST" class="flex-1" onsubmit="return confirm('Delete this payment?')">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="w-full text-sm font-medium px-3 py-1.5 rounded bg-red-50 text-red-700 hover:bg-red-100 transition">
                                        🗑️ Delete
                                    </button>
                                </form>
                            </div>
                        </div>
                    @endforeach
                </div>
                
                <div class="mt-4 px-4 sm:px-0">
                    {{ $payments->links() }}
                </div>
            @endif

        </div>
    </div>
</x-app-layout>
