@php
    $editing = isset($loan);
    $action = $editing ? route('hr.loans.update', $loan) : route('hr.loans.store');
@endphp
<x-ui.layout.page :title="$editing ? __('loans.actions.edit') : __('loans.actions.new')" page-icon="fe fe-dollar-sign">
    <x-slot:actions>
        <x-ui.buttons.secondary :href="route('hr.loans.index')" icon="fe fe-arrow-left">{{ __('common.back') }}</x-ui.buttons.secondary>
    </x-slot:actions>
    <div class="card"><div class="card-body">
        <form method="POST" action="{{ $action }}" class="row g-3">
            @csrf
            @if($editing) @method('PUT') @endif
            <div class="col-md-6">
                <x-ui.form.search-select name="employee_id" lookup="employees" :label="__('loans.fields.employee')"
                    :options="$employees" :selected="old('employee_id', $loan->employee_id ?? null)" required />
            </div>
            <div class="col-md-6">
                <x-ui.form.select name="type" :label="__('loans.fields.type')"
                    :options="collect($types)->mapWithKeys(fn($type) => [$type => __('loans.types.'.$type)])->all()"
                    :selected="old('type', $loan->type ?? null)" required />
            </div>
            <div class="col-md-4"><x-ui.form.input name="amount" type="number" step="0.01" :label="__('loans.fields.amount')" :value="$loan->amount ?? null" required /></div>
            <div class="col-md-4"><x-ui.form.input name="interest_rate" type="number" step="0.01" :label="__('loans.fields.interest_rate')" :value="$loan->interest_rate ?? 0" /></div>
            <div class="col-md-4"><x-ui.form.input name="installment_count" type="number" :label="__('loans.fields.installments')" :value="$loan->installment_count ?? 1" required /></div>
            <div class="col-md-6"><x-ui.form.input name="start_date" type="date" :label="__('loans.fields.start_date')" :value="isset($loan) ? $loan->start_date?->format('Y-m-d') : null" required /></div>
            <div class="col-md-6"><x-ui.form.input name="reason" :label="__('loans.fields.reason')" :value="$loan->reason ?? null" /></div>
            <div class="col-12"><x-ui.form.input name="notes" :label="__('loans.fields.notes')" :value="$loan->notes ?? null" /></div>
            <div class="col-12"><x-ui.buttons.primary type="submit" icon="fe fe-save">{{ __('common.save') }}</x-ui.buttons.primary></div>
        </form>
    </div></div>
</x-ui.layout.page>
