@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 mb-6">
                <div>
                    <h1 class="text-3xl font-bold text-gray-900 mb-2">تقرير تحليل المنتجات</h1>
                    <p class="text-gray-600">تحليل تفصيلي لكل المنتجات المباعة</p>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 p-6 mb-6">
            <h2 class="text-xl font-bold text-gray-900 mb-6">فلاتر البحث</h2>
            
            <form method="GET" class="grid grid-cols-1 md:grid-cols-4 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">من التاريخ</label>
                    <input type="date" name="from_date" value="{{ $fromDate->format('Y-m-d') }}" 
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">إلى التاريخ</label>
                    <input type="date" name="to_date" value="{{ $toDate->format('Y-m-d') }}" 
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </div>
                <div class="flex items-end">
                    <button type="submit" class="w-full bg-blue-600 hover:bg-blue-700 text-white font-semibold py-2 px-4 rounded-lg transition-colors">
                        <i class="fas fa-search ml-1"></i> بحث
                    </button>
                </div>
            </form>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-4 gap-6 mb-6">
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-blue-500">
                <p class="text-gray-600 text-sm">عدد المنتجات</p>
                <p class="text-3xl font-bold text-blue-600">{{ number_format($analysis['product_count']) }}</p>
            </div>

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

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-purple-500">
                <p class="text-gray-600 text-sm">إجمالي القيمة</p>
                <p class="text-2xl font-bold text-purple-600">{{ number_format($analysis['total_value'], 2) }}</p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-amber-500">
                <p class="text-gray-600 text-sm">متوسط قيمة المنتج</p>
                <p class="text-2xl font-bold text-amber-600">{{ number_format(($analysis['total_value'] / max($analysis['product_count'], 1)), 2) }}</p>
            </div>
        </div>

        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-6">
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <h3 class="text-lg font-bold text-gray-900 mb-4">توزيع المنتجات</h3>
                <canvas id="productChart" height="300"></canvas>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <h3 class="text-lg font-bold text-gray-900 mb-4">أفضل 5 منتجات</h3>
                <div class="space-y-3">
                    @forelse(collect($analysis['products'])->take(5) as $product)
                    <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                        <div>
                            <p class="font-semibold text-gray-900">{{ $product['description'] }}</p>
                            <p class="text-xs text-gray-500">{{ number_format($product['percentage'], 2) }}% من الإجمالي</p>
                        </div>
                        <p class="font-bold text-blue-600">{{ number_format($product['total_value'], 2) }}</p>
                    </div>
                    @empty
                    <p class="text-gray-500 text-center py-4">لا توجد بيانات</p>
                    @endforelse
                </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-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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($analysis['products'] as $product)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $product['description'] }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-600">{{ number_format($product['total_quantity']) }}</td>
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ number_format($product['total_value'], 2) }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ number_format($product['invoice_count']) }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ number_format($product['average_quantity_per_invoice'], 2) }}</td>
                            <td class="px-6 py-4 text-center">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                    {{ $product['percentage'] }}%
                                </span>
                            </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>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.min.js"></script>
<script>
    document.addEventListener('DOMContentLoaded', function() {
        const ctx = document.getElementById('productChart').getContext('2d');
        const chartData = {!! json_encode($chartData) !!};
        
        new Chart(ctx, {
            type: 'doughnut',
            data: chartData,
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        position: 'bottom',
                    },
                    title: {
                        display: true,
                        text: 'توزيع قيمة المنتجات'
                    }
                }
            }
        });
    });
</script>
@endsection
