@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-6 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="period" 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="day" {{ $period === 'day' ? 'selected' : '' }}>يومي</option>
                        <option value="week" {{ $period === 'week' ? 'selected' : '' }}>أسبوعي</option>
                        <option value="month" {{ $period === 'month' ? 'selected' : '' }} selected>شهري</option>
                        <option value="quarter" {{ $period === 'quarter' ? 'selected' : '' }}>ربع سنوي</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>
                <div class="flex items-end">
                    <a href="{{ route('reports.sales.by-period') }}" class="w-full px-4 py-2 bg-gray-300 text-gray-700 rounded-lg hover:bg-gray-400 transition-colors font-medium text-center">
                        إعادة تعيين
                    </a>
                </div>
            </form>
        </div>

        <!-- Chart -->
        @if($data->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="salesChart" class="w-full" 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-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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($data as $item)
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ $item['period'] }}</td>
                            <td class="px-6 py-4 text-gray-700 text-center">{{ $item['invoice_count'] }}</td>
                            <td class="px-6 py-4 text-green-600 font-semibold">{{ number_format($item['total_sales'], 0) }}</td>
                            <td class="px-6 py-4 text-purple-600 font-semibold">{{ number_format($item['total_tax'], 0) }}</td>
                            <td class="px-6 py-4 text-orange-600 font-semibold">{{ number_format($item['total_discount'], 0) }}</td>
                            <td class="px-6 py-4 text-blue-600 font-semibold">{{ number_format($item['total_amount'], 0) }}</td>
                            <td class="px-6 py-4 text-emerald-600 font-semibold">{{ number_format($item['paid_amount'], 0) }}</td>
                            <td class="px-6 py-4 text-amber-600 font-semibold">{{ number_format($item['remaining_amount'], 0) }}</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>
</div>

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