@php
    $nextActions = match($loan->status) {
        'draft' => ['submit', 'cancel'], 'submitted' => ['approve', 'reject', 'cancel'],
        'approved', 'active', 'partially_paid' => ['settle'], default => [],
    };
@endphp
<x-ui.layout.page :title="__('loans.details', ['id' => $loan->id])" page-icon="fe fe-dollar-sign">
    <x-slot:actions>
        <x-ui.buttons.secondary :href="route('hr.loans.index')">{{ __('common.back') }}</x-ui.buttons.secondary>
        @can('update', $loan)
            @if($loan->status === 'draft')
                <x-ui.buttons.secondary :href="route('hr.loans.edit', $loan)">{{ __('common.edit') }}</x-ui.buttons.secondary>
            @endif
        @endcan
    </x-slot:actions>
    <div class="card mb-3"><div class="card-body row g-3">
        <div class="col-md-4"><strong>{{ __('loans.fields.employee') }}</strong><div>{{ $loan->employee?->full_name }}</div></div>
        <div class="col-md-4"><strong>{{ __('loans.fields.amount') }}</strong><div>{{ number_format((float)$loan->amount, 2) }}</div></div>
        <div class="col-md-4"><strong>{{ __('loans.fields.status') }}</strong><div>{{ __('loans.status.'.$loan->status) }}</div></div>
        <div class="col-12 d-flex gap-2 flex-wrap">
            @foreach($nextActions as $action)
                @can(in_array($action, ['approve', 'reject']) ? 'approve' : 'manage', $loan)
                    <form method="POST" action="{{ route('hr.loans.'.$action, $loan) }}">@csrf
                        <x-ui.buttons.primary type="submit" size="sm">{{ __('loans.actions.'.$action) }}</x-ui.buttons.primary>
                    </form>
                @endcan
            @endforeach
        </div>
    </div></div>
    <x-ui.table.table :columns="[['label'=>__('loans.fields.due_date')],['label'=>__('loans.fields.amount')],['label'=>__('loans.fields.status')]]">
        @foreach($loan->installments as $installment)
            <x-ui.table.row><td>{{ $installment->due_date?->format('Y-m-d') }}</td><td>{{ number_format((float)$installment->amount, 2) }}</td><td>{{ $installment->status }}</td></x-ui.table.row>
        @endforeach
    </x-ui.table.table>
</x-ui.layout.page>
