@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">
        <!-- رأس الصفحة -->
        <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>
                @can('create', App\Models\InventoryDocument::class)
                <a href="{{ route('inventory.raw-materials-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>
                @endcan
            </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="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 }}</div>
                            </td>
                            <td class="px-6 py-4 text-sm text-gray-600">{{ $issue->document_date->format('Y-m-d') }}</td>
                            <td class="px-6 py-4 text-sm">{{ $issue->warehouse->name ?? '-' }}</td>
                            <td class="px-6 py-4 text-sm">
                                @if($issue->recipient instanceof \App\Models\ProductionSiteOrLine)
                                    {{ $issue->recipient->name ?? '-' }}
                                @else
                                    -
                                @endif
                            </td>
                            <td class="px-6 py-4 text-sm font-medium">{{ number_format($issue->total_quantity, 2) }}</td>
                            <td class="px-6 py-4 text-sm font-medium">{{ number_format($issue->total_cost, 2) }}</td>
                            <td class="px-6 py-4">
                                @if($issue->status === 'draft')
                                <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>
                                @elseif($issue->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>
                                @endif
                            </td>
                            <td class="px-6 py-4 whitespace-nowrap text-center space-x-1">
                                @can('view', $issue)
                                <a href="{{ route('inventory.raw-materials-issues.show', $issue) }}" 
                                   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> عرض
                                </a>
                                @endcan

                                @if($issue->status === 'draft')
                                @can('update', $issue)
                                <a href="{{ route('inventory.raw-materials-issues.edit', $issue) }}" 
                                   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 mr-1"></i> تعديل
                                </a>
                                @endcan

                                @can('delete', $issue)
                                <form action="{{ route('inventory.raw-materials-issues.destroy', $issue) }}" 
                                      method="POST" class="inline" onsubmit="return confirm('هل أنت متأكد من حذف هذه الأذن؟')">
                                    @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="حذف">
                                        <i class="fas fa-trash mr-1"></i> حذف
                                    </button>
                                </form>
                                @endcan

                                @can('approve', $issue)
                                @if($issue->status === 'draft')
                                <form action="{{ route('inventory.raw-materials-issues.approve', $issue) }}" 
                                      method="POST" class="inline" onsubmit="return confirm('هل تريد اعتماد هذه الأذن؟')">
                                    @csrf
                                    <button type="submit" 
                                            class="inline-flex items-center px-3 py-2 rounded-lg bg-green-100 text-green-600 hover:bg-green-200 transition-all duration-150 text-sm font-medium whitespace-nowrap" 
                                            title="اعتماد">
                                        <i class="fas fa-check ml-1"></i> اعتماد
                                    </button>
                                </form>
                                @endif
                                @endcan
                                @endif
                            </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->hasPages())
            <div class="px-6 py-4 bg-gray-50 border-t border-gray-200">
                <div class="flex justify-center">
                    {{ $issues->links() }}
                </div>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
