@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">{{ $quotationRequest->rq_number }}</p>
                </div>
                <a href="{{ route('quotation-requests.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

            @if(session('error'))
            <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded-r-lg">
                <div class="flex">
                    <div class="flex-shrink-0">
                        <i class="fas fa-exclamation-circle text-red-500 text-xl"></i>
                    </div>
                    <div class="mr-3">
                        <p class="text-red-700 font-medium">{{ session('error') }}</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">{{ $quotationRequest->rq_number }}</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">الحالة</label>
                        <div>
                            @if($quotationRequest->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($quotationRequest->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($quotationRequest->status == 'closed')
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-100 text-gray-700">
                                <i class="fas fa-times-circle ml-1"></i>
                                مغلق
                            </span>
                            @else
                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-100 text-gray-700">
                                {{ $quotationRequest->status }}
                            </span>
                            @endif
                        </div>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">الموعد النهائي</label>
                        <p class="text-gray-900">{{ $quotationRequest->deadline->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">{{ $quotationRequest->created_at->format('Y-m-d H:i') }}</p>
                    </div>
                    <div class="md:col-span-2">
                        <label class="block text-sm font-medium text-gray-700 mb-2">الوصف</label>
                        <p class="text-gray-900">{{ $quotationRequest->description }}</p>
                    </div>
                    @if($quotationRequest->notes)
                    <div class="md:col-span-2">
                        <label class="block text-sm font-medium text-gray-700 mb-2">ملاحظات</label>
                        <p class="text-gray-900">{{ $quotationRequest->notes }}</p>
                    </div>
                    @endif
                </div>
            </div>
        </div>

        <!-- Suppliers -->
        <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">الموردون ({{ $quotationRequest->suppliers->count() }})</h2>
            </div>
            <div class="p-6">
                @forelse($quotationRequest->suppliers as $supplier)
                <div class="flex items-center justify-between py-3 border-b border-gray-200 last:border-b-0">
                    <div>
                        <p class="font-semibold text-gray-900">{{ $supplier->name }}</p>
                        <p class="text-sm text-gray-600">{{ $supplier->email ?? 'بلا بريد إلكتروني' }}</p>
                    </div>
                    <p class="text-gray-700">{{ $supplier->phone ?? 'بلا هاتف' }}</p>
                </div>
                @empty
                <div class="text-center py-4">
                    <p class="text-gray-500">لا توجد موردين في هذا الطلب</p>
                </div>
                @endforelse
            </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">الأصناف المطلوبة ({{ $quotationRequest->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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($quotationRequest->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 }}</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 max-w-xs">
                                {{ $item->specifications ? Str::limit($item->specifications, 50) : '-' }}
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="5" class="px-6 py-4 text-center text-gray-500">لا توجد أصناف</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Attachment -->
        @if($quotationRequest->attachment)
        <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">
                <a href="{{ asset('storage/' . $quotationRequest->attachment) }}" target="_blank" class="inline-flex items-center px-6 py-3 bg-blue-100 text-blue-600 hover:bg-blue-200 transition-all duration-150 font-medium rounded-lg">
                    <i class="fas fa-download ml-2"></i>
                    <span>تحميل المرفق</span>
                </a>
            </div>
        </div>
        @endif

        <!-- Quotations Received -->
        @if($quotationRequest->quotations->count() > 0)
        <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">العروض المستقبلة ({{ $quotationRequest->quotations->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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($quotationRequest->quotations as $quotation)
                        <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">{{ $quotation->supplier->name ?? 'N/A' }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">{{ $quotation->created_at->format('Y-m-d') }}</td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ Str::limit($quotation->notes, 50) ?? '-' }}
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="4" class="px-6 py-4 text-center text-gray-500">لم يتم استقبال أي عروض</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        <!-- Actions -->
        <div class="flex gap-3 justify-end">
            @if($quotationRequest->status == 'draft')
                @can('purchasing.quotation-requests.edit')
                <a href="{{ route('quotation-requests.edit', $quotationRequest) }}" 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 mr-2"></i>
                    <span>تعديل</span>
                </a>
                @endcan

                @can('purchasing.quotation-requests.send')
                <form action="{{ route('quotation-requests.send', $quotationRequest) }}" method="POST" class="inline">
                    @csrf
                    <button type="submit" class="inline-flex items-center px-6 py-3 rounded-lg bg-green-100 text-green-600 hover:bg-green-200 transition-all duration-150 font-medium">
                        <i class="fas fa-paper-plane mr-2"></i>
                        <span>إرسال الطلب</span>
                    </button>
                </form>
                @endcan

                @can('purchasing.quotation-requests.delete')
                <form action="{{ route('quotation-requests.destroy', $quotationRequest) }}" method="POST" class="inline">
                    @csrf
                    @method('DELETE')
                    <button type="submit" class="inline-flex items-center px-6 py-3 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 font-medium" onclick="return confirm('هل أنت متأكد من حذف هذا الطلب؟')">
                        <i class="fas fa-trash mr-2"></i>
                        <span>حذف</span>
                    </button>
                </form>
                @endcan
            @elseif($quotationRequest->status == 'sent')
                @can('purchasing.quotation-requests.update')
                <form action="{{ route('quotation-requests.close', $quotationRequest) }}" method="POST" class="inline">
                    @csrf
                    <button type="submit" class="inline-flex items-center px-6 py-3 rounded-lg bg-gray-100 text-gray-600 hover:bg-gray-200 transition-all duration-150 font-medium">
                        <i class="fas fa-times-circle mr-2"></i>
                        <span>إغلاق الطلب</span>
                    </button>
                </form>
                @endcan
            @endif
        </div>
    </div>
</div>
@endsection
