<!DOCTYPE html>
<html dir="rtl" lang="ar">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>فاتورة {{ $invoice->number }}</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <link href="https://fonts.googleapis.com/css2?family=Cairo:wght@400;600;700&display=swap" rel="stylesheet">
    <style>
        body { font-family: 'Cairo', sans-serif; }
        @media print {
            .no-print { display: none; }
            body { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
        }
    </style>
</head>
<body class="bg-gray-50 min-h-screen py-8">
    <div class="max-w-3xl mx-auto bg-white shadow-lg rounded-lg overflow-hidden">
        {{-- Header --}}
        <div class="bg-blue-700 text-white px-8 py-6">
            <div class="flex justify-between items-center">
                <div>
                    <h1 class="text-2xl font-bold">فاتورة</h1>
                    <p class="text-blue-100 mt-1">{{ $invoice->number }}</p>
                </div>
                <div class="text-end">
                    <p class="text-lg font-semibold">{{ $invoice->academy?->name_ar ?? $invoice->academy?->name ?? '' }}</p>
                </div>
            </div>
        </div>

        {{-- Invoice Details --}}
        <div class="px-8 py-6 border-b border-gray-200">
            <div class="grid grid-cols-2 gap-6">
                <div>
                    <h3 class="text-sm text-gray-500 mb-1">العميل</h3>
                    <p class="font-semibold text-gray-800">
                        {{ $invoice->contact_name ?? $invoice->billable?->person?->name_ar ?? $invoice->billable?->person?->name ?? '-' }}
                    </p>
                    @if($invoice->contact_phone)
                        <p class="text-sm text-gray-600" dir="ltr">{{ $invoice->contact_phone }}</p>
                    @endif
                </div>
                <div class="text-start">
                    <div class="space-y-2">
                        <div>
                            <span class="text-sm text-gray-500">تاريخ الإصدار:</span>
                            <span class="font-medium text-gray-800 ms-2">{{ $invoice->issue_date?->format('Y-m-d') ?? '-' }}</span>
                        </div>
                        <div>
                            <span class="text-sm text-gray-500">تاريخ الاستحقاق:</span>
                            <span class="font-medium text-gray-800 ms-2">{{ $invoice->due_date?->format('Y-m-d') ?? '-' }}</span>
                        </div>
                        <div>
                            <span class="text-sm text-gray-500">الحالة:</span>
                            @php
                                $statusColors = [
                                    'draft' => 'bg-gray-100 text-gray-700',
                                    'pending' => 'bg-yellow-100 text-yellow-700',
                                    'sent' => 'bg-blue-100 text-blue-700',
                                    'paid' => 'bg-green-100 text-green-700',
                                    'partially_paid' => 'bg-orange-100 text-orange-700',
                                    'overdue' => 'bg-red-100 text-red-700',
                                    'cancelled' => 'bg-gray-100 text-gray-500',
                                ];
                                $statusLabels = [
                                    'draft' => 'مسودة',
                                    'pending' => 'معلقة',
                                    'awaiting_approval' => 'بانتظار الموافقة',
                                    'sent' => 'مرسلة',
                                    'paid' => 'مدفوعة',
                                    'partially_paid' => 'مدفوعة جزئياً',
                                    'overpaid' => 'مدفوعة بزيادة',
                                    'overdue' => 'متأخرة',
                                    'cancelled' => 'ملغاة',
                                    'refunded' => 'مستردة',
                                    'partially_refunded' => 'مستردة جزئياً',
                                    'written_off' => 'مشطوبة',
                                    'disputed' => 'متنازع عليها',
                                ];
                                $statusValue = $invoice->status?->value ?? $invoice->status ?? '';
                                $colorClass = $statusColors[$statusValue] ?? 'bg-gray-100 text-gray-700';
                                $label = $statusLabels[$statusValue] ?? $statusValue;
                            @endphp
                            <span class="inline-block px-3 py-1 rounded-full text-xs font-semibold {{ $colorClass }} ms-2">
                                {{ $label }}
                            </span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        {{-- Items Table --}}
        <div class="px-8 py-6">
            <table class="w-full">
                <thead>
                    <tr class="border-b-2 border-gray-200">
                        <th class="text-start py-3 text-sm font-semibold text-gray-600">البند</th>
                        <th class="text-center py-3 text-sm font-semibold text-gray-600">الكمية</th>
                        <th class="text-start py-3 text-sm font-semibold text-gray-600">سعر الوحدة</th>
                        <th class="text-start py-3 text-sm font-semibold text-gray-600">الإجمالي</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($invoice->items as $item)
                        <tr class="border-b border-gray-100">
                            <td class="py-3 text-gray-800">{{ $item->description }}</td>
                            <td class="py-3 text-center text-gray-600">{{ $item->quantity }}</td>
                            <td class="py-3 text-gray-600" dir="ltr">{{ number_format($item->unit_price / 100, 2) }} ج.م</td>
                            <td class="py-3 font-medium text-gray-800" dir="ltr">{{ number_format($item->total_amount / 100, 2) }} ج.م</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        {{-- Totals --}}
        <div class="px-8 py-6 bg-gray-50 border-t border-gray-200">
            <div class="flex justify-start">
                <div class="w-72 space-y-2">
                    <div class="flex justify-between text-gray-600">
                        <span>المجموع الفرعي</span>
                        <span dir="ltr">{{ number_format($invoice->subtotal_amount / 100, 2) }} ج.م</span>
                    </div>
                    @if($invoice->discount_amount > 0)
                        <div class="flex justify-between text-green-600">
                            <span>الخصم</span>
                            <span dir="ltr">- {{ number_format($invoice->discount_amount / 100, 2) }} ج.م</span>
                        </div>
                    @endif
                    @if($invoice->tax_amount > 0)
                        <div class="flex justify-between text-gray-600">
                            <span>الضريبة</span>
                            <span dir="ltr">{{ number_format($invoice->tax_amount / 100, 2) }} ج.م</span>
                        </div>
                    @endif
                    <div class="flex justify-between font-bold text-lg text-gray-800 border-t border-gray-300 pt-2">
                        <span>الإجمالي</span>
                        <span dir="ltr">{{ number_format($invoice->total_amount / 100, 2) }} ج.م</span>
                    </div>
                    @if($invoice->paid_amount > 0)
                        <div class="flex justify-between text-blue-600">
                            <span>المدفوع</span>
                            <span dir="ltr">{{ number_format($invoice->paid_amount / 100, 2) }} ج.م</span>
                        </div>
                    @endif
                    @if($invoice->due_amount > 0)
                        <div class="flex justify-between text-red-600 font-semibold">
                            <span>المتبقي</span>
                            <span dir="ltr">{{ number_format($invoice->due_amount / 100, 2) }} ج.م</span>
                        </div>
                    @endif
                </div>
            </div>
        </div>

        {{-- Notes --}}
        @if($invoice->notes)
            <div class="px-8 py-4 border-t border-gray-200">
                <p class="text-sm text-gray-500">ملاحظات:</p>
                <p class="text-gray-700 mt-1">{{ $invoice->notes }}</p>
            </div>
        @endif

        {{-- Footer --}}
        <div class="px-8 py-4 bg-gray-100 text-center">
            <p class="text-xs text-gray-500">تم إنشاء هذا المستند إلكترونياً</p>
            @if($invoice->creator)
                <p class="text-xs text-gray-400 mt-1">أنشأ بواسطة: {{ $invoice->creator->name }}</p>
            @endif
        </div>
    </div>

    {{-- Print Button --}}
    <div class="max-w-3xl mx-auto mt-4 text-center no-print">
        <button onclick="window.print()" class="bg-blue-600 text-white px-6 py-2 rounded-lg hover:bg-blue-700 transition">
            طباعة
        </button>
    </div>
</body>
</html>
