<x-filament-panels::page>
    {{ $this->form }}

    <div class="grid gap-4 lg:grid-cols-3">
        <x-filament::section>
            <x-slot name="heading">{{ __('dberp.resources.customer_credit_control.stat_customer_count') }}</x-slot>
            <div class="text-2xl font-bold text-gray-950 dark:text-white">
                {{ $totals['customer_count'] ?? 0 }}
            </div>
        </x-filament::section>
        <x-filament::section>
            <x-slot name="heading">{{ __('dberp.resources.customer_credit_control.stat_blocked_count') }}</x-slot>
            <div class="text-2xl font-bold text-warning-600 dark:text-warning-400">
                {{ $totals['blocked_count'] ?? 0 }}
            </div>
        </x-filament::section>
        <x-filament::section>
            <x-slot name="heading">{{ __('dberp.resources.customer_credit_control.stat_overdue_count_total') }}</x-slot>
            <div class="text-2xl font-bold text-danger-600 dark:text-danger-400">
                {{ $totals['overdue_count'] ?? 0 }}
            </div>
        </x-filament::section>
    </div>

    <x-filament::section>
        <x-slot name="heading">{{ __('dberp.resources.customer_credit_control.section_overview') }}</x-slot>

        <div class="overflow-x-auto">
            <table class="w-full table-auto border-collapse text-start text-sm">
                <thead>
                    <tr class="border-b border-gray-200 dark:border-white/10">
                        <th class="px-2.5 py-2.5 text-start font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.customer') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-start font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.price_level_code') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-end font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.default_payment_term_days') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-end font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.credit_limit_amount') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-end font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.occupied_amount') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-end font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.projected_amount') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-end font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.overdue_count') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-end font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.overdue_amount') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-start font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.earliest_due_date') }}
                        </th>
                        <th class="px-2.5 py-2.5 text-start font-semibold text-gray-950 dark:text-white">
                            {{ __('dberp.fields.credit_check_result') }}
                        </th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($rows as $row)
                        <tr class="border-b border-gray-100 dark:border-white/5">
                            <td class="px-2.5 py-2.5 text-gray-950 dark:text-white">{{ $row['customer_name'] }}</td>
                            <td class="px-2.5 py-2.5 text-gray-950 dark:text-white">{{ $row['price_level_label'] ?? ($row['price_level_code'] ?: '-') }}</td>
                            <td class="px-2.5 py-2.5 text-end text-gray-950 dark:text-white">{{ $row['default_payment_term_days'] ?? '-' }}</td>
                            <td class="px-2.5 py-2.5 text-end text-gray-950 dark:text-white">{{ $row['credit_limit_amount'] !== '' ? $row['credit_limit_amount'] : '-' }}</td>
                            <td class="px-2.5 py-2.5 text-end text-gray-950 dark:text-white">{{ $row['occupied_amount'] ?: '-' }}</td>
                            <td class="px-2.5 py-2.5 text-end text-gray-950 dark:text-white">{{ $row['projected_amount'] ?: '-' }}</td>
                            <td class="px-2.5 py-2.5 text-end text-gray-950 dark:text-white">{{ $row['overdue_count'] }}</td>
                            <td class="px-2.5 py-2.5 text-end text-gray-950 dark:text-white">{{ $row['overdue_amount'] ?: '-' }}</td>
                            <td class="px-2.5 py-2.5 text-gray-950 dark:text-white">{{ $row['earliest_due_date'] ?: '-' }}</td>
                            <td class="px-2.5 py-2.5">
                                @php
                                    $creditResult = $row['credit_check_result'] ?? '';
                                @endphp
                                <span @class([
                                    'inline-flex items-center rounded-full px-2.5 py-0.5 text-xs font-medium ring-1 ring-inset',
                                    'bg-success-50 text-success-700 ring-success-600/20 dark:bg-success-500/10 dark:text-success-400 dark:ring-success-500/20' => $creditResult === 'pass',
                                    'bg-primary-50 text-primary-700 ring-primary-600/20 dark:bg-primary-500/10 dark:text-primary-400 dark:ring-primary-500/20' => $creditResult === 'released_by_override',
                                    'bg-warning-50 text-warning-700 ring-warning-600/20 dark:bg-warning-500/10 dark:text-warning-400 dark:ring-warning-500/20' => ! in_array($creditResult, ['pass', 'released_by_override'], true),
                                ])>
                                    {{ $row['credit_check_result_label'] }}
                                </span>
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="10" class="px-2.5 py-[18px] text-center text-gray-500 dark:text-gray-400">
                                {{ __('dberp.resources.customer_credit_control.empty_rows') }}
                            </td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </x-filament::section>
</x-filament-panels::page>
