@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>
                <a href="{{ route('reports.sales.index') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-gray-600 to-gray-700 text-white font-semibold rounded-lg">
                    <i class="fas fa-arrow-right ml-2"></i><span>العودة</span>
                </a>
            </div>
        </div>

        <!-- الفلاتر -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">🔍 الفلاتر</h3>
            <form method="GET" class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div>
                    <label class="block text-gray-700 font-medium mb-2">السنة</label>
                    <input type="number" name="year" value="{{ request('year', date('Y')) }}" min="2020" class="w-full px-4 py-2 border border-gray-300 rounded-lg">
                </div>
                <div>
                    <label class="block text-gray-700 font-medium mb-2">نوع المقارنة</label>
                    <select name="comparison" class="w-full px-4 py-2 border border-gray-300 rounded-lg">
                        <option value="monthly" {{ $comparison === 'monthly' ? 'selected' : '' }}>شهري</option>
                        <option value="yearly" {{ $comparison === 'yearly' ? 'selected' : '' }}>سنوي</option>
                    </select>
                </div>
                <div class="flex items-end">
                    <button type="submit" class="w-full px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg">
                        <i class="fas fa-search ml-2"></i>تحديث
                    </button>
                </div>
            </form>
        </div>

        <!-- الرسم البياني -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">📈 الرسم البياني</h3>
            <canvas id="comparisonChart"></canvas>
        </div>

        <!-- جدول المقارنة -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden">
            <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>
                            @if($comparison === 'monthly')
                                <th class="px-6 py-4 text-right font-semibold text-sm">الشهر</th>
                            @else
                                <th class="px-6 py-4 text-right font-semibold text-sm">السنة</th>
                            @endif
                            <th class="px-6 py-4 text-center 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-center 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-center 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-center 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 $row)
                        <tr class="hover:bg-blue-50">
                            <td class="px-6 py-4">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4 font-semibold">
                                @if($comparison === 'monthly')
                                    {{ $row['month_name'] ?? $row['month'] }}
                                @else
                                    {{ $row['year'] }}
                                @endif
                            </td>
                            <td class="px-6 py-4 text-center">{{ $row['invoice_count'] }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($row['total_sales'], 2) }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($row['total_discount'], 2) }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($row['total_tax'], 2) }}</td>
                            <td class="px-6 py-4 text-center font-semibold">{{ number_format($row['total_amount'], 2) }}</td>
                            <td class="px-6 py-4 text-center text-green-600">{{ number_format($row['paid_amount'], 2) }}</td>
                            <td class="px-6 py-4 text-center text-orange-600">{{ number_format($row['remaining_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 bg-blue-100 text-blue-700">
                                    {{ $row['collection_rate'] }}%
                                </span>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="10" class="px-6 py-12 text-center">لا توجد بيانات</td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- أزرار التصدير -->
        <div class="mt-8 flex gap-4 justify-end">
            <button onclick="window.print()" class="inline-flex items-center px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg">
                <i class="fas fa-print ml-2"></i><span>طباعة</span>
            </button>
        </div>
    </div>
</div>

<script src="https://cdn.jsdelivr.net/npm/chart.js@latest"></script>
<script>
const ctx = document.getElementById('comparisonChart').getContext('2d');
new Chart(ctx, {
    type: 'line',
    data: {
        labels: {!! json_encode($chartData['labels']) !!},
        datasets: {!! json_encode($chartData['datasets']) !!}
    },
    options: {
        responsive: true,
        plugins: { legend: { display: true, position: 'top' } },
        scales: { y: { beginAtZero: true } }
    }
});
</script>
@endsection