@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>

        <!-- نموذج الفلترة -->
        <form action="{{ route('inventory.reports.item-movements') }}" method="GET" 
            class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
            <div class="grid grid-cols-1 md:grid-cols-5 gap-4 mb-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:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="">كل المخازن</option>
                        @foreach ($warehouses as $warehouse)
                            <option value="{{ $warehouse->id }}" 
                                {{ request('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:ring-2 focus:ring-blue-500 focus:border-transparent">
                        <option value="">كل الأصناف</option>
                        @foreach ($items as $item)
                            <option value="{{ $item->id }}" 
                                {{ request('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="{{ request('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 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="{{ request('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 class="flex items-end gap-2">
                    <button type="submit" class="w-full inline-flex items-center justify-center px-4 py-2 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-md">
                        <i class="fas fa-search ml-2"></i>
                        بحث
                    </button>
                    <a href="{{ route('inventory.reports.item-movements') }}" class="inline-flex items-center px-4 py-2 bg-gray-200 text-gray-700 font-semibold rounded-lg hover:bg-gray-300 transition-all duration-200">
                        <i class="fas fa-redo ml-2"></i>
                        <span class="hidden md:inline">إعادة تعيين</span>
                    </a>
                </div>
            </div>
        </form>

        <!-- الإحصائيات -->
        @if(count($movements) > 0)
        <div class="grid grid-cols-1 md:grid-cols-5 gap-4 mb-6">
            <!-- إجمالي الوارد -->
            <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_receipts'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ $statistics['receipt_count'] }} حركة</p>
            </div>

            <!-- إجمالي المنصرف -->
            <div class="bg-white rounded-lg shadow-md border-l-4 border-red-500 p-4">
                <p class="text-gray-600 text-sm font-medium">إجمالي المنصرف</p>
                <p class="text-2xl font-bold text-red-600 mt-1">{{ number_format($statistics['total_issues'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ $statistics['issue_count'] }} حركة</p>
            </div>

            <!-- إجمالي المحول -->
            <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">{{ number_format($statistics['total_transfers'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ $statistics['transfer_count'] }} حركة</p>
            </div>

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

        <!-- جدول الحركات -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            @if(count($movements) > 0)
            <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-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>
                            <th class="px-6 py-4 text-right font-semibold text-sm">الملاحظات</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($movements as $movement)
                        <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">
                                @if(isset($movement['date']))
                                    {{ $movement['date'] instanceof \Carbon\Carbon ? $movement['date']->format('Y-m-d') : \Carbon\Carbon::parse($movement['date'])->format('Y-m-d') }}
                                @else
                                    -
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                @php
                                    $docType = $movement['document_type'] ?? '';
                                    $isArabic = strpos($docType, 'أذن') !== false;
                                @endphp
                                @if($isArabic && strpos($docType, 'استلام') !== false || !$isArabic && $docType === 'receipt')
                                    <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($isArabic && strpos($docType, 'صرف') !== false || !$isArabic && $docType === 'issue')
                                    <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 font-semibold text-gray-900">{{ $movement['item_name'] ?? '-' }}</td>
                            <td class="px-6 py-4 text-sm text-gray-700">
                                {{ $movement['to_warehouse'] ?? $movement['from_warehouse'] ?? '-' }}
                            </td>
                            <td class="px-6 py-4 text-center font-medium">{{ number_format($movement['quantity'] ?? 0, 2) }}</td>
                            <td class="px-6 py-4 text-center text-sm">{{ number_format($movement['unit_price'] ?? 0, 2) }}</td>
                            <td class="px-6 py-4 text-center font-semibold text-gray-900">{{ number_format($movement['total_cost'] ?? 0, 2) }}</td>
                            <td class="px-6 py-4 text-center font-semibold {{ ($movement['balance'] ?? 0) >= 0 ? 'text-green-600' : 'text-red-600' }}">
                                {{ number_format($movement['balance'] ?? 0, 2) }}
                            </td>
                            <td class="px-6 py-4 text-right text-sm">{{ $movement['document_number'] ?? '-' }}</td>
                            <td class="px-6 py-4 text-right text-sm text-gray-600">{{ $movement['notes'] ?? '-' }}</td>
                        </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
            @else
            <div 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>
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
