@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">{{ $purchaseOrder->po_number }}</p>
                </div>
                <a href="{{ route('purchase-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

            @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">{{ $purchaseOrder->po_number }}</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">الحالة</label>
                        <div>
                            @if($purchaseOrder->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($purchaseOrder->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($purchaseOrder->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($purchaseOrder->status == 'partial_receive')
                            <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-box ml-1"></i>
                                استقبال جزئي
                            </span>
                            @elseif($purchaseOrder->status == 'received')
                            <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 ml-1"></i>
                                مستقبل
                            </span>
                            @elseif($purchaseOrder->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">{{ $purchaseOrder->supplier->name ?? 'N/A' }}</p>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">تاريخ الطلب</label>
                        <p class="text-gray-900">{{ $purchaseOrder->po_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">{{ $purchaseOrder->delivery_date ? $purchaseOrder->delivery_date->format('Y-m-d') : 'N/A' }}</p>
                    </div>
                    @if($purchaseOrder->requisition_id)
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">طلب الشراء الداخلي</label>
                        <p class="text-gray-900">
                            <a href="{{ route('purchase-requisitions.show', $purchaseOrder->requisition) }}" class="text-blue-600 hover:text-blue-800 underline">
                                {{ $purchaseOrder->requisition->requisition_number ?? 'N/A' }}
                            </a>
                        </p>
                    </div>
                    @endif
                    @if($purchaseOrder->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">{{ $purchaseOrder->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-4 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($purchaseOrder->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($purchaseOrder->tax_amount, 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($purchaseOrder->discount_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($purchaseOrder->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">الأصناف ({{ $purchaseOrder->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>
                            <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($purchaseOrder->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_received }}</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>
                            <td class="px-6 py-4 text-gray-700">{{ $item->costCenter->name ?? 'عام' }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="px-6 py-4 text-center text-gray-500">لا توجد أصناف</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Attachment -->
        @if($purchaseOrder->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/' . $purchaseOrder->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($purchaseOrder->status == 'draft')
                @can('update', $purchaseOrder)
                <form action="{{ route('purchase-orders.send', $purchaseOrder) }}" 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('purchase-orders.edit', $purchaseOrder) }}" 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>
                @endcan

                @can('delete', $purchaseOrder)
                <form action="{{ route('purchase-orders.destroy', $purchaseOrder) }}" 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 ml-2"></i>
                        <span>حذف</span>
                    </button>
                </form>
                @endcan
            @endif

            @if($purchaseOrder->status == 'sent')
                @can('update', $purchaseOrder)
                <form action="{{ route('purchase-orders.accept', $purchaseOrder) }}" 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>
                @endcan
            @endif

            @if(in_array($purchaseOrder->status, ['accepted', 'received', 'partial_receive']))
                @if($purchaseOrder->supplierInvoice)
                    <a href="{{ route('supplier-invoices.show', $purchaseOrder->supplierInvoice) }}" class="inline-flex items-center px-6 py-3 rounded-lg font-medium transition-all duration-150" style="background-color: #15803d; color: #ffffff;">
                        <i class="fas fa-external-link-alt ml-2"></i>
                        <span>عرض فاتورة المشتريات المرتبطة ({{ $purchaseOrder->supplierInvoice->invoice_number }})</span>
                    </a>
                @else
                    <a href="{{ route('supplier-invoices.create-from-po', $purchaseOrder) }}" 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
            @endif

            @if($purchaseOrder->goodsReceiptNotes->count() > 0)
                @foreach($purchaseOrder->goodsReceiptNotes as $grn)
                    <a href="{{ route('goods-receipt-notes.show', $grn) }}" class="inline-flex items-center px-6 py-3 rounded-lg font-medium transition-all duration-150" style="background-color: #0891b2; color: #ffffff;">
                        <i class="fas fa-box-open ml-2"></i>
                        <span>عرض إشعار الاستقبال المرتبط ({{ $grn->grn_number }})</span>
                    </a>
                @endforeach
            @elseif(in_array($purchaseOrder->status, ['sent', 'accepted', 'partial_receive']))
                <a href="{{ route('goods-receipt-notes.create-from-po', $purchaseOrder) }}" class="inline-flex items-center px-6 py-3 rounded-lg font-medium transition-all duration-150" style="background-color: #0891b2; color: #ffffff;">
                    <i class="fas fa-box-open ml-2"></i>
                    <span>تحويل إلى إشعار استقبال</span>
                </a>
            @endif

            @if(!in_array($purchaseOrder->status, ['received', 'cancelled']))
                @can('cancel', $purchaseOrder)
                <form action="{{ route('purchase-orders.cancel', $purchaseOrder) }}" 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" onclick="return confirm('هل أنت متأكد من إلغاء هذا الطلب؟')">
                        <i class="fas fa-ban ml-2"></i>
                        <span>إلغاء الطلب</span>
                    </button>
                </form>
                @endcan
            @endif
        </div>
    </div>
</div>
@endsection
