@extends('layouts.app')

@section('title', TranslationHelper::transFromDatabase('InventoryCounts.ManageTitle', __('عمليات الجرد')))

@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">{{ TranslationHelper::transFromDatabase('InventoryCounts.ManageTitle', __('عمليات الجرد')) }}</h1>
                    <p class="text-gray-600">{{ TranslationHelper::transFromDatabase('InventoryCounts.ManageDescription', __('إدارة وتتبع عمليات جرد المخزون')) }}</p>
                </div>
                <!-- ACTION BUTTON -->
                @can('inventory.counts.create')
                <a href="{{ route('inventory.counts.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>{{ TranslationHelper::transFromDatabase('InventoryCounts.AddNew', __('إنشاء جرد جديد')) }}</span>
                </a>
                @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>

        <!-- 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">{{ TranslationHelper::transFromDatabase('InventoryCounts.Number', __('رقم الجرد')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ TranslationHelper::transFromDatabase('InventoryCounts.Warehouse', __('المخزن')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ TranslationHelper::transFromDatabase('InventoryCounts.Date', __('التاريخ')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ TranslationHelper::transFromDatabase('InventoryCounts.CreatedBy', __('المنشئ')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ TranslationHelper::transFromDatabase('InventoryCounts.Status', __('الحالة')) }}</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">{{ TranslationHelper::transFromDatabase('General.Actions', __('الإجراءات')) }}</th>
                        </tr>
                    </thead>
                    <!-- TABLE BODY -->
                    <tbody class="divide-y divide-gray-200">
                        @forelse($counts as $count)
                        <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">{{ $count->count_number ?? TranslationHelper::transFromDatabase('General.NotSpecified', __('غير محدد')) }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $count->warehouse->name ?? TranslationHelper::transFromDatabase('General.NotSpecified', __('غير محدد')) }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $count->count_date->format('Y-m-d') }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $count->createdByUser->name ?? TranslationHelper::transFromDatabase('General.NotSpecified', __('غير محدد')) }}</td>
                            <td class="px-6 py-4">
                                @if($count->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> {{ TranslationHelper::transFromDatabase('InventoryCounts.Draft', __('مسودة')) }}
                                    </span>
                                @elseif($count->status == 'pending')
                                    <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> {{ TranslationHelper::transFromDatabase('InventoryCounts.UnderReview', __('قيد المراجعة')) }}
                                    </span>
                                @elseif($count->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> {{ TranslationHelper::transFromDatabase('InventoryCounts.Approved', __('معتمد')) }}
                                    </span>
                                @elseif($count->status == 'rejected')
                                    <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> {{ TranslationHelper::transFromDatabase('InventoryCounts.Rejected', __('مرفوض')) }}
                                    </span>
                                @endif
                            </td>
                            <td class="px-6 py-4 flex items-center justify-center gap-1">
                                @can('inventory.counts.view')
                                <a href="{{ route('inventory.counts.show', $count) }}" 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="{{ TranslationHelper::transFromDatabase('General.ShowDetails', __('عرض التفاصيل')) }}">
                                    <i class="fas fa-eye mr-1"></i> {{ TranslationHelper::transFromDatabase('General.Show', __('عرض')) }}
                                </a>
                                @endcan
                                
                                @if($count->status != 'approved')
                                    @can('inventory.counts.edit')
                                    <a href="{{ route('inventory.counts.edit', $count) }}" 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="{{ TranslationHelper::transFromDatabase('General.Edit', __('تعديل')) }}">
                                        <i class="fas fa-edit mr-1"></i> {{ TranslationHelper::transFromDatabase('General.Edit', __('تعديل')) }}
                                    </a>
                                    @endcan
                                    
                                    @can('inventory.counts.delete')
                                    <form action="{{ route('inventory.counts.destroy', $count) }}" method="POST" class="inline" onsubmit="return confirm('{{ TranslationHelper::transFromDatabase('InventoryCounts.ConfirmDelete', __('هل أنت متأكد من حذف هذا الجرد؟')) }}');">
                                        @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 text-sm font-medium transition-colors" title="{{ TranslationHelper::transFromDatabase('General.Delete', __('حذف')) }}">
                                            <i class="fas fa-trash mr-1"></i> {{ TranslationHelper::transFromDatabase('General.Delete', __('حذف')) }}
                                        </button>
                                    </form>
                                    @endcan
                                    
                                    @can('inventory.counts.approve')
                                    <form action="{{ route('inventory.counts.approve', $count) }}" method="POST" class="inline" onsubmit="return confirm('{{ TranslationHelper::transFromDatabase('InventoryCounts.ConfirmApprove', __('هل أنت متأكد من اعتماد هذا الجرد؟ سيتم تحديث أرصدة المخزون.')) }}');">
                                        @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 text-sm font-medium transition-colors" title="{{ TranslationHelper::transFromDatabase('General.Approve', __('اعتماد')) }}">
                                            <i class="fas fa-check mr-1"></i> {{ TranslationHelper::transFromDatabase('General.Approve', __('اعتماد')) }}
                                        </button>
                                    </form>
                                    @endcan
                                @endif
                            </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">{{ TranslationHelper::transFromDatabase('InventoryCounts.NoData', __('لا توجد عمليات جرد')) }}</p>
                                    <p class="text-gray-500 text-sm mt-1">{{ TranslationHelper::transFromDatabase('InventoryCounts.StartCreating', __('ابدأ بإنشاء جرد جديد')) }}</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">
                    {{ $counts->links() }}
                </div>
            </div>
        </div>
    </div>
</div>
@endsection