@extends('layouts.app')

@section('title', 'My Orders')

@section('content')
<div class="max-w-5xl mx-auto px-4 py-8">
    <h1 class="text-2xl font-bold text-gray-800 mb-6">My Orders</h1>

    @forelse($orders as $order)
    <div class="bg-white rounded-xl shadow-sm mb-4 overflow-hidden">
        <!-- Order Header -->
        <div class="p-4 border-b bg-gray-50 flex flex-wrap justify-between items-center gap-2">
            <div>
                <p class="text-sm text-gray-500">Order #</p>
                <p class="font-bold">{{ $order->order_number }}</p>
            </div>
            
            <div>
                <p class="text-sm text-gray-500">Date</p>
                <p>{{ $order->created_at->format('M d, Y') }}</p>
            </div>
            
            <div>
                <p class="text-sm text-gray-500">Total</p>
                <p class="font-bold text-red-600">{{ format_currency($order->total) }}</p>
            </div>
            
            <div>
                <p class="text-sm text-gray-500">Payment</p>
                <span class="px-2 py-1 rounded text-xs font-medium
                    @if($order->payment_method === 'cod')
                        @if($order->payment_status === 'paid')
                            bg-green-100 text-green-700
                        @else
                            bg-orange-100 text-orange-700
                        @endif
                    @else
                        bg-blue-100 text-blue-700
                    @endif">
                    {{ $order->payment_method === 'cod' ? 'COD' : 'Card' }}
                    @if($order->payment_method === 'cod' && $order->payment_status !== 'paid')
                        (Unpaid)
                    @endif
                </span>
            </div>
            
            <div>
                <span class="px-3 py-1 rounded-full text-sm font-medium
                    @switch($order->status)
                        @case('delivered') bg-green-100 text-green-700 @break
                        @case('shipped') bg-blue-100 text-blue-700 @break
                        @case('processing') bg-yellow-100 text-yellow-700 @break
                        @case('cancelled') bg-red-100 text-red-700 @break
                        @default bg-gray-100 text-gray-700
                    @endswitch">
                    {{ ucfirst($order->status) }}
                </span>
            </div>
        </div>

        <!-- Order Items -->
        <div class="p-4">
            @foreach($order->items as $item)
            <div class="flex gap-4 mb-4 last:mb-0">
                <img src="{{ $item->product->thumbnail_url }}" alt="" class="w-20 h-20 object-cover rounded-lg">
                
                <div class="flex-1">
                    <a href="{{ route('shop.product', $item->product->slug) }}" class="font-medium hover:text-red-600">
                        {{ $item->product_name }}
                    </a>
                    <p class="text-sm text-gray-500">Qty: {{ $item->quantity }}</p>
                    <p class="font-medium">{{ format_currency($item->price) }}</p>
                </div>
            </div>
            @endforeach
        </div>

        <!-- Order Actions -->
        <div class="p-4 border-t bg-gray-50 flex flex-wrap gap-2">
            <a href="{{ route('shop.orders.show', $order->order_number) }}" class="bg-gray-800 text-white px-4 py-2 rounded-lg text-sm hover:bg-gray-900">
                View Details
            </a>
            
            @if($order->shipmentTracking)
            <a href="{{ route('shop.orders.track', $order->order_number) }}" class="bg-blue-600 text-white px-4 py-2 rounded-lg text-sm hover:bg-blue-700">
                Track Order
            </a>
            @endif
            
            @if($order->canBeReviewed())
            <a href="{{ route('shop.orders.show', $order->order_number) }}#review" class="bg-green-600 text-white px-4 py-2 rounded-lg text-sm hover:bg-green-700">
                Write Review
            </a>
            @endif
            
            @if(in_array($order->status, ['pending', 'processing']))
            <form action="{{ route('shop.orders.cancel', $order->order_number) }}" method="POST" class="inline" data-no-loading="true"
                  onsubmit="if(confirm('Are you sure you want to cancel this order?')) { showLoading(); return true; } else { return false; }">
                @csrf
                @method('PATCH')
                <button type="submit" class="bg-red-600 text-white px-4 py-2 rounded-lg text-sm hover:bg-red-700">
                    Cancel Order
                </button>
            </form>
            @endif
        </div>
    </div>
    @empty
    <div class="text-center py-12">
        <svg class="w-16 h-16 mx-auto text-gray-300 mb-4" fill="none" stroke="currentColor" viewBox="0 0 24 24">
            <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M16 11V7a4 4 0 00-8 0v4M5 9h14l1 12H4L5 9z"/>
        </svg>
        <p class="text-xl text-gray-600">No orders yet</p>
        <a href="{{ route('shop.index') }}" class="text-red-600 hover:underline mt-2 inline-block">Start Shopping →</a>
    </div>
    @endforelse

    <div class="mt-6">{{ $orders->links() }}</div>
</div>
@endsection
