@php
    $emptyTitle = request()->hasAny(['q', 'calculation_type', 'is_active', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('compensation.empty');
@endphp

<x-ui.layout.page :title="__('compensation.deductions')" page-icon="fe fe-minus-circle">
    @can('compensation.create')
        <div class="card mb-3"><div class="card-body">
            <form method="POST" action="{{ route('hr.deductions.store') }}" class="row g-3">@csrf
                <div class="col-md-3"><x-ui.form.input name="code" :label="__('compensation.fields.code')" required /></div>
                <div class="col-md-3"><x-ui.form.input name="name" :label="__('compensation.fields.name')" required /></div>
                <div class="col-md-3"><x-ui.form.select name="calculation_type" :label="__('compensation.fields.calculation_type')" :options="['fixed'=>__('compensation.fixed'),'percentage'=>__('compensation.percentage')]" required /></div>
                <div class="col-md-2"><x-ui.form.input name="default_amount" type="number" step="0.01" :label="__('compensation.fields.default_amount')" /></div>
                <input type="hidden" name="is_active" value="1"><input type="hidden" name="is_recurring" value="1">
                <div class="col-md-1 d-flex align-items-end"><x-ui.buttons.primary type="submit">{{ __('common.save') }}</x-ui.buttons.primary></div>
            </form>
        </div></div>
    @endcan
    <x-ui.operational.index-shell>
        <x-slot:toolbar>
            <x-ui.operational.action-bar>
                <x-slot:filters>
                    <x-ui.operational.index-filters :fields="[
                        [
                            'type' => 'search',
                            'name' => 'q',
                            'label' => __('common.search'),
                            'width' => '14rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'calculation_type',
                            'label' => __('compensation.fields.calculation_type'),
                            'options' => ['' => __('common.all'), 'fixed' => __('compensation.fixed'), 'percentage' => __('compensation.percentage')],
                            'width' => '10rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'is_active',
                            'label' => __('common.active'),
                            'options' => ['' => __('common.all'), '1' => __('common.active'), '0' => __('common.inactive')],
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => __('compensation.fields.code'), 'sort' => 'code'],
            ['label' => __('compensation.fields.name'), 'sort' => 'name'],
            ['label' => __('compensation.fields.calculation_type')],
            ['label' => __('compensation.fields.default_amount'), 'sort' => 'default_amount'],
            ['label' => __('common.actions'), 'align' => 'end'],
        ]">
            @forelse($types as $type)
                <x-ui.table.row><td>{{ $type->code }}</td><td>{{ $type->name }}</td><td>{{ __('compensation.'.$type->calculation_type) }}</td><td>{{ number_format((float)$type->default_amount, 2) }}</td>
                    <td class="text-end"><x-ui.action-modal.trigger :entity="__('compensation.deductions')" :record="$type->name" :delete-url="auth()->user()?->can('compensation.manage') ? route('hr.deductions.destroy', $type) : null" /></td>
                </x-ui.table.row>
            @empty<x-ui.table.row><td colspan="5"><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="$types" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>
    <x-ui.action-modal />
</x-ui.layout.page>
