@php
    $emptyTitle = request()->hasAny(['type', 'status', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('loans.empty');
@endphp

<x-ui.layout.page :title="__('loans.title')" :subtitle="__('loans.subtitle')" page-icon="fe fe-dollar-sign">
    <x-ui.operational.index-shell>
        <x-slot:toolbar>
            <x-ui.operational.action-bar>
                <x-slot:filters>
                    <x-ui.operational.index-filters :fields="[
                        [
                            'type' => 'select',
                            'name' => 'type',
                            'label' => __('loans.fields.type'),
                            'options' => ['' => __('common.all'), 'loan' => __('loans.types.loan'), 'advance' => __('loans.types.advance')],
                            'width' => '9rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'status',
                            'label' => __('loans.fields.status'),
                            'options' => ['' => __('common.all')] + collect(['draft', 'submitted', 'approved', 'rejected', 'cancelled', 'active', 'partially_paid', 'settled'])
                                ->mapWithKeys(fn ($status) => [$status => __('loans.status.'.$status)])->all(),
                            'width' => '10rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:primary>
                    <x-ui.operational.create-button :href="route('hr.loans.create')" :label="__('loans.actions.new')" permission="loans.create" />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>
        <x-ui.table.table :columns="[
            ['label' => '#'], ['label' => __('loans.fields.employee')], ['label' => __('loans.fields.type')],
            ['label' => __('loans.fields.amount'), 'sort' => 'amount'], ['label' => __('loans.fields.balance')],
            ['label' => __('loans.fields.status'), 'sort' => 'status'], ['label' => __('common.actions'), 'align' => 'end'],
        ]">
            @forelse($loans as $loan)
                <x-ui.table.row>
                    <td><code>{{ $loan->id }}</code></td>
                    <td>{{ $loan->employee?->full_name ?? '—' }}</td>
                    <td>{{ __('loans.types.'.$loan->type) }}</td>
                    <td>{{ number_format((float) $loan->amount, 2) }}</td>
                    <td>{{ number_format((float) $loan->remaining_balance, 2) }}</td>
                    <td><x-ui.operational.status-badge :label="__('loans.status.'.$loan->status)" /></td>
                    <td class="text-end">
                        <x-ui.buttons.secondary size="sm" :href="route('hr.loans.show', $loan)">{{ __('common.view') }}</x-ui.buttons.secondary>
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row><td colspan="7"><x-ui.operational.empty-state :title="$emptyTitle" /></td></x-ui.table.row>
            @endforelse
        </x-ui.table.table>
        <x-slot:pagination>
            <x-ui.operational.pagination :paginator="$loans" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
