@extends('layouts.app')

@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>
        </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 class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 hover:shadow-xl transition-shadow">
                <div class="bg-gradient-to-r from-blue-600 to-blue-700 text-white px-6 py-4">
                    <p class="text-sm font-medium opacity-90">إجمالي الأذونات</p>
                </div>
                <div class="px-6 py-4">
                    <p class="text-3xl font-bold text-blue-600">{{ $statistics['total_issues'] ?? 0 }}</p>
                    <p class="text-gray-600 text-sm mt-1">إذن صرف</p>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 hover:shadow-xl transition-shadow">
                <div class="bg-gradient-to-r from-green-600 to-green-700 text-white px-6 py-4">
                    <p class="text-sm font-medium opacity-90">إجمالي الكمية</p>
                </div>
                <div class="px-6 py-4">
                    <p class="text-3xl font-bold text-green-600">{{ number_format($statistics['total_quantity'] ?? 0, 2) }}</p>
                    <p class="text-gray-600 text-sm mt-1">وحدة</p>
                </div>
            </div>

            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 hover:shadow-xl transition-shadow">
                <div class="bg-gradient-to-r from-purple-600 to-purple-700 text-white px-6 py-4">
                    <p class="text-sm font-medium opacity-90">إجمالي التكلفة</p>
                </div>
                <div class="px-6 py-4">
                    <p class="text-3xl font-bold text-purple-600">{{ number_format($statistics['total_cost'] ?? 0, 2) }}</p>
                    <p class="text-gray-600 text-sm mt-1">ريال</p>
                </div>
            </div>
        </div>

        <!-- التصفية -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-8">
            <div class="px-6 py-4 border-b border-gray-200 bg-gray-50">
                <h3 class="text-lg font-semibold text-gray-900">التصفية والبحث</h3>
            </div>
            <div class="p-6">
                <form method="GET" action="{{ route('inventory.reports.issues') }}" class="grid grid-cols-1 md:grid-cols-3 gap-4">
                    <div>
                        <label for="warehouse_id" class="block text-sm font-medium text-gray-700 mb-2">المخزن</label>
                        <select id="warehouse_id" name="warehouse_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                            <option value="">جميع المخازن</option>
                            @foreach($warehouses as $warehouse)
                                <option value="{{ $warehouse->id }}" @selected($filters['warehouse_id'] == $warehouse->id)>
                                    {{ $warehouse->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div>
                        <label for="item_id" class="block text-sm font-medium text-gray-700 mb-2">الصنف</label>
                        <select id="item_id" name="item_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                            <option value="">جميع الأصناف</option>
                            @foreach($items as $item)
                                <option value="{{ $item->id }}" @selected($filters['item_id'] == $item->id)>
                                    {{ $item->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="flex items-end gap-2">
                        <button type="submit" class="flex-1 px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition-colors">
                            <i class="fas fa-search ml-2"></i>تصفية
                        </button>
                        <a href="{{ route('inventory.reports.issues') }}" class="flex-1 px-4 py-2 bg-gray-300 hover:bg-gray-400 text-gray-900 font-medium rounded-lg transition-colors text-center">
                            <i class="fas fa-redo ml-2"></i>إعادة تعيين
                        </a>
                    </div>
                </form>
            </div>
        </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-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>
                            <th class="px-6 py-4 text-center font-semibold text-sm">الإجراءات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($issues as $issue)
                            <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">
                                    <div class="font-semibold text-gray-900">{{ $issue->document_number ?? 'N/A' }}</div>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="text-gray-700">{{ is_string($issue->document_date) ? $issue->document_date : ($issue->document_date ? $issue->document_date->format('Y-m-d') : 'N/A') }}</span>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="inline-block px-3 py-1 bg-blue-100 text-blue-700 rounded-full text-sm">
                                        {{ $issue->warehouse_name ?? 'غير محدد' }}
                                    </span>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="text-gray-700">{{ $issue->item_name ?? 'غير محدد' }}</span>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="text-gray-900 font-semibold">{{ number_format($issue->quantity ?? 0, 2) }}</span>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="text-gray-900 font-semibold">{{ number_format($issue->total_cost ?? 0, 2) }}</span>
                                </td>
                                <td class="px-6 py-4">
                                    @php
                                        $status = $issue->status ?? 'draft';
                                        $statusMap = [
                                            'draft' => ['text' => 'مسودة', 'color' => 'yellow'],
                                            'approved' => ['text' => 'معتمد', 'color' => 'green'],
                                            'cancelled' => ['text' => 'ملغي', 'color' => 'red']
                                        ];
                                        $statusInfo = $statusMap[$status] ?? ['text' => $status, 'color' => 'gray'];
                                    @endphp
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-{{ $statusInfo['color'] }}-100 text-{{ $statusInfo['color'] }}-700">
                                        <span class="w-2 h-2 bg-{{ $statusInfo['color'] }}-500 rounded-full ml-2"></span>
                                        {{ $statusInfo['text'] }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-center">
                                    <div class="flex gap-2 justify-center">
                                        @if($issue->id)
                                            <a href="#" 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>
                                        @endif
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="9" 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($issues->count() > 0)
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                <div class="flex justify-center">
                    <!-- يمكن إضافة pagination هنا إذا كانت النتائج مرقمة -->
                </div>
            </div>
            @endif
        </div>
    </div>
</div>

@push('scripts')
<script>
    // إضافة تأثيرات إضافية إذا لزم الأمر
    document.addEventListener('DOMContentLoaded', function() {
        // يمكن إضافة أكواد JavaScript هنا
    });
</script>
@endpush

@endsection
