@extends('layouts.admin', ['title' => 'طلب '.$order->order_number])

@section('content')
    <section class="lc-page-shell">
        <div class="surface p-6">
            @if(session('status'))
                <div class="mb-4 rounded border border-green-200 bg-green-50 p-3 text-sm text-green-700">{{ session('status') }}</div>
            @endif
            @if($errors->any())
                <div class="mb-4 rounded border border-red-200 bg-red-50 p-3 text-sm text-red-700">{{ $errors->first() }}</div>
            @endif
            <div class="section-heading">
                <div>
                    <span>تشغيل الطلب</span>
                    <h1>{{ $order->order_number }}</h1>
                </div>
                <a class="secondary-button" href="{{ route('admin.orders.index') }}">كل الطلبات</a>
            </div>
            <div class="mt-6 grid gap-4 md:grid-cols-4">
                <div class="metric-card"><span>العميل</span><strong>{{ $order->customer_name }}</strong></div>
                <div class="metric-card"><span>الدفع</span><strong>{{ $order->payment_status }}</strong></div>
                <div class="metric-card"><span>التنفيذ</span><strong>{{ $order->fulfillment_status }}</strong></div>
                <div class="metric-card"><span>الإجمالي</span><strong>{{ number_format($order->total_minor / 100, 2) }}</strong></div>
            </div>
            <div class="mt-4 grid gap-4 md:grid-cols-4">
                <div class="metric-card"><span>Subtotal</span><strong>{{ number_format($order->subtotal_minor / 100, 2) }}</strong></div>
                <div class="metric-card"><span>Discount</span><strong>{{ number_format($order->discount_minor / 100, 2) }}</strong></div>
                <div class="metric-card"><span>Cashback</span><strong>{{ number_format($order->cashback_minor / 100, 2) }}</strong></div>
                <div class="metric-card"><span>Coupon</span><strong>{{ $order->coupon_code ?? '-' }}</strong></div>
            </div>
            <div class="mt-6 flex flex-wrap gap-3">
                <form method="POST" action="{{ route('admin.orders.paid', $order) }}">
                    @csrf
                    <button class="secondary-button" type="submit">تعليم كمدفوع</button>
                </form>
                <form method="POST" action="{{ route('admin.orders.complete', $order) }}">
                    @csrf
                    <button class="primary-button" type="submit">تم التنفيذ</button>
                </form>
                <form method="POST" action="{{ route('admin.fulfillment.provider-mock', $order) }}">
                    @csrf
                    <button class="primary-button" type="submit">تنفيذ API تجريبي</button>
                </form>
            </div>
            @if($order->latestPaymentAttempt)
                <div class="mt-6 rounded-2xl border border-[#e0e8e3] bg-[#f7f8fb] p-4 text-sm text-[#596174]">
                    آخر محاولة دفع: <strong>{{ $order->latestPaymentAttempt->reference }}</strong> / {{ $order->latestPaymentAttempt->status }}
                </div>
            @endif
            <div class="mt-6 overflow-hidden rounded border border-[#e7e9ef]">
                @foreach($order->items as $item)
                    <div class="admin-row">
                        <span>
                            {{ $item->name_snapshot }} - {{ $item->sku_snapshot }}
                            @if($item->latestProviderAttempt)
                                <small class="block text-xs text-[#6e7488]">
                                    Provider: {{ $item->latestProviderAttempt->reference }} / {{ $item->latestProviderAttempt->status }}
                                </small>
                            @endif
                        </span>
                        <strong>{{ $item->delivery_method_snapshot }}</strong>
                        <em>{{ $item->fulfillment_status }}</em>
                    </div>
                @endforeach
            </div>
            <pre class="mt-6 overflow-auto rounded bg-[#17264d] p-4 text-xs text-white">{{ json_encode($order->customer_fields, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE) }}</pre>
            <section class="mt-6">
                <h2 class="text-xl font-black text-[#17264d]">سجل الإشعارات</h2>
                <div class="mt-3 overflow-hidden rounded border border-[#e7e9ef]">
                    @forelse($order->notificationDeliveries->sortByDesc('id') as $delivery)
                        <div class="admin-row">
                            <span>{{ $delivery->subject ?? $delivery->body }}<small class="block text-xs text-[#6e7488]">{{ $delivery->recipient }}</small></span>
                            <strong>{{ $delivery->channel }} / {{ $delivery->provider }}</strong>
                            <em>{{ $delivery->status }}</em>
                        </div>
                    @empty
                        <div class="p-4 text-sm text-[#6e7488]">لا توجد إشعارات مرسلة لهذا الطلب بعد.</div>
                    @endforelse
                </div>
            </section>
        </div>
    </section>
@endsection
