@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-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['total_sales'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ $data['invoice_count'] }} فاتورة</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['gross_profit'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">{{ $data['profit_margin'] }}% هامش الربح</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['net_revenue'], 2) }}</p>
                <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-amber-500">
                <p class="text-gray-600 text-sm">متوسط قيمة الفاتورة</p>
                <p class="text-3xl font-bold text-amber-600">{{ number_format($data['average_invoice_value'], 2) }}</p>
                <p class="text-xs text-gray-500 mt-2">من {{ $data['invoice_count'] }} فاتورة</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['net_revenue'], 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-bold text-green-600">{{ $data['profit_margin'] }}%</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'] / max($data['total_sales'], 1)) * 100, 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'] / max($data['total_sales'], 1)) * 100, 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">{{ $data['net_profit_margin'] }}%</span>
                    </div>
                </div>
            </div>
        </div>

        <div class="grid grid-cols-1 md:grid-cols-2 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>
                <div class="space-y-3">
                    <div class="flex items-center justify-between p-3 bg-green-50 rounded-lg">
                        <div>
                            <p class="text-gray-600 text-sm">المبالغ المدفوعة</p>
                            <p class="font-bold text-green-600">{{ number_format($data['total_paid'], 2) }}</p>
                        </div>
                        <i class="fas fa-check-circle text-4xl text-green-200"></i>
                    </div>
                    <div class="flex items-center justify-between p-3 bg-red-50 rounded-lg">
                        <div>
                            <p class="text-gray-600 text-sm">المبالغ المستحقة</p>
                            <p class="font-bold text-red-600">{{ number_format($data['total_outstanding'], 2) }}</p>
                        </div>
                        <i class="fas fa-exclamation-circle text-4xl text-red-200"></i>
                    </div>
                    <div class="flex items-center justify-between p-3 bg-blue-50 rounded-lg">
                        <div>
                            <p class="text-gray-600 text-sm">نسبة التحصيل</p>
                            <p class="font-bold text-blue-600">{{ number_format(($data['total_paid'] / max($data['net_revenue'], 1)) * 100, 2) }}%</p>
                        </div>
                        <i class="fas fa-percentage text-4xl text-blue-200"></i>
                    </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 text-gray-700">
                    <p>
                        <span class="font-semibold">الفترة:</span>
                        {{ $fromDate->format('d/m/Y') }} إلى {{ $toDate->format('d/m/Y') }}
                    </p>
                    <p>
                        <span class="font-semibold">عدد الفواتير:</span>
                        {{ $data['invoice_count'] }} فاتورة
                    </p>
                    <p class="p-3 bg-green-50 rounded-lg">
                        <span class="font-semibold">الربح الإجمالي:</span>
                        <span class="text-green-600 font-bold">{{ number_format($data['gross_profit'], 2) }} ريال</span>
                    </p>
                    <p class="p-3 bg-blue-50 rounded-lg">
                        <span class="font-semibold">متوسط قيمة الفاتورة:</span>
                        <span class="text-blue-600 font-bold">{{ number_format($data['average_invoice_value'], 2) }} ريال</span>
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>
@endsection
