@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">
        <!-- Header Section -->
        <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">
                    <button onclick="window.print()" class="inline-flex items-center px-4 py-2 bg-gray-600 text-white font-semibold rounded-lg hover:bg-gray-700 transition-all duration-200">
                        <i class="fas fa-print ml-2"></i>
                        <span>طباعة</span>
                    </button>
                    <a href="?export=true&format=excel" class="inline-flex items-center px-4 py-2 bg-green-600 text-white font-semibold rounded-lg hover:bg-green-700 transition-all duration-200">
                        <i class="fas fa-file-excel ml-2"></i>
                        <span>Excel</span>
                    </a>
                    <a href="?export=true&format=pdf" class="inline-flex items-center px-4 py-2 bg-red-600 text-white font-semibold rounded-lg hover:bg-red-700 transition-all duration-200">
                        <i class="fas fa-file-pdf ml-2"></i>
                        <span>PDF</span>
                    </a>
                </div>
            </div>
        </div>

        <!-- Filter Section -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
            <form method="GET" class="space-y-4">
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
                    <!-- Start Date -->
                    <div>
                        <label for="start_date" class="block text-sm font-semibold text-gray-700 mb-2">تاريخ البداية</label>
                        <input type="date" id="start_date" name="start_date" 
                               value="{{ request('start_date') }}"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                    </div>

                    <!-- End Date -->
                    <div>
                        <label for="end_date" class="block text-sm font-semibold text-gray-700 mb-2">تاريخ النهاية</label>
                        <input type="date" id="end_date" name="end_date" 
                               value="{{ request('end_date') }}"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                    </div>

                    <!-- Search -->
                    <div>
                        <label for="search" class="block text-sm font-semibold text-gray-700 mb-2">بحث</label>
                        <input type="text" id="search" name="search" 
                               placeholder="ابحث برقم الإشعار أو اسم المورد..."
                               value="{{ request('search') }}"
                               class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                    </div>
                </div>

                <div class="flex gap-3 justify-end">
                    <button type="reset" class="inline-flex items-center px-4 py-2 bg-gray-300 text-gray-800 font-semibold rounded-lg hover:bg-gray-400 transition-all duration-200">
                        <i class="fas fa-redo ml-2"></i>
                        <span>إعادة تعيين</span>
                    </button>
                    <button type="submit" class="inline-flex items-center px-6 py-2 bg-blue-600 text-white font-semibold rounded-lg hover:bg-blue-700 transition-all duration-200 shadow-lg">
                        <i class="fas fa-search ml-2"></i>
                        <span>بحث</span>
                    </button>
                </div>
            </form>
        </div>

        <!-- Statistics Section -->
        @if(isset($data) && is_array($data))
            <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-8">
                <!-- Total GRNs -->
                <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-blue-500">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-600 text-sm font-medium mb-1">إجمالي الإشعارات</p>
                            <p class="text-3xl font-bold text-gray-900">{{ $data['statistics']['total_grns'] ?? 0 }}</p>
                        </div>
                        <div class="bg-blue-100 p-3 rounded-lg">
                            <i class="fas fa-clipboard-list text-blue-600 text-xl"></i>
                        </div>
                    </div>
                </div>

                <!-- Total Items -->
                <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-green-500">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-600 text-sm font-medium mb-1">إجمالي الأصناف</p>
                            <p class="text-3xl font-bold text-gray-900">{{ $data['statistics']['total_items'] ?? 0 }}</p>
                        </div>
                        <div class="bg-green-100 p-3 rounded-lg">
                            <i class="fas fa-cube text-green-600 text-xl"></i>
                        </div>
                    </div>
                </div>

                <!-- Accepted Quantity -->
                <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-emerald-500">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-600 text-sm font-medium mb-1">الكمية المقبولة</p>
                            <p class="text-3xl font-bold text-gray-900">{{ $data['statistics']['accepted_quantity'] ?? 0 }}</p>
                        </div>
                        <div class="bg-emerald-100 p-3 rounded-lg">
                            <i class="fas fa-check-circle text-emerald-600 text-xl"></i>
                        </div>
                    </div>
                </div>

                <!-- Rejected Quantity -->
                <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-red-500">
                    <div class="flex justify-between items-start">
                        <div>
                            <p class="text-gray-600 text-sm font-medium mb-1">الكمية المرفوضة</p>
                            <p class="text-3xl font-bold text-gray-900">{{ $data['statistics']['rejected_quantity'] ?? 0 }}</p>
                        </div>
                        <div class="bg-red-100 p-3 rounded-lg">
                            <i class="fas fa-times-circle text-red-600 text-xl"></i>
                        </div>
                    </div>
                </div>
            </div>
        @endif

        <!-- GRN Details Table -->
        <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-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($data['grns'] ?? [] as $grn)
                            <tr class="hover:bg-blue-50 transition-colors duration-150 border-b border-gray-100">
                                <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                                <td class="px-6 py-4">
                                    <span class="font-semibold text-gray-900">{{ $grn['number'] ?? 'N/A' }}</span>
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-600">
                                    {{ \Carbon\Carbon::parse($grn['date'])->format('Y-m-d') ?? 'N/A' }}
                                </td>
                                <td class="px-6 py-4">
                                    <span class="text-gray-900 font-medium">{{ $grn['supplier_name'] ?? 'N/A' }}</span>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="text-gray-900 font-medium">{{ $grn['warehouse_name'] ?? 'N/A' }}</span>
                                </td>
                                <td class="px-6 py-4 text-gray-900 font-semibold">
                                    {{ number_format($grn['total_quantity'] ?? 0, 2) }}
                                </td>
                                <td class="px-6 py-4">
                                    @if(($grn['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(($grn['status'] ?? '') === '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>
                                    @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-pause-circle ml-1"></i>
                                            معلق
                                        </span>
                                    @endif
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-center">
                                    <div class="flex justify-center gap-2">
                                        <a href="{{ route('goods-receipt-notes.show', $grn['id'] ?? '#') }}" 
                                           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" 
                                           title="عرض التفاصيل">
                                            <i class="fas fa-eye"></i>
                                        </a>
                                    </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>
        </div>

        <!-- Pagination -->
        @if(isset($data['grns']) && method_exists($data['grns'], 'hasPages') && $data['grns']->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                <div class="flex justify-center">
                    {{ $data['grns']->links() }}
                </div>
            </div>
        @endif

        <!-- Detailed Items Section -->
        @if(isset($data['items']) && count($data['items']) > 0)
            <div class="mt-8">
                <h2 class="text-2xl font-bold text-gray-900 mb-6">تفاصيل البنود المستقبلة</h2>
                
                <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-blue-600 to-blue-700 text-white">
                                <tr>
                                    <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-right font-semibold text-sm">الإجمالي</th>
                                </tr>
                            </thead>
                            <tbody class="divide-y divide-gray-200">
                                @foreach($data['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">
                                            <span class="font-semibold text-gray-900">{{ $item['grn_number'] ?? 'N/A' }}</span>
                                        </td>
                                        <td class="px-6 py-4">
                                            <span class="text-gray-900 font-medium">{{ $item['item_name'] ?? 'N/A' }}</span>
                                        </td>
                                        <td class="px-6 py-4 text-gray-900">
                                            {{ number_format($item['ordered_quantity'] ?? 0, 2) }}
                                        </td>
                                        <td class="px-6 py-4 text-green-600 font-semibold">
                                            {{ number_format($item['received_quantity'] ?? 0, 2) }}
                                        </td>
                                        <td class="px-6 py-4">
                                            @php
                                                $diff = ($item['ordered_quantity'] ?? 0) - ($item['received_quantity'] ?? 0);
                                            @endphp
                                            @if($diff > 0)
                                                <span class="text-red-600 font-semibold">-{{ number_format($diff, 2) }}</span>
                                            @elseif($diff < 0)
                                                <span class="text-blue-600 font-semibold">+{{ number_format(abs($diff), 2) }}</span>
                                            @else
                                                <span class="text-gray-600">—</span>
                                            @endif
                                        </td>
                                        <td class="px-6 py-4 text-gray-900">
                                            {{ number_format($item['unit_price'] ?? 0, 2) }}
                                        </td>
                                        <td class="px-6 py-4 text-gray-900 font-semibold">
                                            {{ number_format($item['total_price'] ?? 0, 2) }}
                                        </td>
                                    </tr>
                                @endforeach
                            </tbody>
                        </table>
                    </div>
                </div>
            </div>
        @endif

        <!-- Summary Section -->
        @if(isset($data['summary']) && is_array($data['summary']))
            <div class="mt-8 grid grid-cols-1 md:grid-cols-2 gap-6">
                <!-- Summary Box 1 -->
                <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                    <h3 class="text-lg font-bold text-gray-900 mb-4">ملخص الاستقبال</h3>
                    <div class="space-y-3">
                        <div class="flex justify-between items-center py-2 border-b border-gray-200">
                            <span class="text-gray-600">إجمالي المبلغ:</span>
                            <span class="font-semibold text-gray-900">{{ number_format($data['summary']['total_amount'] ?? 0, 2) }}</span>
                        </div>
                        <div class="flex justify-between items-center py-2 border-b border-gray-200">
                            <span class="text-gray-600">الخصم:</span>
                            <span class="font-semibold text-gray-900">{{ number_format($data['summary']['total_discount'] ?? 0, 2) }}</span>
                        </div>
                        <div class="flex justify-between items-center py-2 border-b border-gray-200">
                            <span class="text-gray-600">الضريبة:</span>
                            <span class="font-semibold text-gray-900">{{ number_format($data['summary']['total_tax'] ?? 0, 2) }}</span>
                        </div>
                        <div class="flex justify-between items-center py-3 bg-blue-50 px-3 rounded-lg">
                            <span class="text-gray-900 font-bold">الإجمالي النهائي:</span>
                            <span class="font-bold text-blue-600 text-lg">{{ number_format($data['summary']['net_total'] ?? 0, 2) }}</span>
                        </div>
                    </div>
                </div>

                <!-- Summary Box 2 -->
                <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                    <h3 class="text-lg font-bold text-gray-900 mb-4">إحصائيات الجودة</h3>
                    <div class="space-y-3">
                        <div class="flex justify-between items-center py-2 border-b border-gray-200">
                            <span class="text-gray-600">الإشعارات المقبولة:</span>
                            <span class="font-semibold text-green-600">{{ $data['summary']['accepted_grns'] ?? 0 }}</span>
                        </div>
                        <div class="flex justify-between items-center py-2 border-b border-gray-200">
                            <span class="text-gray-600">الإشعارات المرفوضة:</span>
                            <span class="font-semibold text-red-600">{{ $data['summary']['rejected_grns'] ?? 0 }}</span>
                        </div>
                        <div class="flex justify-between items-center py-2 border-b border-gray-200">
                            <span class="text-gray-600">الإشعارات المعلقة:</span>
                            <span class="font-semibold text-yellow-600">{{ $data['summary']['pending_grns'] ?? 0 }}</span>
                        </div>
                        <div class="flex justify-between items-center py-3 bg-green-50 px-3 rounded-lg">
                            <span class="text-gray-900 font-bold">نسبة القبول:</span>
                            <span class="font-bold text-green-600 text-lg">
                                {{ $data['summary']['total_grns'] > 0 
                                    ? number_format(($data['summary']['accepted_grns'] / $data['summary']['total_grns'] * 100), 1) 
                                    : 0 }}%
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        @endif
    </div>
</div>

<style>
    @media print {
        body { background: white; }
        .print\:hidden { display: none !important; }
        table { page-break-inside: avoid; }
    }
</style>
@endsection
