﻿@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>
                <!-- ACTION BUTTONS -->
                @can('inventory.issues.create')
                <div class="flex gap-3">
                    <a href="{{ route('inventory.issues.import') }}" 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">
                        <i class="fas fa-file-import ml-2"></i>
                        <span>استيراد من Excel</span>
                    </a>
                    <a href="{{ route('inventory.issues.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>
                @endcan
            </div>

            <!-- SUCCESS MESSAGE -->
            @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

            <!-- ERROR MESSAGE -->
            @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>

        <!-- FILTERS SECTION -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 p-6 mb-6">
            <form action="{{ route('inventory.issues.index') }}" method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">البحث</label>
                    <input type="text" name="search" value="{{ request('search') }}" placeholder="رقم المستند..." class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">المخزن</label>
                    <select name="warehouse_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">جميع المخازن</option>
                        @foreach($warehouses ?? [] as $warehouse)
                            <option value="{{ $warehouse->id }}" {{ request('warehouse_id') == $warehouse->id ? 'selected' : '' }}>{{ $warehouse->name }}</option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">الحالة</label>
                    <select name="status" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="">جميع الحالات</option>
                        <option value="draft" {{ request('status') == 'draft' ? 'selected' : '' }}>مسودة</option>
                        <option value="approved" {{ request('status') == 'approved' ? '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 text-white rounded-lg hover:bg-blue-700 font-medium transition-colors">
                        <i class="fas fa-filter ml-2"></i> تطبيق
                    </button>
                    <a href="{{ route('inventory.issues.index') }}" class="flex-1 px-4 py-2 bg-gray-200 text-gray-700 rounded-lg hover:bg-gray-300 font-medium text-center transition-colors">
                        <i class="fas fa-redo ml-2"></i> إعادة
                    </a>
                </div>
            </form>
        </div>

        <!-- DATA TABLE -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="overflow-x-auto">
                <table class="w-full">
                    <!-- TABLE HEADER -->
                    <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>
                    <!-- TABLE BODY -->
                    <tbody class="divide-y divide-gray-200">
                        @forelse($documents as $document)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 font-medium text-gray-600">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ $document->document_number }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $document->document_date->format('Y-m-d') }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $document->fromWarehouse->name ?? 'غير محدد' }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $document->creator->name ?? 'غير محدد' }}</td>
                            <td class="px-6 py-4">
                                @if($document->status == 'draft')
                                    <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>
                                @elseif($document->status == 'approved')
                                    <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($document->status == '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>
                                @endif
                            </td>
                            <td class="px-6 py-4 flex items-center justify-center gap-1">
                                @can('inventory.issues.view')
                                <a href="{{ route('inventory.issues.show', $document->id) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-blue-100 text-blue-600 hover:bg-blue-200 text-sm font-medium transition-colors" title="عرض التفاصيل">
                                    <i class="fas fa-eye mr-1"></i> عرض
                                </a>
                                @endcan
                                
                                @if($document->status == 'draft')
                                    @can('inventory.issues.edit')
                                    <a href="{{ route('inventory.issues.edit', $document->id) }}" class="inline-flex items-center px-3 py-2 rounded-lg bg-amber-100 text-amber-600 hover:bg-amber-200 text-sm font-medium transition-colors" title="تعديل">
                                        <i class="fas fa-edit mr-1"></i> تعديل
                                    </a>
                                    @endcan
                                    
                                    @can('inventory.issues.approve')
                                    <form action="{{ route('inventory.issues.approve', $document->id) }}" method="POST" class="inline" onsubmit="return confirm('هل أنت متأكد من اعتماد هذا المستند؟');">
                                        @csrf
                                        @method('PATCH')
                                        <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-green-100 text-green-600 hover:bg-green-200 text-sm font-medium transition-colors" title="اعتماد">
                                            <i class="fas fa-check mr-1"></i> اعتماد
                                        </button>
                                    </form>
                                    @endcan
                                    
                                    @can('inventory.issues.cancel')
                                    <form action="{{ route('inventory.issues.cancel', $document->id) }}" method="POST" class="inline" onsubmit="return confirm('هل أنت متأكد من إلغاء هذا المستند؟');">
                                        @csrf
                                        @method('PATCH')
                                        <button type="submit" class="inline-flex items-center px-3 py-2 rounded-lg bg-red-100 text-red-600 hover:bg-red-200 text-sm font-medium transition-colors" title="إلغاء">
                                            <i class="fas fa-times mr-1"></i> إلغاء
                                        </button>
                                    </form>
                                    @endcan
                                @endif
                                
                                @can('inventory.issues.print')
                                <a href="{{ route('inventory.issues.print', $document->id) }}" target="_blank" class="inline-flex items-center px-3 py-2 rounded-lg bg-gray-100 text-gray-600 hover:bg-gray-200 text-sm font-medium transition-colors" title="طباعة">
                                    <i class="fas fa-print mr-1"></i> طباعة
                                </a>
                                @endcan
                            </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>

            <!-- PAGINATION -->
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                <div class="flex justify-center">
                    {{ $documents->appends(request()->query())->links() }}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
