@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">
            <div class="flex items-center justify-between">
                <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="window.print()" 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 ml-2"></i> طباعة
                    </button>
                </div>
            </div>
        </div>

        <!-- الإحصائيات -->
        <div class="grid grid-cols-1 md:grid-cols-4 gap-4 mb-6">
            <div class="bg-white rounded-xl 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">{{ $statistics['total_items'] }}</p>
            </div>

            <div class="bg-white rounded-xl 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">{{ $statistics['compliant_items'] }}</p>
            </div>

            <div class="bg-white rounded-xl 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">{{ $statistics['non_compliant_items'] }}</p>
            </div>

            <div class="bg-white rounded-xl 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">{{ number_format($statistics['avg_variance'] ?? 0, 2) }}</p>
            </div>
        </div>

        <!-- نموذج التصفية -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-6 border border-gray-200 no-print">
            <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.variance-analysis') }}" 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="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>

                <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 ml-2"></i> بحث
                    </button>
                    <a href="{{ route('inventory.reports.variance-analysis') }}" 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 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-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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($varianceData as $row)
                            <tr class="hover:bg-blue-50 transition-colors duration-150">
                                <td class="px-6 py-4 text-gray-600 text-sm whitespace-nowrap">{{ $row->count_date }}</td>
                                <td class="px-6 py-4 font-medium text-blue-600">{{ $row->document_number }}</td>
                                <td class="px-6 py-4 text-sm text-gray-600">{{ $row->warehouse_name }}</td>
                                <td class="px-6 py-4">
                                    <div class="font-semibold text-gray-900">{{ $row->item_name }}</div>
                                    <div class="text-xs text-gray-500">{{ $row->item_code }}</div>
                                </td>
                                <td class="px-6 py-4 text-center text-gray-600">{{ number_format($row->system_quantity, 2) }}</td>
                                <td class="px-6 py-4 text-center text-gray-600">{{ number_format($row->actual_quantity, 2) }}</td>
                                <td class="px-6 py-4 text-center font-bold">
                                    <span class="px-3 py-1 rounded-lg {{ $row->variance == 0 ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                        {{ number_format($row->variance, 2) }}
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-center font-semibold text-purple-600">
                                    {{ number_format($row->total_difference_cost, 2) }}
                                </td>
                                <td class="px-6 py-4">
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium {{ $row->variance_status == 'موافق' ? 'bg-green-100 text-green-700' : 'bg-red-100 text-red-700' }}">
                                        {{ $row->variance_status }}
                                    </span>
                                </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>
                </table>
            </div>
        </div>

    </div>
</div>

<style>
    @media print {
        .no-print {
            display: none !important;
        }
    }
</style>
@endsection
