@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-7xl 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">إدارة وتتبع جميع طلبات عرض الأسعار من الموردين</p>
                </div>
                <div class="flex gap-3">
                    <form action="{{ route('quotations.compare') }}" method="GET" id="compareForm" class="hidden">
                    </form>
                    <button type="submit" form="compareForm" class="inline-flex items-center px-6 py-3 bg-indigo-600 text-white font-semibold rounded-lg hover:bg-indigo-700 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105" onclick="event.preventDefault(); submitCompare();">
                        <i class="fas fa-balance-scale ml-2"></i>
                        <span>مقارنة العروض</span>
                    </button>
                    @can('purchasing.quotation-requests.create')
                    <a href="{{ route('quotation-requests.create') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-semibold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                        <i class="fas fa-plus ml-2"></i>
                        <span>طلب عرض سعر جديد</span>
                    </a>
                    @endcan
                </div>
            </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>

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                        <tr>
                            <th class="px-6 py-4 text-center">
                                <input type="checkbox" id="selectAll" class="rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                            </th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">رقم الطلب</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">الوصف</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">الموردون</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">الموعد النهائي</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">عدد الأصناف</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">الحالة</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">الإجراءات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($requests as $request)
                        <tr class="hover:bg-blue-50 transition-colors duration-150 border-b border-gray-100">
                            <td class="px-6 py-4 text-center">
                                <input type="checkbox" name="quotation_requests[]" value="{{ $request->id }}" class="request-checkbox rounded border-gray-300 text-blue-600 focus:ring-blue-500">
                            </td>
                            <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">{{ $request->rq_number }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ Str::limit($request->description, 50) }}
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                <div class="flex flex-wrap gap-1">
                                    @forelse($request->suppliers as $supplier)
                                    <span class="inline-flex items-center px-2 py-1 rounded-full text-xs font-medium bg-blue-100 text-blue-700">
                                        {{ $supplier->name }}
                                    </span>
                                    @empty
                                    <span class="text-gray-500 text-sm">-</span>
                                    @endforelse
                                </div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ $request->deadline->format('Y-m-d') }}
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                <span class="font-semibold">{{ $request->items()->count() }}</span>
                            </td>
                            <td class="px-6 py-4">
                                @if($request->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($request->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($request->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">
                                    {{ $request->status }}
                                </span>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center justify-center gap-1">
                                    @can('purchasing.quotation-requests.view')
                                    <a href="{{ route('quotation-requests.show', $request) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="عرض التفاصيل">
                                        <i class="fas fa-eye mr-1"></i>
                                        <span>عرض</span>
                                    </a>
                                    @endcan

                                    @if($request->status == 'draft')
                                        @can('purchasing.quotation-requests.edit')
                                        <a href="{{ route('quotation-requests.edit', $request) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="تعديل">
                                            <i class="fas fa-edit mr-1"></i>
                                            <span>تعديل</span>
                                        </a>
                                        @endcan

                                        @can('purchasing.quotation-requests.delete')
                                        <form action="{{ route('quotation-requests.destroy', $request) }}" method="POST" class="inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="حذف" onclick="return confirm('هل أنت متأكد من حذف هذا الطلب؟')">
                                                <i class="fas fa-trash mr-1"></i>
                                                <span>حذف</span>
                                            </button>
                                        </form>
                                        @endcan
                                    @elseif($request->status == 'sent')
                                        @can('purchasing.quotation-requests.update')
                                        <form action="{{ route('quotation-requests.close', $request) }}" method="POST" class="inline">
                                            @csrf
                                            <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-gray-100 text-gray-600 hover:bg-gray-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" title="إغلاق">
                                                <i class="fas fa-times-circle mr-1"></i>
                                                <span>إغلاق</span>
                                            </button>
                                        </form>
                                        @endcan
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="px-6 py-12 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <i class="fas fa-inbox text-gray-400 text-4xl mb-3"></i>
                                    <p class="text-gray-600 font-medium">لا توجد طلبات عرض أسعار حالياً</p>
                                    <p class="text-gray-500 text-sm mt-1">ابدأ بطلب عرض سعر جديد</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if($requests->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                <div class="flex justify-center">
                    {{ $requests->links() }}
                </div>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection

@push('scripts')
<script>
    document.getElementById('selectAll').addEventListener('change', function() {
        const checkboxes = document.querySelectorAll('.request-checkbox');
        checkboxes.forEach(checkbox => {
            checkbox.checked = this.checked;
        });
    });

    function submitCompare() {
        const selected = Array.from(document.querySelectorAll('.request-checkbox:checked'))
            .map(cb => cb.value);
        
        if (selected.length === 0) {
            alert('يرجى اختيار طلب واحد على الأقل للمقارنة');
            return;
        }

        const form = document.getElementById('compareForm');
        form.innerHTML = ''; // Clear existing
        selected.forEach(id => {
            const input = document.createElement('input');
            input.type = 'hidden';
            input.name = 'quotation_requests[]';
            input.value = id;
            form.appendChild(input);
        });
        
        form.submit();
    }
</script>
@endpush
