@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">
                        <i class="fas fa-file-invoice text-blue-600 ml-2"></i>
                        قائمة أوامر الشراء
                    </h1>
                    <p class="text-gray-600">عرض وإدارة جميع أوامر الشراء</p>
                </div>
                <a href="{{ route('purchase-orders.create') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-blue-600 to-blue-700 text-white font-semibold rounded-lg hover:from-blue-700 hover:to-blue-800 transition-all duration-200 shadow-lg hover:shadow-xl transform hover:scale-105">
                    <i class="fas fa-plus ml-2"></i>
                    <span>أمر شراء جديد</span>
                </a>
            </div>
        </div>

        <!-- تصفية وبحث -->
        <div class="bg-white rounded-lg shadow-md p-6 mb-6 border border-gray-200">
            <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4" id="filterForm">
                <!-- تاريخ البداية -->
                <div>
                    <label for="start_date" class="block text-sm font-medium 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 transition-all duration-200"
                    >
                </div>

                <!-- تاريخ النهاية -->
                <div>
                    <label for="end_date" class="block text-sm font-medium 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 transition-all duration-200"
                    >
                </div>

                <!-- المورد -->
                <div>
                    <label for="supplier_id" class="block text-sm font-medium text-gray-700 mb-2">المورد</label>
                    <select 
                        id="supplier_id" 
                        name="supplier_id"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200"
                    >
                        <option value="">كل الموردين</option>
                        @forelse($data->items() ?? [] as $po)
                            @if($po->supplier)
                                <option value="{{ $po->supplier->id }}" {{ request('supplier_id') == $po->supplier->id ? 'selected' : '' }}>
                                    {{ $po->supplier->name }}
                                </option>
                            @endif
                        @empty
                        @endforelse
                    </select>
                </div>

                <!-- الحالة -->
                <div>
                    <label for="status" class="block text-sm font-medium text-gray-700 mb-2">الحالة</label>
                    <select 
                        id="status" 
                        name="status"
                        class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent transition-all duration-200"
                    >
                        <option value="">كل الحالات</option>
                        <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>مسودة</option>
                        <option value="approved" {{ request('status') == 'approved' ? 'selected' : '' }}>موافق عليها</option>
                        <option value="sent" {{ request('status') == 'sent' ? 'selected' : '' }}>مرسلة</option>
                        <option value="received" {{ request('status') == 'received' ? 'selected' : '' }}>مستلمة</option>
                        <option value="partially_received" {{ request('status') == 'partially_received' ? 'selected' : '' }}>مستلمة جزئياً</option>
                        <option value="cancelled" {{ request('status') == 'cancelled' ? 'selected' : '' }}>ملغاة</option>
                    </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-all duration-200">
                        <i class="fas fa-search ml-1"></i>
                        بحث
                    </button>
                    <a href="{{ route('reports.purchase.po-list') }}" class="flex-1 px-4 py-2 bg-gray-300 hover:bg-gray-400 text-gray-900 font-medium rounded-lg transition-all duration-200">
                        <i class="fas fa-redo ml-1"></i>
                        إعادة تعيين
                    </a>
                </div>
            </form>
        </div>

        <!-- إحصائيات سريعة -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-lg shadow-md p-6 border-l-4 border-blue-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">إجمالي الأوامر</p>
                        <p class="text-3xl font-bold text-gray-900">{{ $data->total() ?? 0 }}</p>
                    </div>
                    <div class="text-blue-500 text-4xl">
                        <i class="fas fa-file-alt"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-6 border-l-4 border-green-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">إجمالي المبلغ</p>
                        <p class="text-3xl font-bold text-gray-900">
                            {{ number_format(collect($data->items())->sum('total_amount') ?? 0, 2) }} ر.س
                        </p>
                    </div>
                    <div class="text-green-500 text-4xl">
                        <i class="fas fa-coins"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-6 border-l-4 border-yellow-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">الأوامر المعلقة</p>
                        <p class="text-3xl font-bold text-gray-900">
                            {{ collect($data->items())->whereIn('status', ['draft', 'approved'])->count() ?? 0 }}
                        </p>
                    </div>
                    <div class="text-yellow-500 text-4xl">
                        <i class="fas fa-hourglass-half"></i>
                    </div>
                </div>
            </div>

            <div class="bg-white rounded-lg shadow-md p-6 border-l-4 border-red-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">الأوامر الملغاة</p>
                        <p class="text-3xl font-bold text-gray-900">
                            {{ collect($data->items())->where('status', 'cancelled')->count() ?? 0 }}
                        </p>
                    </div>
                    <div class="text-red-500 text-4xl">
                        <i class="fas fa-ban"></i>
                    </div>
                </div>
            </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-center font-semibold text-sm">الإجراءات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($data->items() ?? [] as $po)
                            <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 + ($data->currentPage() - 1) * $data->perPage() }}</td>
                                <td class="px-6 py-4">
                                    <div class="font-semibold text-gray-900">{{ $po->po_number }}</div>
                                    <div class="text-xs text-gray-500">{{ $po->id }}</div>
                                </td>
                                <td class="px-6 py-4">
                                    @if($po->supplier)
                                        <div class="font-medium text-gray-900">{{ $po->supplier->name }}</div>
                                        <div class="text-xs text-gray-500">{{ $po->supplier->city ?? 'N/A' }}</div>
                                    @else
                                        <span class="text-gray-500">غير محدد</span>
                                    @endif
                                </td>
                                <td class="px-6 py-4">
                                    <div class="text-gray-900">{{ \Carbon\Carbon::parse($po->po_date)->format('d/m/Y') }}</div>
                                    <div class="text-xs text-gray-500">{{ \Carbon\Carbon::parse($po->po_date)->format('H:i') }}</div>
                                </td>
                                <td class="px-6 py-4">
                                    <div class="font-bold text-green-600">{{ number_format($po->total_amount, 2) }} ر.س</div>
                                </td>
                                <td class="px-6 py-4">
                                    @switch($po->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('approved')
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                                <span class="w-2 h-2 bg-blue-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-purple-100 text-purple-700">
                                                <span class="w-2 h-2 bg-purple-500 rounded-full ml-2"></span>
                                                مرسلة
                                            </span>
                                            @break
                                        @case('received')
                                            <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('partially_received')
                                            <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                                <span class="w-2 h-2 bg-yellow-500 rounded-full ml-2"></span>
                                                مستلمة جزئياً
                                            </span>
                                            @break
                                        @case('cancelled')
                                            <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-gray-100 text-gray-700">
                                                {{ ucfirst($po->status) }}
                                            </span>
                                    @endswitch
                                </td>
                                <td class="px-6 py-4 whitespace-nowrap text-center">
                                    <div class="flex justify-center gap-2">
                                        @can('purchase-orders.view')
                                            <a href="{{ route('purchase-orders.show', $po) }}" 
                                               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"></i>
                                            </a>
                                        @endcan
                                        @can('purchase-orders.edit')
                                            @if(in_array($po->status, ['draft', 'approved']))
                                                <a href="{{ route('purchase-orders.edit', $po) }}" 
                                                   class="inline-flex items-center px-3 py-2 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" 
                                                   title="تعديل">
                                                    <i class="fas fa-edit"></i>
                                                </a>
                                            @endif
                                        @endcan
                                        @can('purchase-orders.delete')
                                            @if($po->status === 'draft')
                                                <form action="{{ route('purchase-orders.destroy', $po) }}" method="POST" class="inline-block">
                                                    @csrf
                                                    @method('DELETE')
                                                    <button type="submit" 
                                                            class="inline-flex items-center px-3 py-2 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" 
                                                            title="حذف"
                                                            onclick="return confirm('هل أنت متأكد من حذف هذا الأمر؟')">
                                                        <i class="fas fa-trash"></i>
                                                    </button>
                                                </form>
                                            @endif
                                        @endcan
                                    </div>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" 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>

        <!-- الترقيم -->
        @if($data->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200 mt-6">
                <div class="flex justify-center">
                    {{ $data->links() }}
                </div>
            </div>
        @endif
    </div>
</div>

@endsection
