﻿<x-app-layout>
    <x-slot name="header">
        <h2 class="font-semibold text-l text-gray-800 leading-tight">
            {{ __('الفواتير الإلكترونية') }}
        </h2>
    </x-slot>

    <div class="py-12">
        <div class="max-w-7l m-auto sm:p-6 lg:p-8">
            <div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
                <div class="p-6 bg-white border-b border-gray-200">
                    <div class="flex justify-between mb-6">
                        <h3 class="text-lg font-semibold">قائمة الفواتير الإلكترونية</h3>
                        <a href="{{ route('einvoices.create') }}" class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 p-4 rounded">
                            إنشاء فاتورة إلكترونية جديدة
                        </a>
                    </div>

                    @if (session('success'))
                        <div class="bg-green-100 border border-green-400 text-green-700 p-4 py-3 rounded relative mb-4" role="alert">
                            <span class="block sm:inline">{{ session('success') }}</span>
                        </div>
                    @endif

                    @if (session('error'))
                        <div class="bg-red-100 border border-red-400 text-red-700 p-4 py-3 rounded relative mb-4" role="alert">
                            <span class="block sm:inline">{{ session('error') }}</span>
                        </div>
                    @endif

                    <div class="overflow--auto">
                        <table class="min-w-full bg-white">
                            <thead>
                                <tr>
                                    <th class="py-2 p-4 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        رقم الفاتورة
                                    </th>
                                    <th class="py-2 p-4 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        نوع الفاتورة
                                    </th>
                                    <th class="py-2 p-4 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        تاريخ الفاتورة
                                    </th>
                                    <th class="py-2 p-4 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        العميل
                                    </th>
                                    <th class="py-2 p-4 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        المبلغ الإجمالي
                                    </th>
                                    <th class="py-2 p-4 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        الحالة (ZATCA)
                                    </th>
                                    <th class="py-2 p-4 border-b border-gray-200 bg-gray-50 text-right text-s font-medium text-gray-500 uppercase tracking-wider">
                                        الإجراءات
                                    </th>
                                </tr>
                            </thead>
                            <tbody>
                                @forelse ($einvoices as $einvoice)
                                    <tr>
                                        <td class="py-2 p-4 border-b border-gray-200">
                                            {{ $einvoice->invoice_number }}
                                        </td>
                                        <td class="py-2 p-4 border-b border-gray-200">
                                            {{ $einvoice->getCategoryLabel() }}
                                        </td>
                                        <td class="py-2 p-4 border-b border-gray-200">
                                            {{ $einvoice->invoice_date->format('Y-m-d') }}
                                        </td>
                                        <td class="py-2 p-4 border-b border-gray-200">
                                            {{ $einvoice->customer_name }}
                                        </td>
                                        <td class="py-2 p-4 border-b border-gray-200 font-bold">
                                            {{ number_format($einvoice->total_amount, 2) }} ريال
                                        </td>
                                        <td class="py-2 p-4 border-b border-gray-200">
                                            @if($einvoice->zatca_compliance_status == 'PASS' || $einvoice->zatca_compliance_status == 'REPORTED' || $einvoice->zatca_compliance_status == 'CLEARED' || $einvoice->zatca_compliance_status == 'compliant')
                                                <span class="px-2 py-1 inline-flex text-xs leading-5 font-semibold rounded-full bg-green-100 text-green-800">
                                                    ✅ مرسلة ومقبولة
                                                </span>
                                            @elseif($einvoice->zatca_compliance_status == 'ERROR' || $einvoice->zatca_compliance_status == 'FAILED')
                                                <span class="px-2 py-1 inline-flex text-xs leading-5 font-semibold rounded-full bg-red-100 text-red-800">
                                                    ❌ مرفوضة
                                                </span>
                                            @else
                                                <span class="px-2 py-1 inline-flex text-xs leading-5 font-semibold rounded-full bg-yellow-100 text-yellow-800">
                                                    ⏳ غير مرسلة
                                                </span>
                                            @endif
                                        </td>
                                        <td class="py-2 p-4 border-b border-gray-200">
                                            <div class="flex space-x-3 space-x-reverse">
                                                @if($einvoice->zatca_compliance_status != 'PASS' && $einvoice->zatca_compliance_status != 'REPORTED' && $einvoice->zatca_compliance_status != 'CLEARED' && $einvoice->zatca_compliance_status != 'compliant')
                                                    <form action="{{ route('zatca.submission.invoice', $einvoice->id) }}" method="POST" onsubmit="return confirm('هل أنت متأكد من إرسال هذه الفاتورة لهيئة الزكاة والدخل؟')">
                                                        @csrf
                                                        <button type="submit" class="bg-blue-600 hover:bg-blue-800 text-white text-xs font-bold py-1 px-3 rounded shadow">
                                                            🚀 إرسال الآن
                                                        </button>
                                                    </form>
                                                @endif
                                                <a href="{{ route('einvoices.show', $einvoice->id) }}" class="text-gray-600 hover:text-blue-900 flex items-center">
                                                    <i class="fas fa-eye ml-1"></i> عرض
                                                </a>
                                                <a href="{{ route('einvoices.print', $einvoice->id) }}" class="text-gray-600 hover:text-green-900 flex items-center" target="_blank">
                                                    <i class="fas fa-print ml-1"></i> طباعة
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                @empty
                                    <tr>
                                        <td colspan="7" class="py-4 p-4 border-b border-gray-200 text-center text-gray-500">
                                            لا توجد فواتير إلكترونية
                                        </td>
                                    </tr>
                                @endforelse
                            </tbody>
                        </table>
                    </div>

                    <div class="mt-4">
                        {{ $einvoices->links() }}
                    </div>
                </div>
            </div>
        </div>
    </div>
</x-app-layout>