@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-5 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 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.summary') }}" 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>

        <!-- Statistics Cards -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">عدد الفواتير</p>
                <p class="text-3xl font-bold text-blue-600">{{ number_format($summary['total_invoices']) }}</p>
                <p class="text-xs text-gray-500 mt-2">فاتورة</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">إجمالي المبيعات</p>
                <p class="text-3xl font-bold text-green-600">{{ number_format($summary['total_sales'], 0) }}</p>
                <p class="text-xs text-gray-500 mt-2">ريال</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">الخصومات</p>
                <p class="text-3xl font-bold text-orange-600">{{ number_format($summary['total_discount'], 0) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ number_format(($summary['total_discount'] / max(1, $summary['total_sales'])) * 100, 2) }}%</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">الضريبة</p>
                <p class="text-3xl font-bold text-purple-600">{{ number_format($summary['total_tax'], 0) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ number_format(($summary['total_tax'] / max(1, $summary['total_sales'])) * 100, 2) }}%</p>
            </div>
        </div>

        <!-- Summary Table -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-8">
            <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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        <tr class="hover:bg-blue-50 transition-colors">
                            <td class="px-6 py-4 font-medium text-gray-900">المبيعات الفرعية</td>
                            <td class="px-6 py-4 text-gray-700">{{ number_format($summary['total_sales'], 0) }} ريال</td>
                            <td class="px-6 py-4 text-gray-700">100%</td>
                        </tr>
                        <tr class="hover:bg-orange-50 transition-colors">
                            <td class="px-6 py-4 font-medium text-gray-900">الخصومات</td>
                            <td class="px-6 py-4 text-orange-600">-{{ number_format($summary['total_discount'], 0) }} ريال</td>
                            <td class="px-6 py-4 text-gray-700">{{ number_format(($summary['total_discount'] / max(1, $summary['total_sales'])) * 100, 2) }}%</td>
                        </tr>
                        <tr class="hover:bg-purple-50 transition-colors">
                            <td class="px-6 py-4 font-medium text-gray-900">الضريبة المضافة</td>
                            <td class="px-6 py-4 text-purple-600">+{{ number_format($summary['total_tax'], 0) }} ريال</td>
                            <td class="px-6 py-4 text-gray-700">{{ number_format(($summary['total_tax'] / max(1, $summary['total_sales'])) * 100, 2) }}%</td>
                        </tr>
                        <tr class="bg-blue-50 border-t-2 border-blue-200">
                            <td class="px-6 py-4 font-bold text-gray-900">الإجمالي</td>
                            <td class="px-6 py-4 font-bold text-blue-600">{{ number_format($summary['total_amount'], 0) }} ريال</td>
                            <td class="px-6 py-4 font-bold text-gray-700">100%</td>
                        </tr>
                        <tr class="hover:bg-green-50 transition-colors">
                            <td class="px-6 py-4 font-medium text-gray-900">المبالغ المدفوعة</td>
                            <td class="px-6 py-4 text-green-600">{{ number_format($summary['paid_amount'], 0) }} ريال</td>
                            <td class="px-6 py-4 text-gray-700">{{ number_format(($summary['paid_amount'] / max(1, $summary['total_amount'])) * 100, 2) }}%</td>
                        </tr>
                        <tr class="hover:bg-amber-50 transition-colors">
                            <td class="px-6 py-4 font-medium text-gray-900">المبالغ المعلقة</td>
                            <td class="px-6 py-4 text-amber-600">{{ number_format($summary['remaining_amount'], 0) }} ريال</td>
                            <td class="px-6 py-4 text-gray-700">{{ number_format(($summary['remaining_amount'] / max(1, $summary['total_amount'])) * 100, 2) }}%</td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Additional Stats -->
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6">
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <h3 class="text-lg font-bold text-gray-900 mb-4">📈 النسب والمؤشرات</h3>
                <ul class="space-y-3">
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">معدل التحصيل</span>
                        <span class="font-bold text-green-600">{{ $stats['collection_rate'] }}%</span>
                    </li>
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">متوسط الفاتورة</span>
                        <span class="font-bold text-blue-600">{{ number_format($stats['average_invoice'], 0) }} ر.س</span>
                    </li>
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">عدد الفواتير</span>
                        <span class="font-bold text-purple-600">{{ $stats['total_invoices'] }}</span>
                    </li>
                </ul>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <h3 class="text-lg font-bold text-gray-900 mb-4">💰 الملخص المالي</h3>
                <ul class="space-y-3">
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">إجمالي المبيعات</span>
                        <span class="font-bold text-green-600">{{ number_format($summary['total_sales'], 0) }} ر.س</span>
                    </li>
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">الضريبة المضافة</span>
                        <span class="font-bold text-purple-600">{{ number_format($summary['total_tax'], 0) }} ر.س</span>
                    </li>
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">صافي المبيعات</span>
                        <span class="font-bold text-blue-600">{{ number_format($summary['total_amount'], 0) }} ر.س</span>
                    </li>
                </ul>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <h3 class="text-lg font-bold text-gray-900 mb-4">📋 فترة التقرير</h3>
                <ul class="space-y-3">
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">من التاريخ</span>
                        <span class="font-bold text-gray-600">{{ \Carbon\Carbon::parse($fromDate)->format('Y-m-d') }}</span>
                    </li>
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">إلى التاريخ</span>
                        <span class="font-bold text-gray-600">{{ \Carbon\Carbon::parse($toDate)->format('Y-m-d') }}</span>
                    </li>
                    <li class="flex items-center justify-between">
                        <span class="text-gray-700">عدد الأيام</span>
                        <span class="font-bold text-gray-600">{{ \Carbon\Carbon::parse($toDate)->diffInDays(\Carbon\Carbon::parse($fromDate)) + 1 }}</span>
                    </li>
                </ul>
            </div>
        </div>
    </div>
</div>
@endsection
