@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">{{ number_format($itemStats['total_items']) }}</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-2xl font-bold text-gray-900 mt-1">{{ number_format($itemStats['total_quantity'], 2) }}</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">{{ number_format($itemStats['total_sales'], 2) }}</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">{{ number_format($itemStats['total_discount'], 2) }}</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-2xl font-bold text-gray-900 mt-1">{{ number_format($itemStats['total_tax'], 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-2xl font-bold text-gray-900 mt-1">{{ number_format($itemStats['total_amount'], 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($items as $item)
                        <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">{{ $item['invoice_number'] }}</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-600">{{ $item['customer_id'] }}</td>
                            <td class="px-6 py-4 font-medium text-gray-900">{{ $item['customer_name'] }}</td>
                            <td class="px-6 py-4 text-gray-700 text-sm">{{ $item['description'] }}</td>
                            <td class="px-6 py-4 text-center text-gray-700">{{ number_format($item['quantity'], 2) }}</td>
                            <td class="px-6 py-4 text-center text-gray-700">{{ number_format($item['unit_price'], 2) }}</td>
                            <td class="px-6 py-4 text-center text-orange-600 font-medium">{{ number_format($item['discount_amount'], 2) }}</td>
                            <td class="px-6 py-4 text-center text-red-600 font-medium">{{ number_format($item['tax_amount'], 2) }}</td>
                            <td class="px-6 py-4 text-center font-bold text-teal-600">{{ number_format($item['total'], 2) }}</td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="11" 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>
                            </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 transition-all duration-200 shadow-lg">
                <i class="fas fa-print ml-2"></i>
                <span>طباعة</span>
            </button>
        </div>
    </div>
</div>
@endsection
