@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-value') }}" 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>
                        <label for="valuation_method" class="block text-sm font-medium text-gray-700 mb-1">طريقة التقييم</label>
                        <select id="valuation_method" name="valuation_method" 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="fifo" {{ $filters['valuation_method'] == 'fifo' ? 'selected' : '' }}>FIFO (الداخل أولاً - الخارج أولاً)</option>
                            <option value="lifo" {{ $filters['valuation_method'] == 'lifo' ? 'selected' : '' }}>LIFO (الداخل أخيراً - الخارج أولاً)</option>
                            <option value="average" {{ $filters['valuation_method'] == 'average' ? 'selected' : '' }}>Average (المتوسط المرجح)</option>
                        </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.inventory-value') }}" 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>

        <!-- بطاقة ملخص التقييم -->
        <div class="bg-gradient-to-r from-purple-600 to-purple-700 rounded-xl shadow-lg text-white p-8 mb-6">
            <div class="grid grid-cols-1 md:grid-cols-4 gap-6">
                <div>
                    <p class="text-purple-200 text-sm font-medium mb-1">طريقة التقييم</p>
                    <p class="text-2xl font-bold capitalize">
                        @if($valuationData['summary']['valuation_method'] == 'fifo')
                            FIFO
                        @elseif($valuationData['summary']['valuation_method'] == 'lifo')
                            LIFO
                        @else
                            المتوسط المرجح
                        @endif
                    </p>
                </div>

                <div>
                    <p class="text-purple-200 text-sm font-medium mb-1">إجمالي الكمية</p>
                    <p class="text-2xl font-bold">{{ number_format($valuationData['summary']['total_quantity'], 2) }}</p>
                </div>

                <div>
                    <p class="text-purple-200 text-sm font-medium mb-1">متوسط سعر الوحدة</p>
                    <p class="text-2xl font-bold">{{ number_format($valuationData['summary']['average_unit_cost'], 2) }} ريال</p>
                </div>

                <div>
                    <p class="text-purple-200 text-sm font-medium mb-1">إجمالي القيمة</p>
                    <p class="text-3xl font-bold">{{ number_format($valuationData['summary']['total_value'], 2) }} ريال</p>
                </div>
            </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-purple-800 to-purple-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-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($valuationData['items'] as $item)
                        <tr class="hover:bg-purple-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="inline-block px-2 py-1 text-xs font-semibold text-blue-600 bg-blue-100 rounded">
                                    {{ $item->code }}
                                </span>
                            </td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $item->name }}</div>
                            </td>
                            <td class="px-6 py-4 text-center">
                                <span class="inline-block px-3 py-1 text-sm font-semibold rounded-lg bg-blue-100 text-blue-700">
                                    {{ number_format($item->current_quantity, 2) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-right text-sm">{{ number_format($item->unit_price ?? 0, 2) }}</td>
                            <td class="px-6 py-4 text-right font-semibold text-purple-600">
                                {{ number_format($item->total_value ?? 0, 2) }} ريال
                            </td>
                            <td class="px-6 py-4 text-right">
                                <div class="flex items-center justify-end gap-2">
                                    <div class="w-16 bg-gray-200 rounded-full h-2">
                                        <div class="bg-purple-600 h-2 rounded-full" style="width: {{ $valuationData['summary']['total_value'] > 0 ? (($item->total_value ?? 0) / $valuationData['summary']['total_value']) * 100 : 0 }}%"></div>
                                    </div>
                                    <span class="text-sm font-semibold text-gray-600 w-12">
                                        {{ $valuationData['summary']['total_value'] > 0 ? number_format((($item->total_value ?? 0) / $valuationData['summary']['total_value']) * 100, 1) : 0 }}%
                                    </span>
                                </div>
                            </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>
                    <tfoot class="bg-purple-50 border-t-2 border-purple-300">
                        <tr>
                            <td colspan="5" class="px-6 py-4 font-bold text-gray-900 text-left">الإجمالي</td>
                            <td class="px-6 py-4 text-right font-bold text-purple-600 text-lg">
                                {{ number_format($valuationData['summary']['total_value'], 2) }} ريال
                            </td>
                            <td class="px-6 py-4 text-right font-bold text-purple-600">100%</td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>

        <!-- ملاحظات شرح طرق التقييم -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mt-6">
            <div class="bg-blue-50 border-l-4 border-blue-500 p-4 rounded-r-lg">
                <h4 class="font-semibold text-blue-900 mb-2">FIFO</h4>
                <p class="text-blue-800 text-sm">الداخل أولاً - الخارج أولاً. تفترض أن أقدم المواد هي أول ما يخرج</p>
            </div>

            <div class="bg-yellow-50 border-l-4 border-yellow-500 p-4 rounded-r-lg">
                <h4 class="font-semibold text-yellow-900 mb-2">LIFO</h4>
                <p class="text-yellow-800 text-sm">الداخل أخيراً - الخارج أولاً. تفترض أن أحدث المواد هي أول ما يخرج</p>
            </div>

            <div class="bg-green-50 border-l-4 border-green-500 p-4 rounded-r-lg">
                <h4 class="font-semibold text-green-900 mb-2">المتوسط المرجح</h4>
                <p class="text-green-800 text-sm">حساب متوسط تكلفة الوحدة بناءً على جميع المشتريات</p>
            </div>
        </div>

    </div>
</div>

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

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

@endsection
