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

            <!-- ملخص الإحصائيات -->
            <div class="bg-white rounded-lg shadow-md p-6 mb-6 border border-gray-200">
                <div class="grid grid-cols-1 md:grid-cols-4 gap-4">
                    <div class="flex items-center space-x-4 space-x-reverse">
                        <div class="flex-shrink-0">
                            <span class="inline-flex items-center justify-center h-12 w-12 rounded-md bg-blue-100">
                                <i class="fas fa-receipt text-blue-600 text-lg"></i>
                            </span>
                        </div>
                        <div>
                            <p class="text-gray-500 text-sm">إجمالي الفواتير</p>
                            <p class="text-2xl font-bold text-gray-900">{{ $salesCount }}</p>
                        </div>
                    </div>

                    <div class="flex items-center space-x-4 space-x-reverse">
                        <div class="flex-shrink-0">
                            <span class="inline-flex items-center justify-center h-12 w-12 rounded-md bg-green-100">
                                <i class="fas fa-money-bill text-green-600 text-lg"></i>
                            </span>
                        </div>
                        <div>
                            <p class="text-gray-500 text-sm">إجمالي المبيعات</p>
                            <p class="text-2xl font-bold text-green-600">{{ number_format($totalSales, 2) }}</p>
                        </div>
                    </div>

                    <div class="flex items-center space-x-4 space-x-reverse">
                        <div class="flex-shrink-0">
                            <span class="inline-flex items-center justify-center h-12 w-12 rounded-md bg-red-100">
                                <i class="fas fa-undo text-red-600 text-lg"></i>
                            </span>
                        </div>
                        <div>
                            <p class="text-gray-500 text-sm">إجمالي المرتجعات</p>
                            <p class="text-2xl font-bold text-red-600">{{ number_format(abs($totalReturns), 2) }}</p>
                        </div>
                    </div>

                    <div class="flex items-center space-x-4 space-x-reverse">
                        <div class="flex-shrink-0">
                            <span class="inline-flex items-center justify-center h-12 w-12 rounded-md bg-purple-100">
                                <i class="fas fa-chart-line text-purple-600 text-lg"></i>
                            </span>
                        </div>
                        <div>
                            <p class="text-gray-500 text-sm">صافي المبيعات</p>
                            <p class="text-2xl font-bold text-purple-600">{{ number_format($netSales, 2) }}</p>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- الفلاتر -->
        <div class="bg-white rounded-lg shadow-md p-6 mb-6 border border-gray-200">
            <form action="{{ route('reports.sales.all') }}" 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-1">من التاريخ</label>
                    <input type="date" name="date_from" value="{{ $dateFrom }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500">
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">إلى التاريخ</label>
                    <input type="date" name="date_to" value="{{ $dateTo }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500">
                </div>

                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-1">نوع التقرير</label>
                    <select name="report_type" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-blue-500 focus:border-blue-500">
                        <option value="all" {{ $reportType === 'all' ? 'selected' : '' }}>الكل</option>
                        <option value="sales" {{ $reportType === 'sales' ? 'selected' : '' }}>فواتير المبيعات فقط</option>
                        <option value="returns" {{ $reportType === 'returns' ? 'selected' : '' }}>المرتجعات فقط</option>
                    </select>
                </div>

                <div class="flex items-end">
                    <button type="submit" class="w-full px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition-colors duration-200">
                        <i class="fas fa-search ml-2"></i>بحث
                    </button>
                </div>
            </form>
        </div>

        <!-- أزرار التصدير -->
        <div class="mb-6 flex gap-2">
            <a href="{{ route('reports.sales.all', array_merge(request()->query(), ['export' => 'true', 'format' => 'excel'])) }}" class="inline-flex items-center px-4 py-2 bg-green-600 hover:bg-green-700 text-white font-medium rounded-lg transition-colors duration-200 shadow-md hover:shadow-lg">
                <i class="fas fa-file-excel mr-2"></i>Excel
            </a>
            <a href="{{ route('reports.sales.all', array_merge(request()->query(), ['export' => 'true', 'format' => 'pdf'])) }}" class="inline-flex items-center px-4 py-2 bg-red-600 hover:bg-red-700 text-white font-medium rounded-lg transition-colors duration-200 shadow-md hover:shadow-lg">
                <i class="fas fa-file-pdf mr-2"></i>PDF
            </a>
            <a href="{{ route('reports.sales.all', array_merge(request()->query(), ['export' => 'true', 'format' => 'csv'])) }}" class="inline-flex items-center px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white font-medium rounded-lg transition-colors duration-200 shadow-md hover:shadow-lg">
                <i class="fas fa-file-csv mr-2"></i>CSV
            </a>
        </div>

        <!-- الجدول -->
        @if($data->isEmpty())
        <div class="bg-white rounded-xl shadow-lg p-12 text-center border border-gray-200">
            <i class="fas fa-inbox text-gray-400 text-5xl mb-4"></i>
            <p class="text-gray-600 font-medium text-lg">لا توجد بيانات للعرض</p>
            <p class="text-gray-500 text-sm mt-2">لا توجد فواتير مبيعات في الفترة المحددة</p>
        </div>
        @else
        <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 duration-150 border-b border-gray-100">
                            <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">{{ $item['number'] }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-600">{{ $item['date'] }}</td>
                            <td class="px-6 py-4 text-gray-600">{{ $item['customer_name'] }}</td>
                            <td class="px-6 py-4">
                                @if($item['is_return'])
                                <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-undo ml-1"></i>
                                    مرتجع
                                </span>
                                @else
                                <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-check-circle ml-1"></i>
                                    مبيعات
                                </span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-right font-semibold text-gray-600">
                                {{ number_format($item['discount'], 2) }}
                            </td>
                            <td class="px-6 py-4 text-right font-semibold text-gray-600">
                                {{ number_format($item['tax'], 2) }}
                            </td>
                            <td class="px-6 py-4 text-right font-semibold">
                                <span class="inline-flex items-center px-3 py-1 rounded-lg {{ $item['is_return'] ? 'bg-red-50 text-red-600' : 'bg-green-50 text-green-600' }}">
                                    {{ number_format($item['amount'], 2) }}
                                </span>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="8" class="px-6 py-12 text-center">
                                <p class="text-gray-600">لا توجد بيانات</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>

            <!-- ملخص النتائج -->
            <div class="bg-gray-50 border-t border-gray-200">
                <div class="grid grid-cols-1 md:grid-cols-3 gap-6 p-6">
                    <div class="text-center">
                        <p class="text-gray-600 text-sm mb-1">إجمالي المبيعات</p>
                        <p class="text-2xl font-bold text-green-600">{{ number_format($totalSales, 2) }}</p>
                    </div>
                    <div class="text-center border-l border-r border-gray-200">
                        <p class="text-gray-600 text-sm mb-1">إجمالي المرتجعات</p>
                        <p class="text-2xl font-bold text-red-600">{{ number_format(abs($totalReturns), 2) }}</p>
                    </div>
                    <div class="text-center">
                        <p class="text-gray-600 text-sm mb-1">صافي المبيعات</p>
                        <p class="text-2xl font-bold text-purple-600">{{ number_format($netSales, 2) }}</p>
                    </div>
                </div>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
