<div>
    <div class="flex items-center gap-3 mb-8">
        <a href="{{ route('merchant.orders') }}" class="text-gray-400 hover:text-gray-600 transition">
            <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M9 5l7 7-7 7"/></svg>
        </a>
        <div>
            <h1 class="text-2xl font-bold text-gray-800">طلب #{{ $order->id }}</h1>
            <p class="text-sm text-gray-500">{{ $order->created_at->format('Y/m/d - h:i A') }}</p>
        </div>
    </div>

    <div class="grid lg:grid-cols-3 gap-6">
        <div class="lg:col-span-2 space-y-6">
            {{-- Status Timeline --}}
            <div class="bg-white rounded-2xl border border-gray-200 p-6">
                <h3 class="font-bold text-gray-800 mb-4">حالة الطلب</h3>
                @php
                    $allStatuses = ['pending', 'processing', 'shipping', 'delivered'];
                    if(in_array($order->status, ['pending_supplier', 'ordered_from_supplier', 'received_from_supplier'])) {
                        $allStatuses = ['pending', 'pending_supplier', 'ordered_from_supplier', 'received_from_supplier', 'processing', 'shipping', 'delivered'];
                    }
                    $currentIndex = array_search($order->status, $allStatuses);
                    if($order->status === 'returned') $currentIndex = -1;
                @endphp
                <div class="flex items-center gap-2 overflow-x-auto pb-2">
                    @foreach($allStatuses as $index => $status)
                        @php
                            $isActive = $index <= $currentIndex;
                            $isCurrent = $status === $order->status;
                        @endphp
                        <div class="flex items-center gap-2 flex-shrink-0">
                            <div class="flex flex-col items-center">
                                <div class="w-8 h-8 rounded-full flex items-center justify-center text-xs font-bold
                                    {{ $isActive ? 'bg-emerald-500 text-white' : 'bg-gray-100 text-gray-400' }}
                                    {{ $isCurrent ? 'ring-4 ring-emerald-100' : '' }}">
                                    @if($isActive && !$isCurrent)
                                        <svg class="w-4 h-4" fill="none" stroke="currentColor" viewBox="0 0 24 24"><path stroke-linecap="round" stroke-linejoin="round" stroke-width="3" d="M5 13l4 4L19 7"/></svg>
                                    @else
                                        {{ $index + 1 }}
                                    @endif
                                </div>
                                <span class="text-xs mt-1 {{ $isCurrent ? 'text-emerald-600 font-bold' : 'text-gray-500' }} whitespace-nowrap">
                                    {{ $statusLabels[$status] ?? $status }}
                                </span>
                            </div>
                            @if(!$loop->last)
                                <div class="w-8 h-0.5 {{ $isActive && $index < $currentIndex ? 'bg-emerald-500' : 'bg-gray-200' }} mt-[-1.25rem]"></div>
                            @endif
                        </div>
                    @endforeach
                </div>
                @if($order->status === 'returned')
                    <div class="mt-4 bg-red-50 text-red-700 rounded-xl p-3 text-sm text-center font-medium">
                        تم إرجاع هذا الطلب
                    </div>
                @endif
            </div>

            {{-- Order Items --}}
            <div class="bg-white rounded-2xl border border-gray-200 overflow-hidden">
                <div class="px-6 py-4 border-b border-gray-100">
                    <h3 class="font-bold text-gray-800">عناصر الطلب</h3>
                </div>
                <div class="divide-y divide-gray-50">
                    @foreach($order->items as $item)
                        <div class="px-6 py-4 flex items-center justify-between">
                            <div>
                                <p class="text-sm font-medium text-gray-800">
                                    {{ $item->variant?->product?->name ?? 'منتج محذوف' }} - {{ $item->variant?->pattern_name ?? '-' }}
                                </p>
                                <div class="flex items-center gap-2 text-xs text-gray-500 mt-1">
                                    <span>الكمية: {{ $item->quantity }}</span>
                                    <span>×</span>
                                    <span>{{ number_format($item->unit_price, 2) }} ج.م</span>
                                    <span class="px-2 py-0.5 rounded-full
                                        {{ $item->fulfillment_status === 'pending' ? 'bg-amber-100 text-amber-700' : '' }}
                                        {{ $item->fulfillment_status === 'awaiting_supplier' ? 'bg-orange-100 text-orange-700' : '' }}
                                        {{ $item->fulfillment_status === 'fulfilled' ? 'bg-emerald-100 text-emerald-700' : '' }}">
                                        @switch($item->fulfillment_status)
                                            @case('pending') قيد التجهيز @break
                                            @case('awaiting_supplier') بانتظار المورد @break
                                            @case('fulfilled') تم التوفير @break
                                            @default {{ $item->fulfillment_status }}
                                        @endswitch
                                    </span>
                                </div>
                            </div>
                            <p class="text-sm font-bold text-gray-800">{{ number_format($item->quantity * $item->unit_price, 2) }} ج.م</p>
                        </div>
                    @endforeach
                </div>
            </div>

            {{-- Loyalty Transactions --}}
            @if($order->loyaltyTransactions->isNotEmpty())
                <div class="bg-white rounded-2xl border border-gray-200 overflow-hidden">
                    <div class="px-6 py-4 border-b border-gray-100">
                        <h3 class="font-bold text-gray-800">حركات النقاط</h3>
                    </div>
                    <div class="divide-y divide-gray-50">
                        @foreach($order->loyaltyTransactions as $tx)
                            <div class="px-6 py-3 flex items-center justify-between">
                                <div>
                                    <p class="text-sm text-gray-700">{{ $tx->description }}</p>
                                    <p class="text-xs text-gray-400">{{ $tx->created_at->format('Y/m/d h:i A') }}</p>
                                </div>
                                <span class="text-sm font-bold {{ $tx->type === 'earned' ? 'text-emerald-600' : 'text-amber-600' }}">
                                    {{ $tx->type === 'earned' ? '+' : '-' }}{{ $tx->points }} نقطة
                                </span>
                            </div>
                        @endforeach
                    </div>
                </div>
            @endif
        </div>

        {{-- Order Summary Sidebar --}}
        <div>
            <div class="bg-white rounded-2xl border border-gray-200 p-6 sticky top-24">
                <h3 class="font-bold text-gray-800 mb-4">ملخص الطلب</h3>
                <div class="space-y-3 text-sm">
                    <div class="flex justify-between">
                        <span class="text-gray-500">الإجمالي الفرعي</span>
                        <span class="font-semibold">{{ number_format($order->subtotal, 2) }} ج.م</span>
                    </div>
                    @if($order->discount_from_points > 0)
                        <div class="flex justify-between text-amber-600">
                            <span>خصم النقاط ({{ $order->points_redeemed }})</span>
                            <span class="font-semibold">-{{ number_format($order->discount_from_points, 2) }} ج.م</span>
                        </div>
                    @endif
                    <div class="flex justify-between {{ $order->shipping_cost > 0 ? 'text-gray-600' : 'text-emerald-600' }}">
                        <span>تكلفة الشحن</span>
                        <span class="font-semibold">
                            @if($order->shipping_cost > 0)
                                {{ number_format($order->shipping_cost, 2) }} ج.م
                            @else
                                مجاني
                            @endif
                        </span>
                    </div>
                    @if($order->payment_fee > 0)
                        <div class="flex justify-between text-gray-600">
                            <span>رسوم الدفع</span>
                            <span class="font-semibold">{{ number_format($order->payment_fee, 2) }} ج.م</span>
                        </div>
                    @endif
                    @if($order->extra_charges > 0)
                        <div class="flex justify-between text-gray-600">
                            <span>مصروفات إضافية</span>
                            <span class="font-semibold">{{ number_format($order->extra_charges, 2) }} ج.م</span>
                        </div>
                    @endif
                    <hr class="border-gray-100">
                    <div class="flex justify-between text-lg">
                        <span class="font-bold text-gray-800">الإجمالي</span>
                        <span class="font-bold text-emerald-600">{{ number_format($order->total, 2) }} ج.م</span>
                    </div>
                </div>

                <div class="mt-6 pt-4 border-t border-gray-100 space-y-2 text-sm">
                    <div class="flex justify-between">
                        <span class="text-gray-500">طريقة الدفع</span>
                        <span>{{ $order->paymentMethod?->name ?? ($order->payment_method === 'cash' ? 'كاش عند الاستلام' : ($order->payment_method === 'transfer' ? 'تحويل بنكي' : ($order->payment_method ?? '—'))) }}</span>
                    </div>
                    @if($order->points_earned > 0)
                        <div class="flex justify-between text-emerald-600">
                            <span>نقاط مكتسبة</span>
                            <span class="font-semibold">+{{ $order->points_earned }}</span>
                        </div>
                    @endif
                    @if($order->shipping_address_snapshot)
                        <div class="mt-3 pt-3 border-t border-gray-100">
                            <p class="text-xs text-gray-400 mb-1">عنوان الشحن</p>
                            <p class="text-sm text-gray-600">{{ $order->shipping_address_snapshot }}</p>
                        </div>
                    @endif
                    @if($order->notes)
                        <div class="mt-3 pt-3 border-t border-gray-100">
                            <p class="text-xs text-gray-400 mb-1">ملاحظات</p>
                            <p class="text-sm text-gray-600">{{ $order->notes }}</p>
                        </div>
                    @endif
                </div>
            </div>
        </div>
    </div>
</div>
