@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">{{ $invoice['invoice_number'] ?? 'الفاتورة' }}</p>
            </div>
            <div class="flex gap-3">
                <a href="{{ route('reports.sales.invoice-detailed-pdf', ['id' => request('id') ?? $invoice['id']]) }}" 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>
                <button onclick="window.print()" class="inline-flex items-center px-6 py-3 bg-gray-600 text-white rounded-lg hover:bg-gray-700">
                    <i class="fas fa-print mr-2"></i>
                    طباعة
                </button>
                <a href="{{ route('reports.sales.invoice-list-detailed') }}" 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>

        <!-- Invoice Header Info -->
        <div class="grid grid-cols-1 md:grid-cols-2 gap-8 mb-8">
            <!-- Left: Invoice & Customer Info -->
            <div class="space-y-6">
                <!-- Invoice Info -->
                <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                    <h2 class="text-lg font-bold text-gray-900 mb-4">معلومات الفاتورة</h2>
                    <div class="space-y-3">
                        <div class="flex justify-between">
                            <span class="text-gray-600">رقم الفاتورة:</span>
                            <span class="font-semibold text-gray-900">{{ $invoice['invoice_number'] ?? '-' }}</span>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-gray-600">التاريخ:</span>
                            <span class="font-semibold text-gray-900">{{ isset($invoice['invoice_date']) ? \Carbon\Carbon::parse($invoice['invoice_date'])->format('Y-m-d') : '-' }}</span>
                        </div>
                        <div class="flex justify-between">
                            <span class="text-gray-600">الحالة:</span>
                            <span class="inline-block px-3 py-1 rounded-full text-sm font-medium
                                {{ $invoice['status'] == 'paid' ? 'bg-green-100 text-green-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>
                        </div>
                    </div>
                </div>

                <!-- Customer Info -->
                <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                    <h2 class="text-lg font-bold text-gray-900 mb-4">بيانات العميل</h2>
                    <div class="space-y-3">
                        <div>
                            <span class="text-gray-600 text-sm">الاسم:</span>
                            <p class="font-semibold text-gray-900">{{ $invoice['customer_name'] ?? '-' }}</p>
                        </div>
                        <div>
                            <span class="text-gray-600 text-sm">رقم العميل:</span>
                            <p class="font-semibold text-gray-900">{{ $invoice['customer_code'] ?? '-' }}</p>
                        </div>
                        <div>
                            <span class="text-gray-600 text-sm">البريد الإلكتروني:</span>
                            <p class="font-semibold text-gray-900">{{ $invoice['customer_email'] ?? '-' }}</p>
                        </div>
                        <div>
                            <span class="text-gray-600 text-sm">الهاتف:</span>
                            <p class="font-semibold text-gray-900">{{ $invoice['customer_phone'] ?? '-' }}</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Right: Financial Summary -->
            <div>
                <div class="bg-white rounded-xl shadow-lg p-6 border border-gray-200">
                    <h2 class="text-lg font-bold text-gray-900 mb-6">الملخص المالي</h2>
                    <div class="space-y-4">
                        <div class="flex justify-between p-3 bg-gray-50 rounded">
                            <span class="text-gray-600">المجموع الفرعي:</span>
                            <span class="font-semibold text-gray-900">{{ number_format($invoice['subtotal'] ?? 0, 2) }} ريال</span>
                        </div>
                        <div class="flex justify-between p-3 bg-gray-50 rounded">
                            <span class="text-gray-600">الخصم:</span>
                            <span class="font-semibold text-orange-600">{{ number_format($invoice['discount_amount'] ?? 0, 2) }} ريال</span>
                        </div>
                        <div class="flex justify-between p-3 bg-gray-50 rounded">
                            <span class="text-gray-600">الضريبة:</span>
                            <span class="font-semibold text-purple-600">{{ number_format($invoice['tax_amount'] ?? 0, 2) }} ريال</span>
                        </div>
                        <div class="flex justify-between p-4 bg-blue-100 rounded-lg border-2 border-blue-300">
                            <span class="text-blue-900 font-bold">الإجمالي:</span>
                            <span class="font-bold text-blue-900 text-xl">{{ number_format($invoice['total_amount'] ?? 0, 2) }} ريال</span>
                        </div>
                        <div class="flex justify-between p-3 bg-gray-50 rounded mt-4">
                            <span class="text-gray-600">المدفوع:</span>
                            <span class="font-semibold text-green-600">{{ number_format($invoice['paid_amount'] ?? 0, 2) }} ريال</span>
                        </div>
                        <div class="flex justify-between p-4 bg-orange-100 rounded-lg border-2 border-orange-300">
                            <span class="text-orange-900 font-bold">المتبقي:</span>
                            <span class="font-bold text-orange-900 text-xl">{{ number_format(($invoice['total_amount'] ?? 0) - ($invoice['paid_amount'] ?? 0), 2) }} ريال</span>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- Line Items Table -->
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-8">
            <div class="px-6 py-4 bg-gradient-to-r from-gray-800 to-gray-900 text-white border-b border-gray-200">
                <h2 class="text-lg font-semibold">بنود الفاتورة</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50 border-b border-gray-200">
                        <tr>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">البند</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">الوصف</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">الكمية</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">السعر الوحدة</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">الخصم %</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">المبلغ</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($invoice['line_items'] ?? [] as $item)
                            <tr class="hover:bg-blue-50 transition-colors">
                                <td class="px-6 py-3 font-medium text-gray-900">{{ $item['line_number'] ?? '-' }}</td>
                                <td class="px-6 py-3 text-gray-700">{{ $item['description'] ?? '-' }}</td>
                                <td class="px-6 py-3 text-gray-700">{{ number_format($item['quantity'] ?? 0, 2) }}</td>
                                <td class="px-6 py-3 text-gray-700">{{ number_format($item['unit_price'] ?? 0, 2) }}</td>
                                <td class="px-6 py-3 text-gray-700">{{ $item['discount_percentage'] ?? 0 }}%</td>
                                <td class="px-6 py-3 font-semibold text-gray-900">{{ number_format($item['line_total'] ?? 0, 2) }}</td>
                            </tr>
                        @empty
                            <tr>
                                <td colspan="6" class="px-6 py-8 text-center text-gray-500">
                                    لا توجد بنود لهذه الفاتورة
                                </td>
                            </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        <!-- Payment History -->
        @if(count($invoice['payments'] ?? []) > 0)
        <div class="bg-white rounded-xl shadow-lg overflow-hidden border border-gray-200 mb-8">
            <div class="px-6 py-4 bg-gradient-to-r from-green-800 to-green-900 text-white border-b border-gray-200">
                <h2 class="text-lg font-semibold">سجل الدفعات</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50 border-b border-gray-200">
                        <tr>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">تاريخ الدفع</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">طريقة الدفع</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">المرجع</th>
                            <th class="px-6 py-3 text-right font-semibold text-sm text-gray-700">المبلغ</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @foreach($invoice['payments'] as $payment)
                            <tr class="hover:bg-green-50 transition-colors">
                                <td class="px-6 py-3 text-gray-700">{{ isset($payment['payment_date']) ? \Carbon\Carbon::parse($payment['payment_date'])->format('Y-m-d') : '-' }}</td>
                                <td class="px-6 py-3 text-gray-700">{{ $payment['payment_method'] ?? '-' }}</td>
                                <td class="px-6 py-3 text-gray-700">{{ $payment['reference_number'] ?? '-' }}</td>
                                <td class="px-6 py-3 font-semibold text-green-600">{{ number_format($payment['amount'] ?? 0, 2) }}</td>
                            </tr>
                        @endforeach
                    </tbody>
                </table>
            </div>
        </div>
        @endif
    </div>
</div>
@endsection
