@extends('layouts.app')

@section('title', 'تقرير حركة المخزون الشاملة')

@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">تقرير حركة المخزون الشاملة</h1>
                    <p class="text-gray-600">جميع عمليات الدخول والخروج من المخزن</p>
                </div>
                <div class="flex gap-2">
                    <button onclick="printReport()" class="inline-flex items-center 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-print mr-2"></i> طباعة
                    </button>
                    <button onclick="exportReport('pdf')" class="inline-flex items-center px-4 py-2 bg-red-600 hover:bg-red-700 text-white font-semibold rounded-lg transition-all duration-200">
                        <i class="fas fa-file-pdf mr-2"></i> PDF
                    </button>
                </div>
            </div>
        </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-xl shadow-lg p-6 mb-6 border border-gray-200">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">
                <i class="fas fa-filter ml-2 text-blue-600"></i>تصفية البيانات
            </h3>

            <form method="GET" action="{{ route('inventory.reports.inventory-movements') }}" class="space-y-4">
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                    
                    <!-- نوع المستند -->
                    <div>
                        <label for="document_type" class="block text-sm font-medium text-gray-700 mb-1">نوع العملية</label>
                        <select id="document_type" name="document_type" 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>
                            <option value="receipt" {{ $filters['document_type'] == 'receipt' ? 'selected' : '' }}>
                                <i class="fas fa-arrow-down"></i> استقبال (دخول)
                            </option>
                            <option value="issue" {{ $filters['document_type'] == 'issue' ? 'selected' : '' }}>
                                <i class="fas fa-arrow-up"></i> صرف (خروج)
                            </option>
                        </select>
                    </div>

                    <!-- المخزن -->
                    <div>
                        <label for="warehouse_id" class="block text-sm font-medium text-gray-700 mb-1">المخزن</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 }}" {{ $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-1">الصنف</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 }}" {{ $filters['item_id'] == $item->id ? 'selected' : '' }}>
                                    {{ $item->name }}
                                </option>
                            @endforeach
                        </select>
                    </div>

                    <div class="flex items-end gap-2">
                        <button type="submit" class="flex-1 inline-flex items-center justify-center px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg transition-all duration-200">
                            <i class="fas fa-search mr-2"></i> بحث
                        </button>
                    </div>
                </div>

                <!-- التصفية بالتاريخ -->
                <div class="grid grid-cols-1 md:grid-cols-2 gap-4">
                    <div>
                        <label for="date_from" class="block text-sm font-medium text-gray-700 mb-1">من التاريخ</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: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-1">إلى التاريخ</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:ring-2 focus:ring-blue-500 focus:border-transparent">
                    </div>
                </div>
            </form>
        </div>

        <!-- الإحصائيات السريعة -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-blue-600">
                <p class="text-gray-600 text-sm">عدد الحركات</p>
                <p class="text-2xl font-bold text-blue-600">{{ $statistics['total_movements'] }}</p>
            </div>

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

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

            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-orange-600">
                <p class="text-gray-600 text-sm">متوسط التكلفة</p>
                <p class="text-2xl font-bold text-orange-600">
                    {{ $statistics['total_movements'] > 0 ? number_format($statistics['total_cost'] / $statistics['total_movements'], 2) : '0.00' }}
                </p>
            </div>
        </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-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($movements as $movement)
                        <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 text-sm">{{ $movement->document_date ? \Carbon\Carbon::parse($movement->document_date)->format('Y-m-d') : '-' }}</td>
                            <td class="px-6 py-4">
                                @if($movement->document_type == 'receipt')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-green-100 text-green-700">
                                        <i class="fas fa-arrow-down ml-1"></i> استقبال
                                    </span>
                                @elseif($movement->document_type == 'issue')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-red-100 text-red-700">
                                        <i class="fas fa-arrow-up ml-1"></i> صرف
                                    </span>
                                @elseif($movement->document_type == 'transfer')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-blue-100 text-blue-700">
                                        <i class="fas fa-exchange-alt ml-1"></i> تحويل
                                    </span>
                                @else
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold bg-gray-100 text-gray-700">
                                        <i class="fas fa-info-circle ml-1"></i> أخرى
                                    </span>
                                @endif
                            </td>
                            <td class="px-6 py-4">
                                <span class="inline-block px-2 py-1 text-xs font-semibold text-blue-600 bg-blue-100 rounded">
                                    {{ $movement->document_number }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-sm">{{ $movement->warehouse_name }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $movement->item_name }}</div>
                                <div class="text-xs text-gray-500">{{ $movement->item_code }}</div>
                            </td>
                            <td class="px-6 py-4 text-center">
                                <span class="inline-block px-3 py-1 text-sm font-semibold rounded-lg 
                                    @if($movement->document_type == 'receipt')
                                        bg-green-100 text-green-700
                                    @elseif($movement->document_type == 'issue')
                                        bg-red-100 text-red-700
                                    @elseif($movement->document_type == 'transfer')
                                        bg-blue-100 text-blue-700
                                    @else
                                        bg-gray-100 text-gray-700
                                    @endif">
                                    {{ number_format($movement->quantity, 2) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-right text-sm">{{ number_format($movement->unit_price ?? 0, 2) }}</td>
                            <td class="px-6 py-4 text-right font-semibold text-gray-900">
                                {{ number_format($movement->total_cost ?? 0, 2) }} ريال
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="9" 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>
                    <tfoot class="bg-gray-50 border-t-2 border-gray-300">
                        <tr>
                            <td colspan="6" class="px-6 py-4 font-bold text-gray-900">الإجمالي</td>
                            <td class="px-6 py-4 text-center font-bold text-blue-600">
                                {{ number_format($statistics['total_quantity'], 2) }}
                            </td>
                            <td></td>
                            <td class="px-6 py-4 text-right font-bold text-purple-600">
                                {{ number_format($statistics['total_cost'], 2) }} ريال
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>

    </div>
</div>

<script>
function printReport() {
    window.print();
}

function exportReport(format) {
    const params = new URLSearchParams({
        report: 'movements',
        format: format,
        document_type: document.getElementById('document_type').value,
        warehouse_id: document.getElementById('warehouse_id').value,
        item_id: document.getElementById('item_id').value,
        date_from: document.getElementById('date_from').value,
        date_to: document.getElementById('date_to').value,
    });
    
    window.location.href = '/inventory/reports/export?' + params.toString();
}
</script>

@endsection
