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

        <!-- رسائل التنبيه -->
        @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

        <!-- فلاتر البحث -->
        <div class="bg-white rounded-lg shadow-lg p-6 mb-6 border border-gray-200">
            <form action="{{ route('inventory.reports.item-movements') }}" method="GET" class="space-y-4">
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                    <!-- الصنف -->
                    <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="block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring focus:ring-blue-500 focus:ring-opacity-50">
                            <option value="">جميع الأصناف</option>
                            @foreach($items as $item)
                                <option value="{{ $item->id }}" {{ request('item_id') == $item->id ? 'selected' : '' }}>
                                    {{ $item->name }} ({{ $item->code }})
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <!-- المخزن -->
                    <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="block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring focus:ring-blue-500 focus:ring-opacity-50">
                            <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="from_date" class="block text-sm font-medium text-gray-700 mb-2">من تاريخ</label>
                        <input type="date" id="from_date" name="from_date" value="{{ request('from_date') }}" class="block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring focus:ring-blue-500 focus:ring-opacity-50">
                    </div>

                    <!-- إلى تاريخ -->
                    <div>
                        <label for="to_date" class="block text-sm font-medium text-gray-700 mb-2">إلى تاريخ</label>
                        <input type="date" id="to_date" name="to_date" value="{{ request('to_date') }}" class="block w-full rounded-lg border-gray-300 shadow-sm focus:border-blue-500 focus:ring focus:ring-blue-500 focus:ring-opacity-50">
                    </div>
                </div>

                <!-- أزرار البحث -->
                <div class="flex gap-3">
                    <button type="submit" class="inline-flex items-center px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg transition-all duration-150 shadow-md hover:shadow-lg">
                        <i class="fas fa-search ml-2"></i>
                        بحث
                    </button>
                    <a href="{{ route('inventory.reports.item-movements') }}" class="inline-flex items-center px-6 py-2 bg-gray-300 hover:bg-gray-400 text-gray-700 font-semibold rounded-lg transition-all duration-150">
                        <i class="fas fa-redo ml-2"></i>
                        إعادة تعيين
                    </a>
                </div>
            </form>
        </div>

        <!-- ملخص الحركات -->
        @if($movements->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-green-500 p-4">
                <div class="flex items-center">
                    <div class="flex-1">
                        <p class="text-gray-600 text-sm font-medium">إجمالي الوارد</p>
                        <p class="text-2xl font-bold text-green-600 mt-1">{{ number_format($movements->where('effect', 'receipt')->sum('quantity'), 2) }}</p>
                    </div>
                    <div class="text-3xl text-green-500 opacity-20">
                        <i class="fas fa-arrow-down"></i>
                    </div>
                </div>
            </div>

            <!-- إجمالي المنصرف -->
            <div class="bg-white rounded-lg shadow-md border-l-4 border-red-500 p-4">
                <div class="flex items-center">
                    <div class="flex-1">
                        <p class="text-gray-600 text-sm font-medium">إجمالي المنصرف</p>
                        <p class="text-2xl font-bold text-red-600 mt-1">{{ number_format($movements->where('effect', 'issue')->sum('quantity'), 2) }}</p>
                    </div>
                    <div class="text-3xl text-red-500 opacity-20">
                        <i class="fas fa-arrow-up"></i>
                    </div>
                </div>
            </div>

            <!-- الرصيد الحالي -->
            <div class="bg-white rounded-lg shadow-md border-l-4 border-blue-500 p-4">
                <div class="flex items-center">
                    <div class="flex-1">
                        <p class="text-gray-600 text-sm font-medium">الرصيد الحالي</p>
                        <p class="text-2xl font-bold text-blue-600 mt-1">
                            @php
                                $inTotal = $movements->where('effect', 'receipt')->sum('quantity') ?? 0;
                                $outTotal = $movements->where('effect', 'issue')->sum('quantity') ?? 0;
                                $transferTotal = 0;
                                
                                // حساب تأثير التحويلات بناءً على المخزن المختار
                                $warehouseFilter = request('warehouse_id');
                                if($warehouseFilter) {
                                    foreach($movements->where('effect', 'transfer') as $transfer) {
                                        if($transfer->to_warehouse_id == $warehouseFilter) {
                                            $transferTotal += $transfer->quantity;
                                        } elseif($transfer->from_warehouse_id == $warehouseFilter) {
                                            $transferTotal -= $transfer->quantity;
                                        }
                                    }
                                }
                                
                                $finalBalance = $inTotal - $outTotal + $transferTotal;
                            @endphp
                            {{ number_format($finalBalance, 2) }}
                        </p>
                    </div>
                    <div class="text-3xl text-blue-500 opacity-20">
                        <i class="fas fa-boxes"></i>
                    </div>
                </div>
            </div>
        </div>
        @endif

        <!-- جدول الحركات التفصيلية -->
        <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-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>
                            <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
                                $runningBalance = 0;
                                $isInbound = $movement->effect === 'receipt';
                                $isOutbound = $movement->effect === 'issue';
                                $isTransfer = $movement->effect === 'transfer';
                                $warehouseFilter = request('warehouse_id');
                                
                                // حساب الرصيد التراكمي
                                foreach($movements as $mov) {
                                    if($mov->id === $movement->id) break;
                                    if($mov->effect === 'receipt') {
                                        $runningBalance += $mov->quantity;
                                    } elseif($mov->effect === 'issue') {
                                        $runningBalance -= $mov->quantity;
                                    } elseif($mov->effect === 'transfer' && $warehouseFilter) {
                                        // للتحويلات، نحتاج لمعرفة اتجاه التحويل بناءً على المخزن المختار
                                        if($mov->to_warehouse_id == $warehouseFilter) {
                                            $runningBalance += $mov->quantity;
                                        } elseif($mov->from_warehouse_id == $warehouseFilter) {
                                            $runningBalance -= $mov->quantity;
                                        }
                                    }
                                }
                                
                                // إضافة تأثير الحركة الحالية
                                if($isInbound) {
                                    $runningBalance += $movement->quantity;
                                } elseif($isOutbound) {
                                    $runningBalance -= $movement->quantity;
                                } elseif($isTransfer && $warehouseFilter) {
                                    // حساب التأثير للتحويل الحالي
                                    if($movement->to_warehouse_id == $warehouseFilter) {
                                        $runningBalance += $movement->quantity;
                                    } elseif($movement->from_warehouse_id == $warehouseFilter) {
                                        $runningBalance -= $movement->quantity;
                                    }
                                }
                            @endphp
                            <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">
                                    <span class="text-sm font-medium text-gray-900">
                                        {{ $movement->document_date ? \Carbon\Carbon::parse($movement->document_date)->format('Y-m-d') : '-' }}
                                    </span>
                                </td>

                                <!-- نوع الحركة -->
                                <td class="px-6 py-4">
                                    @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">
                                    <div class="font-semibold text-gray-900">{{ $movement->item_name ?? '-' }}</div>
                                    <div class="text-sm text-gray-600">{{ $movement->item_code ?? '' }}</div>
                                </td>

                                <!-- الكمية الواردة -->
                                <td class="px-6 py-4 text-center">
                                    @if($isInbound)
                                        <span class="font-semibold text-green-600">{{ number_format($movement->quantity, 2) }}</span>
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </td>

                                <!-- الكمية المنصرفة -->
                                <td class="px-6 py-4 text-center">
                                    @if($isOutbound)
                                        <span class="font-semibold text-red-600">{{ number_format($movement->quantity, 2) }}</span>
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </td>

                                <!-- الكمية المحولة -->
                                <td class="px-6 py-4 text-center">
                                    @if($isTransfer)
                                        <span class="font-semibold text-blue-600">
                                            {{ $movement->from_warehouse ? $movement->from_warehouse . ' → ' : '' }}{{ $movement->to_warehouse }}
                                            <br>
                                            <span class="text-sm">{{ number_format($movement->quantity, 2) }}</span>
                                        </span>
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </td>

                                <!-- الرصيد -->
                                <td class="px-6 py-4 text-center">
                                    <span class="inline-flex items-center justify-center w-20 px-3 py-1 rounded-lg font-bold {{ $runningBalance >= 0 ? 'bg-blue-100 text-blue-700' : 'bg-red-100 text-red-700' }}">
                                        {{ number_format($runningBalance, 2) }}
                                    </span>
                                </td>

                                <!-- الوحدة -->
                                <td class="px-6 py-4">
                                    <span class="text-sm text-gray-600">{{ $movement->unit_name ?? '-' }}</span>
                                </td>

                                <!-- رقم المستند -->
                                <td class="px-6 py-4 text-right">
                                    <span class="text-sm font-medium text-gray-900">
                                        {{ $movement->document_number ?? '-' }}
                                        <br>
                                        <span class="text-xs text-gray-600">
                                            @if($isInbound)
                                                استقبال
                                            @elseif($isOutbound)
                                                صرف
                                            @else
                                                تحويل
                                            @endif
                                        </span>
                                    </span>
                                </td>

                                <!-- الملاحظات -->
                                <td class="px-6 py-4 text-right">
                                    @if($movement->notes)
                                        <span class="text-xs text-gray-600 truncate" title="{{ $movement->notes }}">
                                            {{ Str::limit($movement->notes, 25) }}
                                        </span>
                                    @else
                                        <span class="text-gray-400">-</span>
                                    @endif
                                </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
