@extends('layouts.app')

@section('title', 'تفاصيل أمر البيع')

@section('content')
<div class="min-h-screen bg-gradient-to-br from-slate-50 to-slate-100 py-8 px-4 md:px-8">
    <div class="max-w-5xl mx-auto">
        <div class="mb-8">
            <div class="flex items-center justify-between mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">تفاصيل أمر البيع</h1>
                    <p class="text-gray-600">{{ $salesOrder->so_number }}</p>
                </div>
                <a href="{{ route('sales-orders.index') }}" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white font-semibold rounded-lg hover:bg-gray-700 transition-all duration-200">
                    <i class="fas fa-arrow-right ml-2"></i>
                    <span>العودة</span>
                </a>
            </div>

            @if(session('success'))
            <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-check-circle text-green-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-green-700 font-medium">{{ session('success') }}</p>
                    </div>
                </div>
            </div>
            @endif
        </div>

        <!-- Basic Information -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-6">
            <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                <h2 class="text-xl font-semibold">معلومات الأمر</h2>
            </div>
            <div class="p-6">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">رقم الأمر</label>
                        <p class="text-gray-900 font-semibold">{{ $salesOrder->so_number }}</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">الحالة</label>
                        <div>
                            @if($salesOrder->status == 'draft')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                <i class="fas fa-file-alt ml-1"></i>
                                مسودة
                            </span>
                            @elseif($salesOrder->status == 'sent')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                <i class="fas fa-paper-plane ml-1"></i>
                                مرسل للعميل
                            </span>
                            @elseif($salesOrder->status == 'accepted')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                <span class="w-2 h-2 bg-green-500 rounded-full ml-2"></span>
                                مقبول
                            </span>
                            @elseif($salesOrder->status == 'partial_delivery')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-orange-100 text-orange-700">
                                <i class="fas fa-truck ml-1"></i>
                                تسليم جزئي
                            </span>
                            @elseif($salesOrder->status == 'delivered')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                <i class="fas fa-check-double ml-1"></i>
                                تم التسليم
                            </span>
                            @elseif($salesOrder->status == 'cancelled')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                <i class="fas fa-times-circle ml-1"></i>
                                ملغي
                            </span>
                            @endif
                        </div>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">العميل</label>
                        <p class="text-gray-900">{{ $salesOrder->customer->name ?? 'N/A' }}</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">تاريخ الأمر</label>
                        <p class="text-gray-900">{{ $salesOrder->so_date->format('Y-m-d') }}</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">تاريخ التسليم المتوقع</label>
                        <p class="text-gray-900">{{ $salesOrder->delivery_date ? $salesOrder->delivery_date->format('Y-m-d') : 'N/A' }}</p>
                    </div>
                    @if($salesOrder->quotation_id)
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">عرض السعر المرتبط</label>
                        <p class="text-gray-900">
                            <a href="{{ route('sales-quotations.show', $salesOrder->quotation_id) }}" class="text-blue-600 hover:text-blue-800 underline">
                                {{ $salesOrder->quotation->quotation_number ?? 'N/A' }}
                            </a>
                        </p>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Financial Summary -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-6">
            <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                <h2 class="text-xl font-semibold">ملخص مالي</h2>
            </div>
            <div class="p-6">
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                    <div class="border-l border-gray-200 pl-6 text-right">
                        <label class="block text-sm font-medium text-gray-700 mb-2">المجموع الجزئي</label>
                        <p class="text-2xl font-bold text-gray-900">{{ number_format($salesOrder->subtotal, 2) }}</p>
                    </div>
                    <div class="border-l border-gray-200 pl-6 text-right">
                        <label class="block text-sm font-medium text-gray-700 mb-2">الضريبة</label>
                        <p class="text-2xl font-bold text-gray-900">{{ number_format($salesOrder->tax_amount, 2) }}</p>
                    </div>
                    <div class="text-right">
                        <label class="block text-sm font-medium text-gray-700 mb-2">الإجمالي</label>
                        <p class="text-2xl font-bold text-blue-600">{{ number_format($salesOrder->total_amount, 2) }}</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Items -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-6">
            <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                <h2 class="text-xl font-semibold">الأصناف ({{ $salesOrder->items->count() }})</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50 border-b border-gray-200">
                        <tr>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">#</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">اسم الصنف</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">الكمية المطلوبة</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">الكمية المسلمة</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">الوحدة</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">سعر الوحدة</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">الإجمالي</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($salesOrder->items as $item)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $item->item->name ?? 'N/A' }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">{{ $item->quantity_ordered }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $item->quantity_delivered }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ $item->unit->name ?? 'N/A' }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ number_format($item->unit_price, 2) }}</td>
                            <td class="px-6 py-4 text-gray-700 font-semibold">{{ number_format($item->line_total, 2) }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" class="px-6 py-4 text-center text-gray-500">لا توجد أصناف</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Actions -->
        <div class="flex gap-3 justify-end">
            @if($salesOrder->status == 'draft')
                @permission('sales.orders.edit')
                <form action="{{ route('sales-orders.send', $salesOrder) }}" method="POST" class="inline">
                    @csrf
                    <button type="submit" class="inline-flex items-center px-6 py-3 rounded-lg bg-blue-600 text-white hover:bg-blue-700 transition-all duration-150 font-medium">
                        <i class="fas fa-paper-plane ml-2"></i>
                        <span>إرسال للعميل</span>
                    </button>
                </form>

                <a href="{{ route('sales-orders.edit', $salesOrder) }}" class="inline-flex items-center px-6 py-3 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 font-medium">
                    <i class="fas fa-edit ml-2"></i>
                    <span>تعديل</span>
                </a>
                @endpermission
            @endif

            @if($salesOrder->status == 'sent')
                @permission('sales.orders.accept')
                <form action="{{ route('sales-orders.accept', $salesOrder) }}" method="POST" class="inline">
                    @csrf
                    <button type="submit" class="inline-flex items-center px-6 py-3 rounded-lg bg-green-600 text-white hover:bg-green-700 transition-all duration-150 font-medium">
                        <i class="fas fa-check ml-2"></i>
                        <span>قبول الأمر</span>
                    </button>
                </form>
                @endpermission
            @endif

            @if(in_array($salesOrder->status, ['accepted', 'delivered', 'partial_delivery']))
                <a href="{{ route('customer-invoices.create-from-so', $salesOrder) }}" class="inline-flex items-center px-6 py-3 rounded-lg bg-indigo-600 text-white hover:bg-indigo-700 transition-all duration-150 font-medium">
                    <i class="fas fa-file-invoice-dollar ml-2"></i>
                    <span>تحويل إلى فاتورة</span>
                </a>
            @endif
        </div>
    </div>
</div>
@endsection
