@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-6xl mx-auto">
        <!-- Page Header -->
        <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">{{ $goodsReceiptNote->grn_number }}</p>
                </div>
                <a href="{{ route('goods-receipt-notes.index') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-gray-600 to-gray-700 text-white font-semibold rounded-lg hover:from-gray-700 hover:to-gray-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                    <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

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

        <!-- Basic Information Section -->
        <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 lg:grid-cols-3 gap-6">
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">رقم الاستقبال</label>
                        <p class="text-gray-900 font-semibold">{{ $goodsReceiptNote->grn_number }}</p>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">الحالة</label>
                        <div>
                            @if($goodsReceiptNote->status === 'received')
                            <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-box ml-1"></i>
                                مستقبل
                            </span>
                            @elseif($goodsReceiptNote->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($goodsReceiptNote->status === 'rejected')
                            <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>
                            @else
                            <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-hourglass-half 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">{{ $goodsReceiptNote->receipt_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">{{ $goodsReceiptNote->supplier->name ?? 'N/A' }}</p>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">طلب الشراء</label>
                        <a href="{{ route('purchase-orders.show', $goodsReceiptNote->purchaseOrder) }}" class="text-blue-600 hover:text-blue-700 font-semibold">
                            {{ $goodsReceiptNote->purchaseOrder->po_number }}
                        </a>
                    </div>

                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-2">تاريخ الإنشاء</label>
                        <p class="text-gray-900">{{ $goodsReceiptNote->created_at->format('Y-m-d H:i') }}</p>
                    </div>
                </div>
            </div>
        </div>

        <!-- Items Section -->
        <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">
                    <i class="fas fa-boxes ml-1"></i>
                    الأصناف المستقبلة
                </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-4 py-3 text-right text-sm font-semibold text-gray-700">اسم الصنف</th>
                            <th class="px-4 py-3 text-center text-sm font-semibold text-gray-700">الكمية المطلوبة</th>
                            <th class="px-4 py-3 text-center text-sm font-semibold text-gray-700">الكمية المستقبلة</th>
                            <th class="px-4 py-3 text-center text-sm font-semibold text-gray-700">الكمية المقبولة</th>
                            <th class="px-4 py-3 text-center text-sm font-semibold text-gray-700">الكمية المرفوضة</th>
                            <th class="px-4 py-3 text-right text-sm font-semibold text-gray-700">ملاحظات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($goodsReceiptNote->items as $item)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-4 py-3 text-right">
                                <div class="font-semibold text-gray-900">{{ $item->item->name ?? 'Unknown' }}</div>
                                <div class="text-xs text-gray-500">{{ $item->unit->name ?? '' }}</div>
                            </td>
                            <td class="px-4 py-3 text-center text-sm text-gray-900 font-semibold">
                                {{ number_format($item->quantity_ordered, 2) }}
                            </td>
                            <td class="px-4 py-3 text-center text-sm text-gray-900 font-semibold">
                                {{ number_format($item->quantity_received, 2) }}
                            </td>
                            <td class="px-4 py-3 text-center text-sm">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                    {{ number_format($item->quantity_accepted, 2) }}
                                </span>
                            </td>
                            <td class="px-4 py-3 text-center text-sm">
                                @if($item->quantity_rejected > 0)
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                    {{ number_format($item->quantity_rejected, 2) }}
                                </span>
                                @else
                                <span class="text-gray-500">-</span>
                                @endif
                            </td>
                            <td class="px-4 py-3 text-right text-sm text-gray-600">
                                {{ $item->rejection_reason ?? '-' }}
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="6" 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>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Quality Check Section -->
        @if($goodsReceiptNote->quality_check_passed !== null || $goodsReceiptNote->quality_check_notes)
        <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">
                    <i class="fas fa-check-square ml-1"></i>
                    فحص الجودة
                </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>
                        @if($goodsReceiptNote->quality_check_passed)
                        <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-circle ml-1"></i>
                            نجح الفحص
                        </span>
                        @else
                        <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>

                    @if($goodsReceiptNote->quality_check_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 bg-gray-50 rounded-lg p-3">{{ $goodsReceiptNote->quality_check_notes }}</p>
                    </div>
                    @endif
                </div>
            </div>
        </div>
        @endif

        <!-- Discrepancies Section -->
        @if($goodsReceiptNote->discrepancies_noted)
        <div class="bg-yellow-50 border-l-4 border-yellow-500 p-4 mb-6 rounded-r-lg">
            <div class="flex">
                <div class="flex-shrink-0">
                    <i class="fas fa-exclamation-triangle text-yellow-500 text-xl"></i>
                </div>
                <div class="mr-3">
                    <p class="text-yellow-700 font-medium">تم ملاحظة تباينات في الاستقبال</p>
                    @if($goodsReceiptNote->discrepancies_details)
                    <p class="text-yellow-600 text-sm mt-1">{{ $goodsReceiptNote->discrepancies_details }}</p>
                    @endif
                </div>
            </div>
        </div>
        @endif

        <!-- Notes Section -->
        @if($goodsReceiptNote->notes)
        <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">
                    <i class="fas fa-comment-dots ml-1"></i>
                    ملاحظات عامة
                </h2>
            </div>
            <div class="p-6">
                <p class="text-gray-900">{{ $goodsReceiptNote->notes }}</p>
            </div>
        </div>
        @endif

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

        <!-- Action Buttons -->
        @if($goodsReceiptNote->status !== 'accepted' && $goodsReceiptNote->status !== 'rejected')
        <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 flex items-center gap-4">
                @can('accept', $goodsReceiptNote)
                <form action="{{ route('goods-receipt-notes.accept', $goodsReceiptNote) }}" method="POST" class="inline">
                    @csrf
                    @method('POST')
                    <button type="submit" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-green-600 to-green-700 text-white font-semibold rounded-lg hover:from-green-700 hover:to-green-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                        <i class="fas fa-check ml-2"></i>
                        <span>قبول الاستقبال</span>
                    </button>
                </form>
                @endcan

                @can('reject', $goodsReceiptNote)
                <button type="button" onclick="showRejectModal()" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-red-600 to-red-700 text-white font-semibold rounded-lg hover:from-red-700 hover:to-red-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                    <i class="fas fa-times ml-2"></i>
                    <span>رفض الاستقبال</span>
                </button>
                @endcan

                @can('delete', $goodsReceiptNote)
                <form action="{{ route('goods-receipt-notes.destroy', $goodsReceiptNote) }}" method="POST" class="inline">
                    @csrf
                    @method('DELETE')
                    <button type="submit" onclick="return confirm('هل أنت متأكد من حذف هذا الاستقبال؟')" 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 shadow-lg hover:shadow-xl transform hover:scale-105">
                        <i class="fas fa-trash ml-2"></i>
                        <span>حذف</span>
                    </button>
                </form>
                @endcan
            </div>
        </div>
        @endif
    </div>
</div>

<!-- Reject Modal -->
<div id="rejectModal" class="hidden fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50">
    <div class="bg-white rounded-xl shadow-2xl max-w-md w-full mx-4">
        <div class="px-6 py-4 bg-gradient-to-r from-red-600 to-red-700 text-white">
            <h3 class="text-lg font-semibold">
                <i class="fas fa-times-circle ml-1"></i>
                رفض الاستقبال
            </h3>
        </div>
        <form action="{{ route('goods-receipt-notes.reject', $goodsReceiptNote) }}" method="POST" class="p-6">
            @csrf
            @method('POST')
            <div class="mb-4">
                <label for="rejection_reason" class="block text-sm font-medium text-gray-700 mb-2">سبب الرفض *</label>
                <textarea id="rejection_reason" name="rejection_reason" rows="4" required placeholder="أدخل سبب رفض الاستقبال..." class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-red-500 focus:border-transparent transition resize-none"></textarea>
                @error('rejection_reason')
                <p class="text-red-600 text-sm mt-1">{{ $message }}</p>
                @enderror
            </div>

            <div class="flex gap-3">
                <button type="button" onclick="closeRejectModal()" class="flex-1 px-4 py-2 bg-gray-300 text-gray-800 font-semibold rounded-lg hover:bg-gray-400 transition">
                    <i class="fas fa-times ml-1"></i>
                    إلغاء
                </button>
                <button type="submit" class="flex-1 px-4 py-2 bg-red-600 text-white font-semibold rounded-lg hover:bg-red-700 transition">
                    <i class="fas fa-check ml-1"></i>
                    تأكيد الرفض
                </button>
            </div>
        </form>
    </div>
</div>

<script>
function showRejectModal() {
    document.getElementById('rejectModal').classList.remove('hidden');
}

function closeRejectModal() {
    document.getElementById('rejectModal').classList.add('hidden');
}

// Close modal when clicking outside
document.getElementById('rejectModal')?.addEventListener('click', function(e) {
    if (e.target === this) {
        closeRejectModal();
    }
});
</script>
@endsection
