@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>
                    <select name="months" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        <option value="3" {{ request('months') == 3 ? 'selected' : '' }}>3 أشهر</option>
                        <option value="6" {{ request('months') == 6 ? 'selected' : '' }}>6 أشهر</option>
                        <option value="12" {{ request('months') == 12 ? 'selected' : '' }} selected>12 شهر</option>
                        <option value="24" {{ request('months') == 24 ? 'selected' : '' }}>24 شهر</option>
                    </select>
                </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($data->sum('total_amount'), 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">لـ {{ $data->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($data->sum('paid_amount'), 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ number_format(($data->sum('paid_amount') / max($data->sum('total_amount'), 1)) * 100, 1) }}% من الإجمالي</p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-red-500">
                <p class="text-gray-600 text-sm">المبالغ المتبقية</p>
                <p class="text-3xl font-bold text-red-600">{{ number_format($data->sum('remaining_amount'), 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ number_format(($data->sum('remaining_amount') / max($data->sum('total_amount'), 1)) * 100, 1) }}% من الإجمالي</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-3xl font-bold text-purple-600">{{ number_format(($data->sum('total_amount') / max($data->count(), 1)), 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">متوسط قيمة الشهر</p>
            </div>
        </div>

        <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200 mb-6">
            <h3 class="text-lg font-bold text-gray-900 mb-4">مخطط الأداء الشهري</h3>
            <canvas id="performanceChart" height="300"></canvas>
        </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-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($data as $month)
                        <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">{{ $month['month'] }}</div>
                                <p class="text-xs text-gray-500">{{ $month['year'] }}</p>
                            </td>
                            <td class="px-6 py-4 text-gray-600">{{ number_format($month['invoice_count']) }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ number_format($month['total_sales'], 2) }}</td>
                            <td class="px-6 py-4 text-red-600">{{ number_format($month['total_discount'], 2) }}</td>
                            <td class="px-6 py-4 text-green-600">{{ number_format($month['total_tax'], 2) }}</td>
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ number_format($month['total_amount'], 2) }}</td>
                            <td class="px-6 py-4 text-blue-600 font-semibold">{{ number_format($month['paid_amount'], 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 
                                    @if($month['collection_rate'] >= 80) bg-green-100 text-green-700
                                    @elseif($month['collection_rate'] >= 50) bg-yellow-100 text-yellow-700
                                    @else bg-red-100 text-red-700 @endif">
                                    {{ number_format($month['collection_rate'], 1) }}%
                                </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>
                                </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('performanceChart').getContext('2d');
        const chartData = {!! json_encode($chartData) !!};
        
        new Chart(ctx, {
            type: 'line',
            data: chartData,
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        display: true,
                        position: 'top'
                    }
                },
                scales: {
                    y: {
                        beginAtZero: true,
                        title: {
                            display: true,
                            text: 'المبلغ (ريال)'
                        }
                    },
                    y1: {
                        type: 'linear',
                        display: true,
                        position: 'right',
                        beginAtZero: true,
                        max: 100,
                        title: {
                            display: true,
                            text: 'نسبة التحصيل (%)'
                        }
                    }
                }
            }
        });
    });
</script>
@endsection
