@extends('layouts.app')

@section('content')
<div class="container mx-auto px-4 py-8">
    <div class="max-w-4xl mx-auto">
        <h1 class="text-3xl font-bold mb-8">Pending Payments</h1>

        @if($orders->isEmpty())
            <div class="bg-blue-50 border border-blue-200 rounded-lg p-6">
                <p class="text-blue-800">You have no pending payments. All your orders are either paid or cancelled.</p>
                <a href="{{ route('order-history') }}" class="mt-4 inline-block text-blue-600 hover:text-blue-800 font-semibold">
                    ← Back to Order History
                </a>
            </div>
        @else
            <div class="space-y-4">
                @foreach($orders as $order)
                    <div class="border border-gray-200 rounded-lg p-6 hover:shadow-lg transition-shadow">
                        <div class="flex justify-between items-start mb-4">
                            <div>
                                <h2 class="text-xl font-semibold text-gray-900">{{ $order->order_number }}</h2>
                                <p class="text-sm text-gray-600">
                                    Ordered: {{ $order->created_at->format('M d, Y @ H:i') }}
                                </p>
                            </div>
                            <div class="text-right">
                                <p class="text-2xl font-bold text-gray-900">R {{ number_format($order->total, 2) }}</p>
                                <span class="inline-block mt-2 px-3 py-1 bg-yellow-100 text-yellow-800 text-xs font-semibold rounded-full">
                                    @if($order->payment_status === 'pending')
                                        Payment Pending
                                    @elseif($order->payment_status === 'failed')
                                        Payment Failed
                                    @else
                                        {{ ucfirst($order->payment_status) }}
                                    @endif
                                </span>
                            </div>
                        </div>

                        <div class="bg-gray-50 rounded p-4 mb-4">
                            <h3 class="font-semibold text-gray-900 mb-3">Order Items</h3>
                            <div class="space-y-2">
                                @foreach($order->items as $item)
                                    <div class="flex justify-between text-sm text-gray-700">
                                        <span>{{ $item->product->name }}</span>
                                        <span>{{ $item->quantity }} x R {{ number_format($item->price, 2) }}</span>
                                    </div>
                                @endforeach
                            </div>
                        </div>

                        <div class="grid grid-cols-2 gap-4 text-sm mb-4 pb-4 border-b border-gray-200">
                            <div>
                                <p class="text-gray-600">Customer Name</p>
                                <p class="font-semibold text-gray-900">{{ $order->customer_name }}</p>
                            </div>
                            <div>
                                <p class="text-gray-600">Shipping Address</p>
                                <p class="font-semibold text-gray-900">{{ $order->shipping_address }}</p>
                            </div>
                        </div>

                        @if($order->yoco_redirect_url)
                            <div class="flex gap-3">
                                <a href="{{ $order->yoco_redirect_url }}" 
                                   target="_blank"
                                   class="flex-1 bg-blue-600 hover:bg-blue-700 text-white font-semibold py-3 px-4 rounded-lg text-center transition-colors">
                                    Complete Payment with Yoco
                                </a>
                                <a href="{{ route('order-history') }}" 
                                   class="flex-1 bg-gray-200 hover:bg-gray-300 text-gray-800 font-semibold py-3 px-4 rounded-lg text-center transition-colors">
                                    Back
                                </a>
                            </div>
                        @else
                            <div class="bg-red-50 border border-red-200 rounded p-4">
                                <p class="text-red-800 text-sm">
                                    <strong>Note:</strong> Payment link is not available for this order. Please contact support.
                                </p>
                            </div>
                        @endif
                    </div>
                @endforeach
            </div>

            <div class="mt-8">
                <a href="{{ route('order-history') }}" class="text-blue-600 hover:text-blue-800 font-semibold">
                    ← Back to All Orders
                </a>
            </div>
        @endif
    </div>
</div>
@endsection
