@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 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.stock-balances') }}" class="space-y-4">
                <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-4">
                    
                    <!-- المخزن -->
                    <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>
                        <label for="category_id" class="block text-sm font-medium text-gray-700 mb-1">الفئة</label>
                        <select id="category_id" name="category_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($categories as $category)
                                <option value="{{ $category->id }}" {{ $filters['category_id'] == $category->id ? 'selected' : '' }}>
                                    {{ $category->name }}
                                </option>
                            @endforeach
                        </select>
                    </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-200">
                        <i class="fas fa-search mr-2"></i> بحث
                    </button>
                    <a href="{{ route('inventory.reports.stock-balances') }}" class="inline-flex items-center px-6 py-2 bg-gray-400 hover:bg-gray-500 text-white font-semibold rounded-lg transition-all duration-200">
                        <i class="fas fa-redo mr-2"></i> إعادة تعيين
                    </a>
                </div>
            </form>
        </div>

        <!-- ملخص الأرصدة -->
        @if (count($stockBalances) > 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">{{ collect($stockBalances)->groupBy('name')->count() }}</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(collect($stockBalances)->sum('current_quantity'), 2) }}</p>
                </div>

                <div class="bg-white rounded-lg shadow-md border-l-4 border-orange-500 p-4">
                    <p class="text-gray-600 text-sm font-medium">متوسط الكمية</p>
                    <p class="text-2xl font-bold text-orange-600 mt-1">{{ number_format(collect($stockBalances)->avg('current_quantity') ?? 0, 2) }}</p>
                </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-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">
                        @forelse($stockBalances as $item)
                            @php
                                $balance = floatval($item->current_quantity);
                            @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 font-semibold text-gray-900">{{ $item->name ?? '-' }}</td>
                                <td class="px-6 py-4 text-sm text-gray-600">{{ $item->warehouse_name ?? '-' }}</td>
                                <td class="px-6 py-4 text-center text-gray-600">{{ number_format($balance, 2) }}</td>
                                <td class="px-6 py-4 text-center font-bold">
                                    <span class="px-3 py-1 rounded-lg {{ $balance >= 0 ? 'bg-blue-100 text-blue-700' : 'bg-red-100 text-red-700' }}">
                                        {{ number_format($balance, 2) }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-sm text-gray-600">{{ $item->primary_unit_name ?? '-' }}</td>
                                <td class="px-6 py-4 text-center">
                                    @if($balance > 0)
                                        <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-check ml-1"></i>متوفر
                                        </span>
                                    @elseif($balance == 0)
                                        <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                            <i class="fas fa-minus ml-1"></i>فارغ
                                        </span>
                                    @else
                                        <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-exclamation ml-1"></i>سالب
                                        </span>
                                    @endif
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="7" 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
