@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">{{ $quotation->quotation_number }}</p>
                </div>
                <a href="{{ route('quotations.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">{{ $quotation->quotation_number }}</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">الحالة</label>
                        <div>
                            @switch($quotation->status)
                                @case('draft')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-gray-100 text-gray-700">
                                        <span class="w-2 h-2 bg-gray-500 rounded-full ml-2"></span>
                                        مسودة
                                    </span>
                                    @break
                                @case('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>
                                    @break
                                @case('received')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-purple-100 text-purple-700">
                                        <i class="fas fa-check ml-1"></i>
                                        مستلم
                                    </span>
                                    @break
                                @case('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>
                                    @break
                                @case('rejected')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                        <span class="w-2 h-2 bg-red-500 rounded-full ml-2"></span>
                                        مرفوض
                                    </span>
                                    @break
                                @default
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                        {{ $quotation->status }}
                                    </span>
                            @endswitch
                        </div>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">المورد</label>
                        <p class="text-gray-900">{{ $quotation->supplier->name ?? 'غير محدد' }}</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">تاريخ الصلاحية</label>
                        <p class="text-gray-900">{{ $quotation->validity_date->format('Y-m-d') }}</p>
                    </div>
                    @if($quotation->request)
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">طلب عرض السعر</label>
                        <p class="text-gray-900">{{ $quotation->request->request_number ?? 'غير محدد' }}</p>
                    </div>
                    @endif
                    @if($quotation->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">{{ $quotation->notes }}</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-r border-gray-200 pr-6">
                        <label class="block text-sm font-medium text-gray-700 mb-2">المجموع الجزئي</label>
                        <p class="text-2xl font-bold text-gray-900">{{ number_format($quotation->subtotal, 2) }} ريال</p>
                    </div>
                    <div class="border-r border-gray-200 pr-6">
                        <label class="block text-sm font-medium text-gray-700 mb-2">الضريبة</label>
                        <p class="text-2xl font-bold text-gray-900">{{ number_format($quotation->tax_amount, 2) }} ريال</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">الإجمالي</label>
                        <p class="text-2xl font-bold text-blue-600">{{ number_format($quotation->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">الأصناف ({{ $quotation->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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($quotation->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 ?? 'غير محدد' }}</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 ?? 'غير محدد' }}</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="6" class="px-6 py-4 text-center text-gray-500">لا توجد أصناف</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Attachment -->
        @if($quotation->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/' . $quotation->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

        <!-- Actions -->
        <div class="flex gap-3 justify-end">
            @if($quotation->status === 'draft')
                <a href="{{ route('quotations.edit', $quotation) }}" 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>

                <form action="{{ route('quotations.send', $quotation) }}" 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>

                <form action="{{ route('quotations.destroy', $quotation) }}" 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>
            @elseif(in_array($quotation->status, ['received', 'sent']))
                <form action="{{ route('quotations.accept', $quotation) }}" 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-check ml-2"></i>
                        <span>قبول</span>
                    </button>
                </form>

                <form action="{{ route('quotations.reject', $quotation) }}" method="POST" class="inline">
                    @csrf
                    <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">
                        <i class="fas fa-times ml-2"></i>
                        <span>رفض</span>
                    </button>
                </form>
            @endif
        </div>
    </div>
</div>
@endsection
