@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 class="flex gap-2">
                    @if(request('export'))
                        <form method="GET" class="inline">
                            <input type="hidden" name="export" value="1">
                            <select name="format" class="px-4 py-2 rounded border border-gray-300">
                                <option value="excel">تصدير Excel</option>
                                <option value="pdf">تصدير PDF</option>
                                <option value="csv">تصدير CSV</option>
                            </select>
                            <button type="submit" class="ml-2 px-4 py-2 bg-green-600 text-white rounded">تصدير</button>
                        </form>
                    @else
                        <a href="?export=1" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-green-600 to-green-700 text-white font-semibold rounded-lg hover:from-green-700 hover:to-green-800 transition-all duration-200 shadow-lg">
                            <i class="fas fa-download ml-2"></i>
                            <span>تصدير</span>
                        </a>
                    @endif
                </div>
            </div>

            <!-- فلاتر البحث -->
            <div class="bg-white rounded-lg shadow p-4 mb-6">
                <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-1">من التاريخ</label>
                        <input type="date" name="start_date" value="{{ request('start_date') }}" class="w-full px-4 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-1">إلى التاريخ</label>
                        <input type="date" name="end_date" value="{{ request('end_date') }}" class="w-full px-4 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-1">&nbsp;</label>
                        <button type="submit" class="w-full px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700">
                            <i class="fas fa-filter ml-2"></i>بحث
                        </button>
                    </div>
                    <div>
                        <label class="block text-sm font-medium text-gray-700 mb-1">&nbsp;</label>
                        <a href="{{ route('reports.purchase.discounts-premiums') }}" class="w-full block text-center px-4 py-2 bg-gray-400 text-white rounded-lg hover:bg-gray-500">
                            <i class="fas fa-redo ml-2"></i>إعادة تعيين
                        </a>
                    </div>
                </form>
            </div>
        </div>

        <!-- البطاقات الإحصائية -->
        @if($data && isset($data['details']))
        <div class="grid grid-cols-1 md:grid-cols-3 lg:grid-cols-6 gap-4 mb-8">
            <!-- إجمالي الخصومات -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-blue-500">
                <div class="flex flex-col items-center text-center">
                    <div class="text-blue-500 text-3xl mb-2">
                        <i class="fas fa-minus-circle"></i>
                    </div>
                    <p class="text-gray-600 text-xs font-medium mb-1">إجمالي الخصومات</p>
                    <p class="text-2xl font-bold text-gray-900">
                        {{ number_format($data['total_discount'], 2) }}
                    </p>
                </div>
            </div>

            <!-- عدد الفواتير بخصم -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-green-500">
                <div class="flex flex-col items-center text-center">
                    <div class="text-green-500 text-3xl mb-2">
                        <i class="fas fa-receipt"></i>
                    </div>
                    <p class="text-gray-600 text-xs font-medium mb-1">فواتير بخصم</p>
                    <p class="text-2xl font-bold text-gray-900">
                        {{ $data['discount_count'] }}
                    </p>
                </div>
            </div>

            <!-- متوسط الخصم -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-yellow-500">
                <div class="flex flex-col items-center text-center">
                    <div class="text-yellow-500 text-3xl mb-2">
                        <i class="fas fa-percent"></i>
                    </div>
                    <p class="text-gray-600 text-xs font-medium mb-1">متوسط النسبة</p>
                    <p class="text-2xl font-bold text-gray-900">
                        @if($data['discount_count'] > 0)
                            {{ number_format(collect($data['details'])->avg('discount_percentage') ?? 0, 2) }}%
                        @else
                            0.00%
                        @endif
                    </p>
                </div>
            </div>

            <!-- إجمالي العلاوات -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-orange-500">
                <div class="flex flex-col items-center text-center">
                    <div class="text-orange-500 text-3xl mb-2">
                        <i class="fas fa-plus-circle"></i>
                    </div>
                    <p class="text-gray-600 text-xs font-medium mb-1">إجمالي العلاوات</p>
                    <p class="text-2xl font-bold text-gray-900">
                        {{ number_format($data['total_premium'] ?? 0, 2) }}
                    </p>
                </div>
            </div>

            <!-- أكبر خصم -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-red-500">
                <div class="flex flex-col items-center text-center">
                    <div class="text-red-500 text-3xl mb-2">
                        <i class="fas fa-arrow-up"></i>
                    </div>
                    <p class="text-gray-600 text-xs font-medium mb-1">أكبر خصم</p>
                    <p class="text-2xl font-bold text-gray-900">
                        @if(count($data['details']) > 0)
                            {{ number_format($data['details']->max('discount_amount') ?? 0, 2) }}
                        @else
                            0.00
                        @endif
                    </p>
                </div>
            </div>

            <!-- الفائدة الصافية -->
            <div class="bg-white rounded-xl shadow-lg p-6 border-l-4 border-purple-500">
                <div class="flex flex-col items-center text-center">
                    <div class="text-purple-500 text-3xl mb-2">
                        <i class="fas fa-balance-scale"></i>
                    </div>
                    <p class="text-gray-600 text-xs font-medium mb-1">الفائدة الصافية</p>
                    <p class="text-2xl font-bold {{ ($data['total_discount'] - ($data['total_premium'] ?? 0)) >= 0 ? 'text-green-600' : 'text-red-600' }}">
                        {{ number_format(($data['total_discount'] ?? 0) - ($data['total_premium'] ?? 0), 2) }}
                    </p>
                </div>
            </div>
        </div>
        @endif

        <!-- الجدول التفصيلي -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200">
            <div class="px-6 py-4 bg-gray-50 border-b border-gray-200">
                <h2 class="text-lg font-semibold text-gray-900">تفاصيل الخصومات</h2>
            </div>

            @if($data && isset($data['details']) && count($data['details']) > 0)
            <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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($data['details'] as $index => $item)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $index + 1 }}</td>
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-blue-100 text-blue-700">
                                    {{ $item['invoice_number'] }}
                                </span>
                            </td>
                            <td class="px-6 py-4 font-semibold text-gray-900">{{ $item['supplier_name'] }}</td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ \Carbon\Carbon::parse($item['invoice_date'])->format('Y-m-d') }}
                            </td>
                            <td class="px-6 py-4 text-gray-700 font-medium">
                                {{ number_format($item['invoice_total'], 2) }}
                            </td>
                            <td class="px-6 py-4">
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-red-100 text-red-700">
                                    {{ number_format($item['discount_amount'], 2) }}
                                </span>
                            </td>
                            <td class="px-6 py-4">
                                <div class="flex items-center">
                                    <div class="w-16 bg-gray-200 rounded-full h-2 mr-2">
                                        <div class="bg-red-500 h-2 rounded-full" style="width: {{ min($item['discount_percentage'], 100) }}%"></div>
                                    </div>
                                    <span class="text-sm font-medium text-gray-700">
                                        {{ number_format($item['discount_percentage'], 2) }}%
                                    </span>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                    </tbody>
                    <tfoot class="bg-gray-50">
                        <tr class="border-t-2 border-gray-300">
                            <td colspan="4" class="px-6 py-4 font-semibold text-gray-900">الإجمالي</td>
                            <td class="px-6 py-4 font-bold text-lg text-gray-900">
                                {{ number_format($data['details']->sum('invoice_total'), 2) }}
                            </td>
                            <td class="px-6 py-4 font-bold text-lg text-red-600">
                                {{ number_format($data['details']->sum('discount_amount'), 2) }}
                            </td>
                            <td class="px-6 py-4 font-bold text-lg">
                                {{ number_format($data['details']->avg('discount_percentage'), 2) }}%
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>
            @else
            <div 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>
                    <p class="text-gray-500 text-sm mt-1">حاول تغيير معايير البحث</p>
                </div>
            </div>
            @endif
        </div>

        <!-- رسم بياني للتوزيع -->
        @if($data && isset($data['details']) && count($data['details']) > 0)
        <div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mt-8">
            <!-- أعلى الموردين بالخصومات -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">أعلى الموردين بالخصومات</h3>
                @php
                    $topSuppliers = collect($data['details'])
                        ->groupBy('supplier_name')
                        ->map(function($items) {
                            return [
                                'supplier' => $items->first()['supplier_name'],
                                'total_discount' => $items->sum('discount_amount'),
                                'count' => $items->count(),
                            ];
                        })
                        ->sortByDesc('total_discount')
                        ->take(5);
                @endphp
                <div class="space-y-3">
                    @forelse($topSuppliers as $supplier)
                    <div class="flex items-center justify-between p-3 bg-gray-50 rounded-lg">
                        <div>
                            <p class="font-medium text-gray-900">{{ $supplier['supplier'] }}</p>
                            <p class="text-sm text-gray-500">{{ $supplier['count'] }} فاتورة</p>
                        </div>
                        <p class="font-bold text-red-600">{{ number_format($supplier['total_discount'], 2) }}</p>
                    </div>
                    @empty
                    <p class="text-gray-500 text-center py-4">لا توجد بيانات</p>
                    @endforelse
                </div>
            </div>

            <!-- توزيع الخصومات -->
            <div class="bg-white rounded-xl shadow-lg p-6">
                <h3 class="text-lg font-semibold text-gray-900 mb-4">إحصائيات الخصومات</h3>
                <div class="space-y-4">
                    @php
                        $allDiscounts = collect($data['details'])->pluck('discount_percentage');
                        $avgDiscount = $allDiscounts->avg();
                        $minDiscount = $allDiscounts->min();
                        $maxDiscount = $allDiscounts->max();
                    @endphp
                    <div class="flex justify-between items-center p-3 bg-blue-50 rounded-lg">
                        <span class="text-gray-700 font-medium">متوسط نسبة الخصم</span>
                        <span class="text-2xl font-bold text-blue-600">{{ number_format($avgDiscount, 2) }}%</span>
                    </div>
                    <div class="flex justify-between items-center p-3 bg-green-50 rounded-lg">
                        <span class="text-gray-700 font-medium">أقل نسبة خصم</span>
                        <span class="text-2xl font-bold text-green-600">{{ number_format($minDiscount, 2) }}%</span>
                    </div>
                    <div class="flex justify-between items-center p-3 bg-red-50 rounded-lg">
                        <span class="text-gray-700 font-medium">أعلى نسبة خصم</span>
                        <span class="text-2xl font-bold text-red-600">{{ number_format($maxDiscount, 2) }}%</span>
                    </div>
                </div>
            </div>
        </div>
        @endif

    </div>
</div>

@push('scripts')
<script src="https://cdn.jsdelivr.net/npm/chart.js@4.4.0/dist/chart.umd.js"></script>
<script>
    @if($data && isset($data['details']) && count($data['details']) > 0)
    // إذا كنت تريد إضافة رسوم بيانية متقدمة
    @endif
</script>
@endpush

@endsection