@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">
        <!-- رأس الصفحة -->
        <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.index') }}" class="inline-flex items-center px-6 py-3 bg-gradient-to-r from-gray-600 to-gray-700 text-white font-semibold rounded-lg hover:from-gray-700 hover:to-gray-800 transition-all duration-200 shadow-lg">
                    <i class="fas fa-arrow-right ml-2"></i>
                    <span>العودة</span>
                </a>
            </div>
        </div>

        <!-- الفلاتر -->
        <div class="bg-white rounded-xl shadow-lg border border-gray-200 p-6 mb-8">
            <h3 class="text-lg font-semibold text-gray-900 mb-4">🔍 الفلاتر</h3>
            <form method="GET" class="grid grid-cols-1 md:grid-cols-3 gap-4">
                <div>
                    <label class="block text-gray-700 font-medium 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:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div>
                    <label class="block text-gray-700 font-medium 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:ring-2 focus:ring-blue-500 focus:border-transparent">
                </div>
                <div class="flex items-end">
                    <button type="submit" class="w-full px-6 py-2 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg transition-all duration-200 shadow-lg">
                        <i class="fas fa-search ml-2"></i>تحديث
                    </button>
                </div>
            </form>
        </div>

        <!-- الإحصائيات السريعة -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-6 gap-4 mb-8">
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-blue-500">
                <div class="text-gray-600 text-sm font-medium">إجمالي المرتجعات</div>
                <div class="text-2xl font-bold text-gray-900 mt-1">{{ $returnStats['total_returns'] }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-orange-500">
                <div class="text-gray-600 text-sm font-medium">المرتجعات البيعية</div>
                <div class="text-2xl font-bold text-gray-900 mt-1">{{ $returnStats['sales_returns'] }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-purple-500">
                <div class="text-gray-600 text-sm font-medium">الإشعارات الدائنة</div>
                <div class="text-2xl font-bold text-gray-900 mt-1">{{ $returnStats['credit_notes'] }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-red-500">
                <div class="text-gray-600 text-sm font-medium">إجمالي المرتجع</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($returnStats['total_returned_amount'], 2) }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-green-500">
                <div class="text-gray-600 text-sm font-medium">الضريبة المرتجعة</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($returnStats['total_tax_returned'], 2) }}</div>
            </div>
            <div class="bg-white rounded-lg shadow-md p-4 border-l-4 border-teal-500">
                <div class="text-gray-600 text-sm font-medium">الخصم المرتجع</div>
                <div class="text-lg font-bold text-gray-900 mt-1">{{ number_format($returnStats['total_discount_returned'], 2) }}</div>
            </div>
        </div>

        <!-- جدول المرتجعات -->
        <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-center font-semibold text-sm">المبلغ الفرعي</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">الضريبة</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">الخصم</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">الإجمالي</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm">الحالة</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($returns as $return)
                        <tr class="hover:bg-blue-50 transition-colors duration-150">
                            <td class="px-6 py-4 text-gray-600 font-medium">{{ $loop->iteration }}</td>
                            <td class="px-6 py-4 font-semibold text-blue-600">{{ $return['return_number'] }}</td>
                            <td class="px-6 py-4 text-gray-700">{{ \Carbon\Carbon::parse($return['return_date'])->format('Y-m-d') }}</td>
                            <td class="px-6 py-4">
                                @if($return['return_type'] === 'sales_return')
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-orange-100 text-orange-700">
                                        <i class="fas fa-box ml-1"></i>مرتجع بيعي
                                    </span>
                                @else
                                    <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-purple-100 text-purple-700">
                                        <i class="fas fa-receipt ml-1"></i>إشعار دائن
                                    </span>
                                @endif
                            </td>
                            <td class="px-6 py-4 text-gray-700">{{ $return['invoice_number'] }}</td>
                            <td class="px-6 py-4 font-medium text-gray-900">{{ $return['customer_name'] }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($return['return_subtotal'], 2) }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($return['return_tax'], 2) }}</td>
                            <td class="px-6 py-4 text-center">{{ number_format($return['return_discount'], 2) }}</td>
                            <td class="px-6 py-4 text-center font-semibold text-red-600">{{ number_format($return['return_total'], 2) }}</td>
                            <td class="px-6 py-4 text-center">
                                @php
                                    $statusBg = ['draft' => 'bg-gray-100', 'sent' => 'bg-blue-100', 'approved' => 'bg-green-100', 'cancelled' => 'bg-red-100'];
                                    $statusText = ['draft' => 'text-gray-700', 'sent' => 'text-blue-700', 'approved' => 'text-green-700', 'cancelled' => 'text-red-700'];
                                    $statusLabel = ['draft' => 'مسودة', 'sent' => 'مرسلة', 'approved' => 'معتمدة', 'cancelled' => 'ملغاة'];
                                @endphp
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium {{ $statusBg[$return['status']] ?? 'bg-gray-100' }} {{ $statusText[$return['status']] ?? 'text-gray-700' }}">
                                    {{ $statusLabel[$return['status']] ?? $return['status'] }}
                                </span>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="11" class="px-6 py-12 text-center">
                                <i class="fas fa-inbox text-gray-400 text-4xl mb-3 block"></i>
                                <p class="text-gray-600 font-medium">لا توجد مرتجعات</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- أزرار التصدير -->
        <div class="mt-8 flex gap-4 justify-end">
            <button onclick="window.print()" class="inline-flex items-center px-6 py-3 bg-blue-600 hover:bg-blue-700 text-white font-semibold rounded-lg">
                <i class="fas fa-print ml-2"></i><span>طباعة</span>
            </button>
        </div>
    </div>
</div>
@endsection