@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">
        <!-- Header -->
        <div class="mb-8 flex items-center justify-between">
            <div>
                <h1 class="text-3xl font-bold text-gray-900 mb-2">📋 قائمة الفواتير المفصلة</h1>
                <p class="text-gray-600">عرض شامل لجميع الفواتير مع تصفية متقدمة</p>
            </div>
            <div class="flex gap-3">
                <a href="{{ route('reports.sales.invoice-list-detailed-pdf', $filters) }}" class="inline-flex items-center px-6 py-3 bg-red-600 text-white rounded-lg hover:bg-red-700">
                    <i class="fas fa-file-pdf mr-2"></i>
                    تحميل PDF
                </a>
                <a href="{{ route('reports.sales.index') }}" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white rounded-lg hover:bg-gray-700">
                    <i class="fas fa-arrow-right mr-2"></i>
                    عودة
                </a>
            </div>
        </div>

        <!-- Summary Stats -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">عدد الفواتير</p>
                <p class="text-3xl font-bold text-blue-600">{{ number_format($summary['total_invoices']) }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">إجمالي المبيعات</p>
                <p class="text-3xl font-bold text-green-600">{{ number_format($summary['total_amount'], 0) }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">المدفوع</p>
                <p class="text-3xl font-bold text-emerald-600">{{ number_format($summary['total_paid'], 0) }}</p>
            </div>
            <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                <p class="text-gray-600 text-sm font-medium mb-2">المعلق</p>
                <p class="text-3xl font-bold text-orange-600">{{ number_format($summary['total_remaining'], 0) }}</p>
            </div>
        </div>

        <!-- Filters -->
        <div class="bg-white rounded-xl shadow-lg p-6 mb-8 border border-gray-200">
            <form method="GET" class="grid grid-cols-1 md:grid-cols-2 lg: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="{{ $filters['from_date'] }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none 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="{{ $filters['to_date'] }}" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">العميل</label>
                    <select name="customer_id" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                        <option value="">-- جميع العملاء --</option>
                        @foreach($customers as $customer)
                            <option value="{{ $customer->id }}" {{ $filters['customer_id'] == $customer->id ? 'selected' : '' }}>
                                {{ $customer->name }}
                            </option>
                        @endforeach
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">حالة الفاتورة</label>
                    <select name="status" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                        <option value="">-- جميع الحالات --</option>
                        <option value="draft" {{ $filters['status'] == 'draft' ? 'selected' : '' }}>مسودة</option>
                        <option value="issued" {{ $filters['status'] == 'issued' ? 'selected' : '' }}>صادرة</option>
                        <option value="paid" {{ $filters['status'] == 'paid' ? 'selected' : '' }}>مدفوعة</option>
                        <option value="cancelled" {{ $filters['status'] == 'cancelled' ? 'selected' : '' }}>ملغاة</option>
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">حالة الدفع</label>
                    <select name="payment_status" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                        <option value="">-- جميع الحالات --</option>
                        <option value="paid_fully" {{ $filters['payment_status'] == 'paid_fully' ? 'selected' : '' }}>مدفوع كاملاً</option>
                        <option value="paid_partially" {{ $filters['payment_status'] == 'paid_partially' ? 'selected' : '' }}>مدفوع جزئياً</option>
                        <option value="pending" {{ $filters['payment_status'] == 'pending' ? 'selected' : '' }}>معلق</option>
                    </select>
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">الحد الأدنى للمبلغ</label>
                    <input type="number" name="min_amount" value="{{ $filters['min_amount'] }}" placeholder="0" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                </div>
                <div>
                    <label class="block text-sm font-medium text-gray-700 mb-2">الحد الأقصى للمبلغ</label>
                    <input type="number" name="max_amount" value="{{ $filters['max_amount'] }}" placeholder="0" class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                </div>
                <div class="flex items-end gap-2">
                    <button type="submit" class="flex-1 px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition-colors font-medium">
                        تصفية
                    </button>
                </div>
            </form>
        </div>

        <!-- Invoices Table -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-8">
            <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>
                            <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>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($invoices as $invoice)
                            <tr class="hover:bg-blue-50 transition-colors">
                                <td class="px-6 py-4 font-medium text-gray-900">{{ $invoice['invoice_number'] }}</td>
                                <td class="px-6 py-4 text-gray-700">{{ $invoice['customer_name'] }}</td>
                                <td class="px-6 py-4 text-gray-700">{{ \Carbon\Carbon::parse($invoice['invoice_date'])->format('Y-m-d') }}</td>
                                <td class="px-6 py-4 text-gray-900 font-semibold">{{ number_format($invoice['total_amount'], 2) }}</td>
                                <td class="px-6 py-4 text-emerald-600 font-semibold">{{ number_format($invoice['paid_amount'], 2) }}</td>
                                <td class="px-6 py-4 text-orange-600 font-semibold">{{ number_format($invoice['pending_amount'], 2) }}</td>
                                <td class="px-6 py-4">
                                    <span class="inline-block px-3 py-1 rounded-full text-sm font-medium
                                        {{ $invoice['status'] == 'paid' ? 'bg-emerald-100 text-emerald-800' : '' }}
                                        {{ $invoice['status'] == 'issued' ? 'bg-blue-100 text-blue-800' : '' }}
                                        {{ $invoice['status'] == 'draft' ? 'bg-gray-100 text-gray-800' : '' }}
                                        {{ $invoice['status'] == 'cancelled' ? 'bg-red-100 text-red-800' : '' }}
                                    ">
                                        {{ $invoice['status'] }}
                                    </span>
                                </td>
                                <td class="px-6 py-4">
                                    <span class="inline-block px-3 py-1 rounded-full text-sm font-medium
                                        {{ $invoice['payment_status'] == 'paid_fully' ? 'bg-emerald-100 text-emerald-800' : '' }}
                                        {{ $invoice['payment_status'] == 'paid_partially' ? 'bg-amber-100 text-amber-800' : '' }}
                                        {{ $invoice['payment_status'] == 'pending' ? 'bg-orange-100 text-orange-800' : '' }}
                                    ">
                                        @if($invoice['payment_status'] == 'paid_fully')
                                            مدفوع كاملاً
                                        @elseif($invoice['payment_status'] == 'paid_partially')
                                            مدفوع جزئياً
                                        @else
                                            معلق
                                        @endif
                                    </span>
                                </td>
                                <td class="px-6 py-4 text-center">
                                    <a href="{{ route('reports.sales.invoice-detailed', ['id' => $invoice['id']]) }}" class="inline-flex items-center px-3 py-1 bg-blue-600 text-white rounded hover:bg-blue-700 text-sm">
                                        <i class="fas fa-eye mr-1"></i> عرض
                                    </a>
                                </td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="9" class="px-6 py-8 text-center text-gray-500">
                                    <i class="fas fa-inbox text-4xl mb-4 block opacity-50"></i>
                                    لا توجد فواتير مطابقة للمعايير المحددة
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Pagination Info -->
        @if($pagination)
        <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200 text-center">
            <p class="text-gray-700">
                عرض <strong>{{ number_format(($pagination['current_page'] - 1) * $pagination['per_page'] + 1) }}</strong> 
                إلى <strong>{{ number_format(min($pagination['current_page'] * $pagination['per_page'], $pagination['total'])) }}</strong>
                من <strong>{{ number_format($pagination['total']) }}</strong> فاتورة
            </p>
            @if($pagination['total'] > $pagination['per_page'])
                <p class="text-gray-500 text-sm mt-2">
                    الصفحة {{ $pagination['current_page'] }} من {{ $pagination['last_page'] }}
                </p>
            @endif
        </div>
        @endif
    </div>
</div>
@endsection
