@extends('layouts.app')

@section('title', 'طلبات الشراء الداخلية')

@section('content')
<div class="min-h-screen bg-slate-50 py-6 px-4 md:px-8">
    <div class="max-w-7xl mx-auto">
        <!-- Compact Header -->
        <div class="mb-6 flex flex-col md:flex-row md:items-center justify-between gap-4">
            <div>
                <h1 class="text-2xl font-bold text-gray-900">طلبات الشراء الداخلية</h1>
                <p class="text-gray-500 text-sm">إدارة وتتبع كافة طلبات الشراء الداخلية وحالاتها</p>
            </div>
            @can('purchasing.requisitions.create')
            <a href="{{ route('purchase-requisitions.create') }}" class="inline-flex items-center px-6 py-2.5 bg-blue-600 text-white font-bold rounded-xl hover:bg-blue-700 transition-all duration-200 shadow-md hover:shadow-lg">
                <i class="fas fa-plus ml-2 text-xs"></i>
                <span>إنشاء طلب شراء جديد</span>
            </a>
            @endcan
        </div>

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

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

        <div class="bg-white rounded-xl shadow-sm border border-gray-200 overflow-hidden">
            <div class="overflow-x-auto">
                <table class="w-full text-right border-collapse">
                    <thead>
                        <tr class="bg-slate-50 text-slate-700 text-xs font-bold uppercase border-b border-slate-200">
                            <th class="px-6 py-4 w-12 text-center">#</th>
                            <th class="px-6 py-4 w-40">رقم الطلب</th>
                            <th class="px-6 py-4">الوصف</th>
                            <th class="px-6 py-4 w-40">مركز التكلفة</th>
                            <th class="px-6 py-4 w-32 text-center">الأصناف</th>
                            <th class="px-6 py-4 w-32 text-center">الحالة</th>
                            <th class="px-6 py-4 w-32 text-center">التاريخ</th>
                            <th class="px-6 py-4 w-40 text-center border-r border-slate-100">الإجراءات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        @forelse($requisitions as $requisition)
                        <tr class="hover:bg-slate-50/50 transition-colors">
                            <td class="px-6 py-4 text-slate-400 font-medium text-center text-sm">{{ $loop->iteration + ($requisitions->currentPage() - 1) * $requisitions->perPage() }}</td>
                            <td class="px-6 py-4">
                                <div class="font-bold text-blue-700 hover:text-blue-900 transition-colors">
                                    <a href="{{ route('purchase-requisitions.show', $requisition) }}">{{ $requisition->requisition_number }}</a>
                                </div>
                            </td>
                            <td class="px-6 py-4">
                                <div class="text-sm text-slate-800 font-medium truncate max-w-xs md:max-w-md lg:max-w-lg" title="{{ $requisition->description }}">
                                    {{ $requisition->description }}
                                </div>
                            </td>
                            <td class="px-6 py-4">
                                <span class="text-xs font-bold px-2 py-1 bg-slate-100 text-slate-700 rounded border border-slate-200">
                                    {{ $requisition->costCenter->name ?? 'عام' }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center">
                                <div class="flex items-center justify-center gap-1.5">
                                    <i class="fas fa-box text-slate-400 text-[10px]"></i>
                                    <span class="text-sm font-black text-slate-800">{{ $requisition->items->count() }}</span>
                                </div>
                            </td>
                            <td class="px-6 py-4 text-center">
                                @if($requisition->status == 'draft')
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-[10px] font-black bg-yellow-100 text-yellow-800 border border-yellow-200 uppercase">
                                    مسودة
                                </span>
                                @elseif($requisition->status == 'approved')
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-[10px] font-black bg-green-100 text-green-800 border border-green-200 uppercase">
                                    موافق عليه
                                </span>
                                @elseif($requisition->status == 'rejected')
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-[10px] font-black bg-red-100 text-red-800 border border-red-200 uppercase">
                                    مرفوض
                                </span>
                                @else
                                <span class="inline-flex items-center px-2.5 py-0.5 rounded-full text-[10px] font-black bg-slate-100 text-slate-800 border border-slate-200 uppercase">
                                    {{ $requisition->status }}
                                </span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-xs text-slate-600 font-bold text-center">
                                {{ $requisition->created_at->format('Y-m-d') }}
                            </td>
                            <td class="px-6 py-4 border-r border-slate-50">
                                <div class="flex items-center justify-center gap-2">
                                    @can('purchasing.requisitions.view')
                                    <a href="{{ route('purchase-requisitions.show', $requisition) }}" class="p-1.5 text-slate-400 hover:text-blue-700 hover:bg-blue-50 rounded-lg transition-all" title="عرض التفاصيل">
                                        <i class="fas fa-eye text-base"></i>
                                    </a>
                                    @endcan

                                    @if($requisition->status == 'draft')
                                        @can('purchasing.requisitions.edit')
                                        <a href="{{ route('purchase-requisitions.edit', $requisition) }}" class="p-1.5 text-slate-400 hover:text-amber-700 hover:bg-amber-50 rounded-lg transition-all" title="تعديل">
                                            <i class="fas fa-edit text-base"></i>
                                        </a>
                                        @endcan

                                        @can('purchasing.requisitions.delete')
                                        <form action="{{ route('purchase-requisitions.destroy', $requisition) }}" method="POST" class="inline">
                                            @csrf
                                            @method('DELETE')
                                            <button type="submit" class="p-1.5 text-slate-400 hover:text-red-700 hover:bg-red-50 rounded-lg transition-all" title="حذف" onclick="return confirm('هل أنت متأكد من حذف هذا الطلب؟')">
                                                <i class="fas fa-trash-alt text-base"></i>
                                            </button>
                                        </form>
                                        @endcan
                                    @endif
                                </div>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="px-6 py-20 text-center">
                                <div class="flex flex-col items-center justify-center">
                                    <div class="w-16 h-16 bg-gray-50 rounded-full flex items-center justify-center mb-4">
                                        <i class="fas fa-inbox text-gray-200 text-3xl"></i>
                                    </div>
                                    <h3 class="text-lg font-bold text-gray-800">لا توجد طلبات شراء</h3>
                                    <p class="text-gray-500 text-sm mt-1 max-w-xs mx-auto">لم يتم إنشاء أي طلبات شراء داخلية حتى الآن. ابدأ بإنشاء أول طلب لك.</p>
                                    @can('purchasing.requisitions.create')
                                    <a href="{{ route('purchase-requisitions.create') }}" class="mt-6 inline-flex items-center px-6 py-2 bg-blue-600 text-white font-bold rounded-xl hover:bg-blue-700 transition-all">
                                        <i class="fas fa-plus ml-2 text-xs"></i>
                                        إنشاء طلب شراء
                                    </a>
                                    @endcan
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            @if($requisitions->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                {{ $requisitions->links() }}
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
