<x-app-layout>
<x-slot name="title">My Orders</x-slot>
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<div class="grid grid-cols-1 md:grid-cols-4 gap-6">
@include('account.partials.sidebar', ['active' => 'orders'])
<div class="md:col-span-3">
<h1 class="section-title mb-6">My Orders</h1>
@if($orders->count())
<div class="space-y-4">
@foreach($orders as $order)
<div class="card card-body">
<div class="flex flex-col sm:flex-row sm:items-center justify-between gap-3">
<div>
<p class="font-medium text-gray-900">Order #{{ $order->order_number }}</p>
<p class="text-sm text-gray-500">{{ $order->created_at->format('d M Y') }} ยท {{ $order->items->count() }} item(s)</p>
</div>
<div class="flex items-center gap-3">
<span class="badge {{ match($order->status) {
'pending' => 'badge-yellow',
'confirmed','processing' => 'badge-blue',
'shipped','out_for_delivery' => 'badge-blue',
'delivered' => 'badge-green',
'cancelled','refunded','returned' => 'badge-red',
'return_requested' => 'badge-yellow',
default => 'badge-gray'
} }}">{{ ucfirst(str_replace('_', ' ', $order->status)) }}</span>
<span class="font-bold text-primary-600">{{ format_price($order->total_amount) }}</span>
</div>
</div>
<div class="flex gap-3 mt-4 pt-3 border-t border-gray-50">
<a href="{{ route('orders.track', $order->uuid) }}" class="btn-secondary btn-sm">Track Order</a>
@if(in_array($order->status, ['pending','confirmed']))
<form method="POST" action="{{ route('orders.cancel', $order->order_number) }}">
@csrf
<button class="btn-danger btn-sm" onclick="return confirm('Cancel this order?')">Cancel</button>
</form>
@endif
@if($order->isDelivered())
@php
// Non-returnable enforcement: check if ALL items are non-returnable
$allNonReturnable = $order->items->every(
fn($item) => isset($item->product) && $item->product->is_returnable === false
);
@endphp
@if($allNonReturnable)
<span class="text-xs text-orange-600 bg-orange-50 border border-orange-200 rounded px-2 py-1">
Non-returnable โ medicines cannot be returned per drug regulations
</span>
@else
<a href="{{ route('orders.return.create', $order->uuid) }}" class="btn-secondary btn-sm">Return / Refund</a>
@endif
@endif
</div>
</div>
@endforeach
</div>
<div class="mt-6">{{ $orders->links() }}</div>
@else
<div class="card card-body text-center py-16 text-gray-400">
<p class="mb-4">You haven't placed any orders yet.</p>
<a href="{{ route('products.index') }}" class="btn-primary">Start Shopping</a>
</div>
@endif
</div>
</div>
</div>
</x-app-layout>