@extends('layouts.app')

@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-4xl font-bold text-gray-900 mb-2">بيان المورد</h1>
                    <p class="text-gray-600">تفاصيل الفواتير والدفعات للمورد</p>
                </div>
                <div class="flex gap-3">
                    <button onclick="window.print()" class="inline-flex items-center px-4 py-2 bg-blue-600 text-white rounded-lg hover:bg-blue-700 transition">
                        <i class="fas fa-print ml-2"></i>
                        <span>طباعة</span>
                    </button>
                    <a href="?export=true&format=pdf" class="inline-flex items-center px-4 py-2 bg-red-600 text-white rounded-lg hover:bg-red-700 transition">
                        <i class="fas fa-file-pdf ml-2"></i>
                        <span>PDF</span>
                    </a>
                    <a href="?export=true&format=excel" class="inline-flex items-center px-4 py-2 bg-green-600 text-white rounded-lg hover:bg-green-700 transition">
                        <i class="fas fa-file-excel ml-2"></i>
                        <span>Excel</span>
                    </a>
                </div>
            </div>

            {{-- رسائل التنبيه --}}
            @if(session('success'))
            <div class="bg-green-50 border-l-4 border-green-500 p-4 mb-6 rounded-r-lg">
                <p class="text-green-700 font-medium">{{ session('success') }}</p>
            </div>
            @endif

            @if(session('error'))
            <div class="bg-red-50 border-l-4 border-red-500 p-4 mb-6 rounded-r-lg">
                <p class="text-red-700 font-medium">{{ session('error') }}</p>
            </div>
            @endif
        </div>

        {{-- معلومات المورد --}}
        @if($data['supplier'])
        <div class="bg-white rounded-xl shadow-lg p-8 mb-8 border border-gray-200">
            <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
                <div class="border-l-4 border-blue-500 pl-4">
                    <p class="text-gray-600 text-sm mb-1">اسم المورد</p>
                    <p class="text-2xl font-bold text-gray-900">{{ $data['supplier']->name ?? 'N/A' }}</p>
                </div>
                <div class="border-l-4 border-blue-500 pl-4">
                    <p class="text-gray-600 text-sm mb-1">المدينة</p>
                    <p class="text-lg text-gray-700">{{ $data['supplier']->city ?? 'N/A' }}</p>
                </div>
                <div class="border-l-4 border-blue-500 pl-4">
                    <p class="text-gray-600 text-sm mb-1">البريد الإلكتروني</p>
                    <p class="text-lg text-gray-700">{{ $data['supplier']->email ?? 'N/A' }}</p>
                </div>
                <div class="border-l-4 border-blue-500 pl-4">
                    <p class="text-gray-600 text-sm mb-1">الهاتف</p>
                    <p class="text-lg text-gray-700">{{ $data['supplier']->phone ?? 'N/A' }}</p>
                </div>
            </div>
        </div>
        @endif

        {{-- ملخص المالي --}}
        <div class="grid grid-cols-1 md:grid-cols-3 gap-6 mb-8">
            {{-- إجمالي الفواتير --}}
            <div class="bg-white rounded-xl shadow-lg p-6 border-t-4 border-blue-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm mb-1">إجمالي المشتريات</p>
                        <p class="text-3xl font-bold text-gray-900">{{ number_format($data['total_invoiced'], 2) }}</p>
                    </div>
                    <div class="bg-blue-100 rounded-full p-3">
                        <i class="fas fa-file-invoice text-blue-600 text-2xl"></i>
                    </div>
                </div>
            </div>

            {{-- إجمالي المدفوع --}}
            <div class="bg-white rounded-xl shadow-lg p-6 border-t-4 border-green-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm mb-1">المبلغ المدفوع</p>
                        <p class="text-3xl font-bold text-green-600">{{ number_format($data['total_paid'], 2) }}</p>
                    </div>
                    <div class="bg-green-100 rounded-full p-3">
                        <i class="fas fa-check-circle text-green-600 text-2xl"></i>
                    </div>
                </div>
            </div>

            {{-- المتبقي --}}
            <div class="bg-white rounded-xl shadow-lg p-6 border-t-4 border-red-500">
                <div class="flex items-center justify-between">
                    <div>
                        <p class="text-gray-600 text-sm mb-1">المتبقي</p>
                        <p class="text-3xl font-bold text-red-600">{{ number_format($data['total_due'], 2) }}</p>
                    </div>
                    <div class="bg-red-100 rounded-full p-3">
                        <i class="fas fa-exclamation-circle text-red-600 text-2xl"></i>
                    </div>
                </div>
            </div>
        </div>

        {{-- جدول الفواتير --}}
        <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-700">
                <h2 class="text-xl font-bold">الفواتير</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">رقم الفاتورة</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">التاريخ</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">المبلغ الإجمالي</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">المبلغ المدفوع</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">المتبقي</th>
                            <th class="px-6 py-4 text-center font-semibold text-sm text-gray-700">الحالة</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @forelse($data['invoices'] ?? [] as $invoice)
                        <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">
                                <div class="font-semibold text-gray-900">{{ $invoice->invoice_number ?? 'N/A' }}</div>
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ $invoice->invoice_date ? $invoice->invoice_date->format('Y-m-d') : 'N/A' }}
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                {{ number_format($invoice->total_amount, 2) }}
                            </td>
                            <td class="px-6 py-4 text-gray-700">
                                <span class="text-green-600 font-semibold">{{ number_format($invoice->paid_amount ?? 0, 2) }}</span>
                            </td>
                            <td class="px-6 py-4">
                                @php
                                    $remaining = ($invoice->total_amount ?? 0) - ($invoice->paid_amount ?? 0);
                                @endphp
                                <span class="text-{{ $remaining > 0 ? 'red' : 'green' }}-600 font-semibold">
                                    {{ number_format($remaining, 2) }}
                                </span>
                            </td>
                            <td class="px-6 py-4 text-center">
                                @php
                                    $status = $invoice->status ?? 'unknown';
                                    $statusText = [
                                        'draft' => 'مسودة',
                                        'sent' => 'مرسلة',
                                        'approved' => 'معتمدة',
                                        'paid' => 'مدفوعة',
                                        'partial' => 'مدفوعة جزئياً',
                                        'cancelled' => 'ملغاة'
                                    ][$status] ?? $status;
                                    $badgeColor = [
                                        'draft' => 'gray',
                                        'sent' => 'blue',
                                        'approved' => 'yellow',
                                        'paid' => 'green',
                                        'partial' => 'amber',
                                        'cancelled' => 'red'
                                    ][$status] ?? 'gray';
                                @endphp
                                <span class="inline-flex items-center px-3 py-1 rounded-full text-sm font-medium bg-{{ $badgeColor }}-100 text-{{ $badgeColor }}-700">
                                    {{ $statusText }}
                                </span>
                            </td>
                        </tr>
                        @empty
                        <tr>
                            <td colspan="7" 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>
                                </div>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>

        {{-- جدول الدفعات (إن وجدت) --}}
        @php
            $hasPayments = false;
            if (isset($data['invoices'])) {
                foreach ($data['invoices'] as $inv) {
                    if ($inv->payments && $inv->payments->count() > 0) {
                        $hasPayments = true;
                        break;
                    }
                }
            }
        @endphp
        @if($hasPayments)
        <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-700">
                <h2 class="text-xl font-bold">الدفعات</h2>
            </div>
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-gray-50">
                        <tr>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">#</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">رقم الفاتورة</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">تاريخ الدفع</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">المبلغ</th>
                            <th class="px-6 py-4 text-right font-semibold text-sm text-gray-700">الطريقة</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-gray-200">
                        @php
                            $paymentIndex = 0;
                        @endphp
                        @forelse($data['invoices'] ?? [] as $invoice)
                            @forelse($invoice->payments ?? [] as $payment)
                            @php $paymentIndex++; @endphp
                            <tr class="hover:bg-blue-50 transition-colors duration-150">
                                <td class="px-6 py-4 text-gray-600 font-medium">{{ $paymentIndex }}</td>
                                <td class="px-6 py-4 font-semibold text-gray-900">{{ $invoice->invoice_number ?? 'N/A' }}</td>
                                <td class="px-6 py-4 text-gray-700">
                                    {{ $payment->payment_date ? $payment->payment_date->format('Y-m-d') : 'N/A' }}
                                </td>
                                <td class="px-6 py-4 text-green-600 font-semibold">
                                    {{ number_format($payment->amount ?? 0, 2) }}
                                </td>
                                <td class="px-6 py-4 text-gray-700">
                                    {{ $payment->payment_method ?? 'N/A' }}
                                </td>
                            </tr>
                            @empty
                            @endforelse
                        @empty
                        <tr>
                            <td colspan="5" class="px-6 py-12 text-center">
                                <p class="text-gray-600 font-medium">لا توجد دفعات</p>
                            </td>
                        </tr>
                        @endforelse
                    </tbody>
                </table>
            </div>
        </div>
        @endif

        {{-- ملخص نهائي --}}
        <div class="bg-gradient-to-r from-blue-50 to-indigo-50 rounded-xl p-8 border border-blue-200">
            <h3 class="text-xl font-bold text-gray-900 mb-6">ملخص الحساب</h3>
            <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                <div class="flex justify-between items-center pb-4 border-b border-blue-200">
                    <span class="text-gray-700 font-medium">إجمالي المشتريات</span>
                    <span class="text-2xl font-bold text-gray-900">{{ number_format($data['total_invoiced'], 2) }}</span>
                </div>
                <div class="flex justify-between items-center pb-4 border-b border-blue-200">
                    <span class="text-gray-700 font-medium">عدد الفواتير</span>
                    <span class="text-2xl font-bold text-gray-900">{{ count($data['invoices'] ?? []) }}</span>
                </div>
                <div class="flex justify-between items-center pb-4 border-b border-green-200">
                    <span class="text-gray-700 font-medium">المبلغ المدفوع</span>
                    <span class="text-2xl font-bold text-green-600">{{ number_format($data['total_paid'], 2) }}</span>
                </div>
                <div class="flex justify-between items-center pb-4 border-b border-green-200">
                    <span class="text-gray-700 font-medium">نسبة السداد</span>
                    <span class="text-2xl font-bold text-green-600">
                        {{ $data['total_invoiced'] > 0 ? number_format(($data['total_paid'] / $data['total_invoiced']) * 100, 1) : 0 }}%
                    </span>
                </div>
                <div class="flex justify-between items-center pb-4 border-b border-red-200">
                    <span class="text-gray-700 font-medium">المتبقي</span>
                    <span class="text-2xl font-bold text-red-600">{{ number_format($data['total_due'], 2) }}</span>
                </div>
                <div class="flex justify-between items-center pb-4 border-b border-red-200">
                    <span class="text-gray-700 font-medium">نسبة المتبقي</span>
                    <span class="text-2xl font-bold text-red-600">
                        {{ $data['total_invoiced'] > 0 ? number_format(($data['total_due'] / $data['total_invoiced']) * 100, 1) : 0 }}%
                    </span>
                </div>
            </div>
        </div>

        {{-- تاريخ التقرير --}}
        <div class="text-center mt-8 text-gray-600 text-sm">
            <p>تم إنشاء التقرير في {{ now()->format('Y-m-d H:i') }}</p>
        </div>
    </div>
</div>

@push('styles')
<style>
    @media print {
        .no-print { display: none !important; }
        .bg-gradient-to-br { background: white !important; }
    }
</style>
@endpush
@endsection