{{-- The accounts that need a decision. RTL: logical properties only. --}}
<div class="max-w-7xl mx-auto px-3 sm:px-4 py-4 sm:py-6" dir="rtl">

    <div class="mb-4 sm:mb-6 flex flex-wrap items-start justify-between gap-3">
        <div>
            <h1 class="text-xl sm:text-2xl font-bold text-gray-900">{{ __('حالات تحتاج تسوية') }}</h1>
            <p class="mt-1 text-sm text-gray-500">
                {{ __('حسابات لا تتفق أرقامها مع الواقع: مال حُصِّل ولم يُسجَّل، شهور بلا فواتير، منتجات بيعت خارج السيستم.') }}
                <span class="block mt-0.5">{{ __('الشهر الجاري لا يُحتسب هنا — تحصيله من شاشة «تحصيل دفعة». وكذلك الأقساط المنتظمة حتى الشهر الجاري.') }}</span>
            </p>
        </div>
        <button type="button" wire:click="export"
                class="inline-flex items-center gap-2 px-3 py-2 border border-gray-300 text-gray-700 text-sm rounded-lg hover:bg-gray-50">
            {{ __('تصدير Excel') }}
        </button>
    </div>

    {{-- Headline --}}
    <div class="grid grid-cols-2 sm:grid-cols-4 gap-3 mb-5">
        <div class="p-3 bg-white border border-gray-200 rounded-xl">
            <p class="text-[11px] text-gray-500">{{ __('حسابات مرصودة') }}</p>
            <p class="text-xl font-bold text-gray-900">{{ count($rows) }}</p>
        </div>
        <div class="p-3 bg-white border border-gray-200 rounded-xl">
            <p class="text-[11px] text-gray-500">{{ __('إجمالي المستحق عليها') }}</p>
            <p class="text-xl font-bold text-red-700 tabular-nums" dir="ltr">{{ format_money($totalOwed) }}</p>
        </div>
        <div class="p-3 bg-white border border-gray-200 rounded-xl col-span-2">
            <p class="text-[11px] text-gray-500 mb-1">{{ __('بحث') }}</p>
            <input type="text" wire:model.live.debounce.400ms="search" placeholder="{{ __('اسم أو هاتف...') }}"
                   class="w-full px-3 py-1.5 border border-gray-300 rounded-lg text-sm">
        </div>
    </div>

    {{-- Case filters --}}
    <div class="flex flex-wrap gap-2 mb-4">
        <button type="button" wire:click="clearFilter"
                class="px-3 py-1.5 text-xs rounded-full border {{ $caseFilter === '' ? 'bg-gray-900 text-white border-gray-900' : 'bg-white text-gray-600 border-gray-300 hover:bg-gray-50' }}">
            {{ __('الكل') }} ({{ count($rows) }})
        </button>
        @foreach($cases as $code => $case)
            @if(!empty($counts[$code]) || $caseFilter === $code)
                <button type="button" wire:click="$set('caseFilter', '{{ $code }}')"
                        class="px-3 py-1.5 text-xs rounded-full border {{ $caseFilter === $code ? 'bg-amber-500 text-white border-amber-500' : 'bg-white text-gray-600 border-gray-300 hover:bg-amber-50' }}">
                    {{ $case['label'] }} ({{ $counts[$code] ?? 0 }})
                </button>
            @endif
        @endforeach
    </div>

    @if($caseFilter && isset($cases[$caseFilter]))
        <div class="mb-4 p-3 bg-amber-50 border border-amber-200 rounded-lg text-xs text-amber-900">
            {{ $cases[$caseFilter]['hint'] }}
        </div>
    @endif

    {{-- The list --}}
    <div class="bg-white border border-gray-200 rounded-xl overflow-hidden">
        <div class="overflow-x-auto">
            <table class="w-full text-sm">
                <thead class="bg-gray-50 text-xs text-gray-600">
                    <tr>
                        <th class="px-3 py-2.5 text-start font-medium">{{ __('المشترك') }}</th>
                        <th class="px-3 py-2.5 text-start font-medium">{{ __('البرنامج') }}</th>
                        <th class="px-3 py-2.5 text-start font-medium">{{ __('ما الذي رُصد') }}</th>
                        <th class="px-3 py-2.5 text-center font-medium">{{ __('غير مسددة') }}</th>
                        <th class="px-3 py-2.5 text-center font-medium">{{ __('المستحق') }}</th>
                        <th class="px-3 py-2.5 text-center font-medium">{{ __('المحصَّل') }}</th>
                        <th class="px-3 py-2.5 text-center font-medium"></th>
                    </tr>
                </thead>
                <tbody class="divide-y divide-gray-100">
                    @forelse($rows as $row)
                        @php $participant = $row['participant']; @endphp
                        <tr class="hover:bg-gray-50 transition">
                            <td class="px-3 py-2.5">
                                <span class="block font-medium text-gray-900">{{ $participant->person?->name_ar }}</span>
                                @if($participant->person?->phone)
                                    <span class="block text-xs text-gray-500" dir="ltr">{{ $participant->person->phone }}</span>
                                @endif
                            </td>
                            <td class="px-3 py-2.5 text-xs text-gray-600">{{ $participant->enrollments->first()?->program?->name_ar ?? '—' }}</td>
                            <td class="px-3 py-2.5">
                                <div class="flex flex-wrap gap-1">
                                    @foreach($row['cases'] as $case)
                                        {{-- Severity is the point of the list; a duplicate billing and a
                                             missing month reading identically is what made the operator
                                             work top-to-bottom instead of worst-first. --}}
                                        <span class="px-1.5 py-0.5 text-[10px] rounded-full
                                            {{ ($cases[$case]['severity'] ?? 9) <= 2 ? 'bg-rose-100 text-rose-800' : 'bg-amber-100 text-amber-800' }}">
                                            {{ $cases[$case]['label'] }}
                                        </span>
                                    @endforeach
                                </div>
                                @if(!empty($row['detail']['over_billed_bundle']))
                                    {{-- The figure is the reason this row is here; making the operator
                                         open the wizard to find out how much is a wasted click. --}}
                                    @foreach($row['detail']['over_billed_bundle'] as $b)
                                        <p class="mt-1 text-[11px] text-rose-700">
                                            {{ \Illuminate\Support\Str::limit($b['product_name'], 26) }}:
                                            {{ __('محاسَب') }} <span dir="ltr">{{ format_money($b['billed']) }}</span>
                                            {{ __('وسعره') }} <span dir="ltr">{{ format_money($b['price']) }}</span> —
                                            <span class="font-bold">{{ __('زيادة') }} <span dir="ltr">{{ format_money($b['over_billed']) }}</span></span>
                                        </p>
                                    @endforeach
                                @endif
                            </td>
                            <td class="px-3 py-2.5 text-center tabular-nums">{{ $row['unpaid_invoices'] }}</td>
                            <td class="px-3 py-2.5 text-center tabular-nums font-bold {{ $row['owed'] > 0 ? 'text-red-700' : 'text-gray-400' }}" dir="ltr">
                                {{ format_money($row['owed']) }}
                            </td>
                            <td class="px-3 py-2.5 text-center tabular-nums text-emerald-700" dir="ltr">{{ format_money($row['paid']) }}</td>
                            <td class="px-3 py-2.5 text-center">
                                <a href="{{ route('admin.account-settlement', ['participant' => $participant->id]) }}" wire:navigate
                                   class="inline-flex px-3 py-1.5 bg-amber-500 text-white text-xs font-medium rounded-lg hover:bg-amber-600">
                                    {{ __('تسوية') }}
                                </a>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="px-3 py-10 text-center">
                                <p class="text-sm text-gray-500">{{ __('لا توجد حالات مرصودة') }}</p>
                                <p class="text-xs text-gray-400 mt-1">{{ __('كل الحسابات في هذا الفرع متسقة — أو غيّر الفلتر.') }}</p>
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>
</div>
