@extends('layouts.app')

@section('title', TranslationHelper::transFromDatabase('Warehouses.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">
        <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('Warehouses.ManageTitle', __('إدارة المخازن')) }}</h1>
                    <p class="text-gray-600">{{ TranslationHelper::transFromDatabase('Warehouses.ManageDescription', __('إدارة وتحديث معلومات المخازن ومستودعات الشركة')) }}</p>
                </div>
                @can('inventory.warehouses.create')
                <a href="{{ route('inventory.warehouses.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('Warehouses.AddNew', __('إضافة مخزن جديد')) }}</span>
                </a>
                @endcan
            </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>

        <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">{{ TranslationHelper::transFromDatabase('Warehouses.Name', __('اسم المخزن')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ TranslationHelper::transFromDatabase('Warehouses.Code', __('كود المخزن')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ TranslationHelper::transFromDatabase('Warehouses.Managers', __('مديرو المخزن')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ TranslationHelper::transFromDatabase('Warehouses.Address', __('العنوان')) }}</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm">{{ TranslationHelper::transFromDatabase('Warehouses.Status', __('الحالة')) }}</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">{{ TranslationHelper::transFromDatabase('General.Actions', __('الإجراءات')) }}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($warehouses as $warehouse)
                        <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">{{ $warehouse->name }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                <span class="inline-block px-3 py-1 bg-gray-100 text-gray-700 rounded-md text-sm font-medium">
                                    {{ $warehouse->code ?? '-' }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                <div class="flex flex-wrap gap-1">
                                    @if($warehouse->managers->count() > 0)
                                        @foreach($warehouse->managers as $manager)
                                            <span class="inline-flex items-center px-2.5 py-1 rounded-full text-xs font-medium" 
                                                style="background-color: {{ $manager->pivot->is_primary ? '#dbeafe' : '#f3f4f6' }}; color: {{ $manager->pivot->is_primary ? '#1e40af' : '#374151' }}">
                                                {{ $manager->name }}
                                                @if($manager->pivot->is_primary)
                                                    <i class="fas fa-star ml-1" style="font-size: 10px;"></i>
                                                @endif
                                            </span>
                                        @endforeach
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ Str::limit($warehouse->address ?? '-', 30) }}
                            </td>
                            <td class="px-6 py-4">
                                @if($warehouse->is_active)
                                <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('Status.Active', __('نشط')) }}
                                </span>
                                @else
                                <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('Status.Inactive', __('غير نشط')) }}
                                </span>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center justify-center gap-1">
                                    @can('inventory.warehouses.view')
                                    <a href="{{ route('inventory.warehouses.show', $warehouse->id) }}" 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="{{ TranslationHelper::transFromDatabase('General.ShowDetails', __('عرض التفاصيل')) }}">
                                        <i class="fas fa-eye mr-1"></i>
                                        <span>{{ TranslationHelper::transFromDatabase('General.Show', __('عرض')) }}</span>
                                    </a>
                                    @endcan

                                    @can('inventory.warehouses.edit')
                                    <a href="{{ route('inventory.warehouses.edit', $warehouse->id) }}" 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="{{ TranslationHelper::transFromDatabase('General.Edit', __('تعديل')) }}">
                                        <i class="fas fa-edit mr-1"></i>
                                        <span>{{ TranslationHelper::transFromDatabase('General.Edit', __('تعديل')) }}</span>
                                    </a>
                                    @endcan

                                    @can('inventory.warehouses.delete')
                                    <form action="{{ route('inventory.warehouses.destroy', $warehouse->id) }}" method="POST" class="inline">
                                        @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="{{ TranslationHelper::transFromDatabase('General.Delete', __('حذف')) }}" onclick="return confirm('{{ TranslationHelper::transFromDatabase('General.ConfirmDelete', __('هل أنت متأكد من حذف هذا المخزن؟')) }}')">
                                            <i class="fas fa-trash mr-1"></i>
                                            <span>{{ TranslationHelper::transFromDatabase('General.Delete', __('حذف')) }}</span>
                                        </button>
                                    </form>
                                    @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">{{ TranslationHelper::transFromDatabase('General.NoData', __('لا يوجد مخازن حالياً')) }}</p>
                                    <p class="text-gray-500 text-sm mt-1">{{ TranslationHelper::transFromDatabase('General.StartAdding', __('ابدأ بإضافة مخزن جديد')) }}</p>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

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