@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">
        <!-- Header -->
        <div class="mb-8 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>
            <a href="{{ route('reports.sales.index') }}" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white rounded-lg hover:bg-gray-700">
                <i class="fas fa-arrow-right mr-2"></i>
                عودة
            </a>
        </div>

        <!-- Filters -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
            <form method="GET" class="grid grid-cols-1 md:grid-cols-5 gap-4">
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">من التاريخ</label>
                    <input type="date" name="from_date" value="{{ $fromDate }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none 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 }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">عدد العملاء</label>
                    <select name="limit" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                        <option value="5" {{ $limit == 5 ? 'selected' : '' }}>أفضل 5</option>
                        <option value="10" {{ $limit == 10 ? 'selected' : '' }} selected>أفضل 10</option>
                        <option value="15" {{ $limit == 15 ? 'selected' : '' }}>أفضل 15</option>
                        <option value="20" {{ $limit == 20 ? 'selected' : '' }}>أفضل 20</option>
                    </select>
                </div>
                <div class="flex items-end">
                    <button type="submit" class="w-full px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors font-medium">
                        تحديث
                    </button>
                </div>
                <div class="flex items-end">
                    <button type="button" onclick="window.print()" class="w-full px-4 py-2 bg-gray-600 text-white rounded-lg hover:bg-gray-700 transition-colors font-medium">
                        طباعة
                    </button>
                </div>
            </form>
        </div>

        <!-- Chart -->
        @if($customers->count() > 0)
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
            <h3 class="text-lg font-bold text-gray-900 mb-4">📊 الرسم البياني</h3>
            <canvas id="topCustomersChart" style="max-height: 300px;"></canvas>
        </div>
        @endif

        <!-- Table -->
        <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-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>
                            <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($customers as $customer)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 text-center">
                                <div class="inline-flex items-center justify-center w-8 h-8 rounded-full {{ $loop->iteration == 1 ? 'bg-yellow-100 text-yellow-700 font-bold' : ($loop->iteration == 2 ? 'bg-gray-300 text-gray-700 font-bold' : ($loop->iteration == 3 ? 'bg-orange-100 text-orange-700 font-bold' : 'bg-blue-100 text-blue-700')) }}">
                                    @if($loop->iteration == 1)
                                        🥇
                                    @elseif($loop->iteration == 2)
                                        🥈
                                    @elseif($loop->iteration == 3)
                                        🥉
                                    @else
                                        {{ $loop->iteration }}
                                    @endif
                                </div>
                            </td>
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ $customer['customer_name'] }}</td>
                            <td class="px-6 py-4 text-gray-700 text-center">{{ $customer['invoice_count'] }}</td>
                            <td class="px-6 py-4 text-green-600 font-semibold">{{ number_format($customer['total_amount'], 0) }} ريال</td>
                            <td class="px-6 py-4 text-emerald-600 font-semibold">{{ number_format($customer['paid_amount'], 0) }} ريال</td>
                            <td class="px-6 py-4 text-amber-600 font-semibold">{{ number_format($customer['remaining_amount'], 0) }} ريال</td>
                            <td class="px-6 py-4">
                                <div class="w-full bg-gray-200 rounded-full h-2">
                                    <div class="bg-blue-600 h-2 rounded-full" style="width: {{ $customer['percentage'] }}%"></div>
                                </div>
                                <p class="text-sm text-gray-600 text-center mt-1">{{ $customer['percentage'] }}%</p>
                            </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>

@if($customers->count() > 0)
<script src="https://cdn.jsdelivr.net/npm/chart.js"></script>
<script>
    const ctx = document.getElementById('topCustomersChart').getContext('2d');
    const chart = new Chart(ctx, {
        type: 'doughnut',
        data: {
            labels: {!! json_encode($chartData['labels']) !!},
            datasets: [{
                data: {!! json_encode($chartData['datasets'][0]['data']) !!},
                backgroundColor: {!! json_encode($chartData['datasets'][0]['backgroundColor']) !!},
                borderColor: '#ffffff',
                borderWidth: 2
            }]
        },
        options: {
            responsive: true,
            maintainAspectRatio: true,
            plugins: {
                legend: {
                    position: 'right',
                }
            }
        }
    });
</script>
@endif
@endsection
