@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-3 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($data['total_sales'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ $data['total_customers'] }} عميل</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">نسبة تركيز أكبر 5 عملاء</p>
                <p class="text-3xl font-bold text-purple-600">{{ $data['top_5_concentration_percentage'] }}%</p>
                <p class="text-xs text-gray-500 mt-2">{{ number_format($data['top_5_sales'], 2) }} ريال</p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 
                @if($data['concentration_risk'] === 'عالي') border-red-500
                @elseif($data['concentration_risk'] === 'متوسط') border-amber-500
                @else border-green-500 @endif">
                <p class="text-gray-600 text-sm">تقييم المخاطر</p>
                <p class="text-3xl font-bold
                    @if($data['concentration_risk'] === 'عالي') text-red-600
                    @elseif($data['concentration_risk'] === 'متوسط') text-amber-600
                    @else text-green-600 @endif">
                    {{ $data['concentration_risk'] }}
                </p>
                <p class="text-xs text-gray-500 mt-2">
                    @if($data['concentration_risk'] === 'عالي') مخاطر عالية جداً
                    @elseif($data['concentration_risk'] === 'متوسط') مخاطر متوسطة
                    @else مخاطر منخفضة @endif
                </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="concentrationChart" 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">تحليل التركيز</h3>
                <div class="space-y-4">
                    <div class="flex justify-between items-center p-3 bg-gray-50 rounded-lg">
                        <span class="text-gray-600">أكبر 5 عملاء</span>
                        <span class="font-bold text-purple-600">{{ $data['top_5_concentration_percentage'] }}%</span>
                    </div>
                    <div class="flex justify-between items-center p-3 bg-gray-50 rounded-lg">
                        <span class="text-gray-600">أكبر 10 عملاء</span>
                        <span class="font-bold text-blue-600">{{ $data['top_10_concentration_percentage'] }}%</span>
                    </div>
                    <div class="flex justify-between items-center p-3 bg-gray-50 rounded-lg">
                        <span class="text-gray-600">باقي العملاء ({{ $data['total_customers'] - 10 }}+)</span>
                        <span class="font-bold text-green-600">{{ 100 - $data['top_10_concentration_percentage'] }}%</span>
                    </div>
                    <div class="mt-4 p-3 
                        @if($data['concentration_risk'] === 'عالي') bg-red-50
                        @elseif($data['concentration_risk'] === 'متوسط') bg-amber-50
                        @else bg-green-50 @endif rounded-lg">
                        <p class="text-sm font-semibold">
                            @if($data['concentration_risk'] === 'عالي')
                            ⚠️ تحذير: المبيعات مركزة جداً بدرجة عالية جداً
                            @elseif($data['concentration_risk'] === 'متوسط')
                            ⚠️ تنبيه: المبيعات مركزة بدرجة متوسطة
                            @else
                            ✓ آمن: المبيعات موزعة بشكل جيد
                            @endif
                        </p>
                    </div>
                </div>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white">
                <h3 class="text-lg font-bold">أكبر 10 عملاء</h3>
            </div>
            <div class="divide-y divide-gray-200">
                @forelse($data['top_customers'] as $index => $customer)
                <div class="p-6 hover:bg-blue-50 transition-colors">
                    <div class="flex items-center justify-between mb-3">
                        <div>
                            <span class="inline-flex items-center justify-center w-8 h-8 rounded-full bg-blue-600 text-white font-bold">
                                {{ $index + 1 }}
                            </span>
                            <p class="font-semibold text-gray-900 inline-block ml-3">{{ $customer['customer_name'] }}</p>
                        </div>
                        <p class="font-bold text-blue-600 text-lg">{{ number_format($customer['total_amount'], 2) }}</p>
                    </div>
                    
                    <div class="mt-4">
                        <div class="flex justify-between text-xs text-gray-600 mb-2">
                            <span>
                                <span class="font-semibold">{{ number_format(($customer['total_amount'] / max($data['total_sales'], 1)) * 100, 2) }}%</span>
                                من الإجمالي
                            </span>
                            <span>
                                <span class="font-semibold">{{ $customer['invoice_count'] }}</span>
                                فاتورة
                            </span>
                        </div>
                        <div class="w-full bg-gray-200 rounded-full h-2">
                            <div class="bg-gradient-to-r from-blue-600 to-blue-400 h-2 rounded-full" 
                                 style="width: {{ ($customer['total_amount'] / max($data['total_sales'], 1)) * 100 }}%"></div>
                        </div>
                    </div>
                    
                    <div class="grid grid-cols-3 gap-4 mt-3">
                        <div class="text-center">
                            <p class="text-xs text-gray-500">المدفوع</p>
                            <p class="font-semibold text-gray-900">{{ number_format($customer['paid_amount'], 2) }}</p>
                        </div>
                        <div class="text-center">
                            <p class="text-xs text-gray-500">المتبقي</p>
                            <p class="font-semibold text-red-600">{{ number_format($customer['remaining_amount'], 2) }}</p>
                        </div>
                        <div class="text-center">
                            <p class="text-xs text-gray-500">نسبة التحصيل</p>
                            <p class="font-semibold text-green-600">
                                {{ $customer['total_amount'] > 0 ? number_format(($customer['paid_amount'] / $customer['total_amount']) * 100, 1) : 0 }}%
                            </p>
                        </div>
                    </div>
                </div>
                @empty
                <tr>
                    <td colspan="5" 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
            </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('concentrationChart').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
