@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="year" class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                        @for($y = now()->year; $y >= now()->year - 5; $y--)
                        <option value="{{ $y }}" {{ request('year', now()->year) == $y ? 'selected' : '' }}>
                            {{ $y }}
                        </option>
                        @endfor
                    </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['average_monthly_sales'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">السنة {{ $data['year'] }}</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['highest_month']['total_amount'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ $data['highest_month']['month_name'] }}</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['lowest_month']['total_amount'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ $data['lowest_month']['month_name'] }}</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['highest_month']['total_amount'] - $data['lowest_month']['total_amount']), 2) }}
                </p>
                <p class="text-xs text-gray-500 mt-2">
                    {{ number_format((($data['highest_month']['total_amount'] - $data['lowest_month']['total_amount']) / max($data['average_monthly_sales'], 1)) * 100, 1) }}% فرق
                </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="seasonalityChart" 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-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['monthly_data'] as $month)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $month['month'] }}</td>
                            <td class="px-6 py-4">
                                <div class="font-semibold text-gray-900">{{ $month['month_name'] }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-600">{{ number_format($month['invoice_count']) }}</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-green-600 font-semibold">{{ number_format($month['paid_amount'], 2) }}</td>
                            <td class="px-6 py-4 text-red-600">{{ number_format($month['total_amount'] - $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['index'] > 120) bg-green-100 text-green-700
                                    @elseif($month['index'] > 100) bg-blue-100 text-blue-700
                                    @elseif($month['index'] > 80) bg-yellow-100 text-yellow-700
                                    @else bg-red-100 text-red-700 @endif">
                                    {{ number_format($month['index'], 1) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center">
                                @if($month['trend'] === 'مرتفع')
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-green-100 text-green-700">
                                    <i class="fas fa-arrow-up ml-1"></i> {{ $month['trend'] }}
                                </span>
                                @elseif($month['trend'] === 'متوسط')
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-yellow-100 text-yellow-700">
                                    <i class="fas fa-equals ml-1"></i> {{ $month['trend'] }}
                                </span>
                                @else
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                    <i class="fas fa-arrow-down ml-1"></i> {{ $month['trend'] }}
                                </span>
                                @endif
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" 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 class="mt-6 bg-blue-50 border border-blue-200 rounded-xl p-6">
            <h3 class="text-lg font-bold text-blue-900 mb-4">
                <i class="fas fa-lightbulb ml-2"></i> تحليل الموسمية
            </h3>
            <p class="text-blue-800">
                مؤشر الموسمية يقيس مدى انحراف المبيعات عن المتوسط العام:
                <br>
                • <strong>مؤشر > 100</strong>: المبيعات أعلى من المتوسط (موسم مرتفع)
                <br>
                • <strong>مؤشر = 100</strong>: المبيعات تساوي المتوسط (موسم عادي)
                <br>
                • <strong>مؤشر < 100</strong>: المبيعات أقل من المتوسط (موسم منخفض)
            </p>
        </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('seasonalityChart').getContext('2d');
        const chartData = {!! json_encode($chartData) !!};
        
        new Chart(ctx, {
            type: 'line',
            data: chartData,
            options: {
                responsive: true,
                plugins: {
                    legend: {
                        display: true,
                        position: 'top'
                    },
                    title: {
                        display: true,
                        text: 'اتجاه المبيعات الشهري والمتوسط العام'
                    }
                },
                scales: {
                    y: {
                        beginAtZero: true,
                        title: {
                            display: true,
                            text: 'قيمة المبيعات (ريال)'
                        }
                    }
                }
            }
        });
    });
</script>
@endsection
