@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">
                <h1 class="text-3xl font-bold text-gray-900 mb-2">تقرير حركة الأصناف</h1>
                <p class="text-gray-600">حركة الأصناف (وارد - مصرف - تحويل)</p>
            </div>

            <!-- فلاتر البحث -->
            <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-6">
                <form method="GET" action="{{ route('inventory.reports.item-movements') }}" class="p-6">
                    <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
                        <!-- المخزن (إجباري) -->
                        <div>
                            <label class="block text-sm font-semibold text-gray-700 mb-2">
                                المخزن <span class="text-red-500">*</span>
                            </label>
                            <select name="warehouse_id" required
                                class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent">
                                <option value="">-- اختر المخزن --</option>
                                @foreach ($warehouses as $warehouse)
                                    <option value="{{ $warehouse->id }}"
                                        {{ $requestFilters['warehouse_id'] == $warehouse->id ? 'selected' : '' }}>
                                        {{ $warehouse->name }}
                                    </option>
                                @endforeach
                            </select>
                        </div>

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

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

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

                    <div class="mt-6 flex gap-3">
                        <button type="submit"
                            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-search ml-2"></i>بحث
                        </button>
                        <a href="{{ route('inventory.reports.item-movements') }}"
                            class="inline-flex items-center px-6 py-3 bg-gray-300 text-gray-700 font-semibold rounded-lg hover:bg-gray-400 transition-all duration-200">
                            <i class="fas fa-redo ml-2"></i>إعادة تعيين
                        </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-center 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-center 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-center 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>
                            </tr>
                        </thead>
                        <tbody class="divide-y divide-gray-200">
                            @forelse($movements as $movement)
                                @php
                                    $isInbound =
                                        $movement['document_type'] === 'receipt' ||
                                        strpos($movement['document_type'], 'استلام') !== false;
                                    $isOutbound =
                                        $movement['document_type'] === 'issue' ||
                                        strpos($movement['document_type'], 'صرف') !== false;
                                    $isTransfer =
                                        $movement['document_type'] === 'transfer' ||
                                        strpos($movement['document_type'], 'تحويل') !== false;
                                @endphp
                                <tr class="hover:bg-blue-50 transition-colors duration-150">
                                    <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>

                                    <td class="px-6 py-4 text-sm text-gray-900">
                                        {{ isset($movement['date']) ? \Carbon\Carbon::parse($movement['date'])->format('Y-m-d') : '-' }}
                                    </td>

                                    <td class="px-6 py-4 font-semibold text-gray-900">{{ $movement['item_name'] ?? '-' }}
                                    </td>

                                    <!-- الكمية الأصلية -->
                                    <td class="px-6 py-4 text-center font-bold text-blue-600">
                                        {{ number_format($movement['quantity'] ?? 0, 2) }}</td>

                                    <!-- الوحدة -->
                                    <td class="px-6 py-4 text-center text-sm font-medium text-gray-700">
                                        {{ $movement['unit'] ?? '-' }}
                                    </td>

                                    <!-- الكمية المحولة للوحدة الأساسية -->
                                    <td class="px-6 py-4 text-center font-semibold text-purple-600">
                                        {{ number_format($movement['converted_quantity'] ?? 0, 2) }}
                                        <span class="text-xs text-gray-500 block">{{ $movement['primary_unit_name'] ?? '-' }}</span>
                                    </td>

                                    <!-- نوع الحركة (وارد/مصرف/تحويل) -->
                                    <td class="px-6 py-4 text-center">
                                        @if ($isInbound)
                                            <span
                                                class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                                <i class="fas fa-arrow-down ml-1"></i>وارد
                                            </span>
                                        @elseif($isOutbound)
                                            <span
                                                class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                                <i class="fas fa-arrow-up ml-1"></i>مصرف
                                            </span>
                                        @else
                                            <span
                                                class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                                <i class="fas fa-arrows-alt-h ml-1"></i>تحويل
                                            </span>
                                        @endif
                                    </td>

                                    <!-- رقم المستند -->
                                    <td class="px-6 py-4 text-center font-semibold text-gray-900">
                                        {{ $movement['document_number'] ?? '-' }}
                                    </td>

                                    <!-- نوع المستند (source type) -->
                                    <td class="px-6 py-4 text-center">
                                        @if ($movement['document_type'] === 'receipt' || strpos($movement['document_type'], 'استلام') !== false)
                                            <span
                                                class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-green-50 text-green-700 border border-green-200">
                                                إذن استلام
                                            </span>
                                        @elseif ($movement['document_type'] === 'issue' || strpos($movement['document_type'], 'صرف') !== false)
                                            <span
                                                class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-red-50 text-red-700 border border-red-200">
                                                إذن صرف
                                            </span>
                                        @elseif ($movement['document_type'] === 'transfer' || strpos($movement['document_type'], 'تحويل') !== false)
                                            <span
                                                class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-blue-50 text-blue-700 border border-blue-200">
                                                أذن تحويل
                                            </span>
                                        @else
                                            <span
                                                class="inline-flex items-center px-2 py-1 rounded text-xs font-medium bg-gray-50 text-gray-700 border border-gray-200">
                                                {{ $movement['document_type'] ?? '-' }}
                                            </span>
                                        @endif
                                    </td>

                                    <!-- الرصيد الجاري -->
                                    <td class="px-6 py-4 text-center font-bold">
                                        @if ($movement['balance'] >= 0)
                                            <span class="text-green-600">{{ number_format($movement['balance'], 2) }}</span>
                                        @else
                                            <span class="text-red-600">{{ number_format($movement['balance'], 2) }}</span>
                                        @endif
                                    </td>

                                    <td class="px-6 py-4 text-xs text-gray-600">
                                        {{ Str::limit($movement['notes'] ?? '', 20) }}</td>
                                </tr>
                            @empty
                                <tr>
                                    <td colspan="11" 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
