@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>
                    <input type="date" name="from_date" value="{{ $fromDate->format('Y-m-d') }}" 
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg 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->format('Y-m-d') }}" 
                           class="w-full px-3 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500">
                </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-3 gap-6 mb-6">
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-blue-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">معدل الخصم</p>
                        <p class="text-3xl font-bold text-blue-600">{{ number_format($data['discount_rate'], 2) }}%</p>
                    </div>
                    <i class="fas fa-percent text-4xl text-blue-200"></i>
                </div>
                <p class="text-xs text-gray-500 mt-2">من إجمالي المبيعات</p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-green-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">معدل الضريبة</p>
                        <p class="text-3xl font-bold text-green-600">{{ number_format($data['tax_rate'], 2) }}%</p>
                    </div>
                    <i class="fas fa-receipt text-4xl text-green-200"></i>
                </div>
                <p class="text-xs text-gray-500 mt-2">من إجمالي المبيعات</p>
            </div>

            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-purple-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm">عدد الفواتير</p>
                        <p class="text-3xl font-bold text-purple-600">{{ number_format($data['invoice_count']) }}</p>
                    </div>
                    <i class="fas fa-file-invoice text-4xl text-purple-200"></i>
                </div>
                <p class="text-xs text-gray-500 mt-2">في الفترة المحددة</p>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 gap-6 mb-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>
                <div class="space-y-4">
                    <div class="flex justify-between items-center py-3 border-b border-gray-200">
                        <span class="text-gray-600">إجمالي المبيعات</span>
                        <span class="font-semibold text-gray-900">{{ number_format($data['total_sales'], 2) }}</span>
                    </div>
                    <div class="flex justify-between items-center py-3 border-b border-gray-200">
                        <span class="text-gray-600">إجمالي الخصم</span>
                        <span class="font-semibold text-red-600">{{ number_format($data['total_discount'], 2) }}</span>
                    </div>
                    <div class="flex justify-between items-center py-3 border-b border-gray-200">
                        <span class="text-gray-600">إجمالي الضريبة</span>
                        <span class="font-semibold text-green-600">{{ number_format($data['total_tax'], 2) }}</span>
                    </div>
                    <div class="flex justify-between items-center py-3 bg-blue-50 px-3 rounded-lg">
                        <span class="font-semibold text-gray-900">الإجمالي النهائي</span>
                        <span class="font-bold text-blue-600">{{ number_format($data['total_amount'], 2) }}</span>
                    </div>
                </div>
            </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>
                <div class="space-y-4">
                    <div class="flex justify-between items-center py-3 border-b border-gray-200">
                        <span class="text-gray-600">متوسط الخصم</span>
                        <span class="font-semibold text-gray-900">{{ number_format($data['average_discount_per_invoice'], 2) }}</span>
                    </div>
                    <div class="flex justify-between items-center py-3 border-b border-gray-200">
                        <span class="text-gray-600">متوسط الضريبة</span>
                        <span class="font-semibold text-gray-900">{{ number_format($data['average_tax_per_invoice'], 2) }}</span>
                    </div>
                    <div class="flex justify-between items-center py-3 bg-gray-50 px-3 rounded-lg">
                        <span class="font-semibold text-gray-900">متوسط قيمة الفاتورة</span>
                        <span class="font-bold text-gray-900">{{ number_format(($data['total_amount'] / max($data['invoice_count'], 1)), 2) }}</span>
                    </div>
                </div>
            </div>
        </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>
            <div class="space-y-3">
                <p class="text-gray-700">
                    <span class="font-semibold">إجمالي المبيعات:</span>
                    <span class="text-gray-900">{{ number_format($data['total_sales'], 2) }} ريال</span>
                </p>
                <p class="text-gray-700">
                    <span class="font-semibold">نسبة الخصم المطبقة:</span>
                    <span class="text-red-600 font-semibold">{{ number_format($data['discount_rate'], 2) }}%</span>
                    ({{ number_format($data['total_discount'], 2) }} ريال)
                </p>
                <p class="text-gray-700">
                    <span class="font-semibold">نسبة الضريبة المطبقة:</span>
                    <span class="text-green-600 font-semibold">{{ number_format($data['tax_rate'], 2) }}%</span>
                    ({{ number_format($data['total_tax'], 2) }} ريال)
                </p>
                <p class="text-gray-700 bg-blue-50 p-3 rounded-lg">
                    <span class="font-semibold">الإجمالي المستحق من العملاء:</span>
                    <span class="text-blue-600 font-bold">{{ number_format($data['total_amount'], 2) }} ريال</span>
                </p>
            </div>
        </div>
    </div>
</div>
@endsection
