@extends('layouts.app')

@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">
                    <div>
                        <h1 class="text-3xl font-bold text-gray-900 mb-2">تقرير التحويلات بين المخازن</h1>
                        <p class="text-gray-600">عرض جميع التحويلات بين المخازن المختلفة</p>
                    </div>
                    <a href="{{ route('inventory.reports.index') }}" class="inline-flex items-center px-6 py-3 bg-gray-600 hover:bg-gray-700 text-white font-semibold rounded-lg transition-all duration-200 shadow-lg">
                        <i class="fas fa-arrow-right ml-2"></i>
                        العودة للتقارير
                    </a>
                </div>
            </div>

            <!-- ملخص الإحصائيات -->
            @if($transfers->count() > 0)
                <div class="grid grid-cols-1 md:grid-cols-3 gap-4 mb-6">
                    <!-- إجمالي التحويلات -->
                    <div class="bg-white rounded-lg shadow-md border-l-4 border-blue-500 p-4">
                        <p class="text-gray-600 text-sm font-medium">إجمالي التحويلات</p>
                        <p class="text-2xl font-bold text-blue-600 mt-1">{{ $statistics['total_transfers'] ?? 0 }}</p>
                    </div>

                    <!-- إجمالي الكمية المحولة -->
                    <div class="bg-white rounded-lg shadow-md border-l-4 border-green-500 p-4">
                        <p class="text-gray-600 text-sm font-medium">إجمالي الكمية</p>
                        <p class="text-2xl font-bold text-green-600 mt-1">{{ number_format($statistics['total_quantity'] ?? 0, 2) }}</p>
                    </div>

                    <!-- إجمالي التكلفة -->
                    <div class="bg-white rounded-lg shadow-md border-l-4 border-purple-500 p-4">
                        <p class="text-gray-600 text-sm font-medium">إجمالي التكلفة</p>
                        <p class="text-2xl font-bold text-purple-600 mt-1">{{ number_format($statistics['total_cost'] ?? 0, 2) }} ريال</p>
                    </div>
                </div>
            @endif

            <!-- الفلاتر -->
            <div class="bg-white rounded-lg shadow-md p-6 mb-6 border border-gray-200">
                <h2 class="text-lg font-semibold text-gray-900 mb-4">الفلاتر</h2>
                <form action="{{ route('inventory.reports.transfers') }}" method="GET" class="grid grid-cols-1 md:grid-cols-5 gap-4">
                    <!-- المخزن المصدر -->
                    <div>
                        <label for="warehouse_id" class="block text-sm font-medium text-gray-700 mb-2">المخزن</label>
                        <select id="warehouse_id" name="warehouse_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">جميع المخازن</option>
                            @foreach($warehouses as $warehouse)
                                <option value="{{ $warehouse->id }}" {{ $filters['warehouse_id'] == $warehouse->id ? 'selected' : '' }}>
                                    {{ $warehouse->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <!-- الصنف -->
                    <div>
                        <label for="item_id" class="block text-sm font-medium text-gray-700 mb-2">الصنف</label>
                        <select id="item_id" name="item_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                            <option value="">جميع الأصناف</option>
                            @foreach($items as $item)
                                <option value="{{ $item->id }}" {{ $filters['item_id'] == $item->id ? 'selected' : '' }}>
                                    {{ $item->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <!-- من تاريخ -->
                    <div>
                        <label for="date_from" class="block text-sm font-medium text-gray-700 mb-2">من تاريخ</label>
                        <input type="date" id="date_from" name="date_from" value="{{ $filters['date_from'] ?? '' }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>

                    <!-- إلى تاريخ -->
                    <div>
                        <label for="date_to" class="block text-sm font-medium text-gray-700 mb-2">إلى تاريخ</label>
                        <input type="date" id="date_to" name="date_to" value="{{ $filters['date_to'] ?? '' }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                    </div>

                    <!-- أزرار الإجراء -->
                    <div class="flex items-end gap-2">
                        <button type="submit" class="flex-1 px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg transition-all duration-200">
                            <i class="fas fa-search ml-2"></i>تطبيق
                        </button>
                        <a href="{{ route('inventory.reports.transfers') }}" class="flex-1 px-4 py-2 bg-gray-300 hover:bg-gray-400 text-gray-700 font-semibold rounded-lg text-center transition-all duration-200">
                            إعادة تعيين
                        </a>
                    </div>
                </form>
            </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">التاريخ</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>
                                <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>
                                <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>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            @forelse($transfers as $key => $transfer)
                                <tr class="hover:bg-blue-50 transition-colors duration-150">
                                    <td class="px-6 py-4 text-gray-600 font-medium">{{ $key + 1 }}</td>
                                    <td class="px-6 py-4 text-gray-900 whitespace-nowrap">
                                        {{ \Carbon\Carbon::parse($transfer->document_date ?? now())->format('Y-m-d') }}
                                    </td>
                                    <td class="px-6 py-4 text-gray-900">{{ $transfer->item_name ?? 'N/A' }}</td>
                                    <td class="px-6 py-4 text-center font-semibold">{{ number_format($transfer->quantity ?? 0, 2) }}</td>
                                    <td class="px-6 py-4 text-gray-600 whitespace-nowrap">{{ $transfer->unit_name ?? 'N/A' }}</td>
                                    <td class="px-6 py-4 text-gray-900">
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-800">
                                            {{ $transfer->from_warehouse ?? 'N/A' }}
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 text-gray-900">
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-800">
                                            {{ $transfer->to_warehouse ?? 'N/A' }}
                                        </span>
                                    </td>
                                    <td class="px-6 py-4 text-center font-semibold text-gray-900">
                                        {{ number_format($transfer->total_cost ?? 0, 2) }} ريال
                                    </td>
                                    <td class="px-6 py-4 text-gray-600 whitespace-nowrap">{{ $transfer->document_number ?? 'N/A' }}</td>
                                    <td class="px-6 py-4 text-gray-600 text-sm">{{ $transfer->notes ?? '-' }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="10" 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>
            </div>
        </div>
    </div>
@endsection
