@php
    $editingId = (int) request('edit_asset');
@endphp

<p class="text-muted small mb-3">{{ __('employees.assets.future_ready_hint') }}</p>

@can('update', $employee)
    @if ($editingId < 1)
        <form method="POST" action="{{ route('hr.employees.assets.store', $employee) }}" class="mb-4">
            @csrf
            <h6 class="mb-3">{{ __('employees.assets.add') }}</h6>
            @include('pages.hr.employees._partials.asset-fields')
            <x-ui.buttons.primary type="submit" icon="fe fe-plus">{{ __('employees.assets.add') }}</x-ui.buttons.primary>
        </form>
    @endif
@endcan

@if ($tabData === null || $tabData->isEmpty())
    <x-ui.operational.empty-state :title="__('employees.empty_tab.assets')" />
@else
    <x-ui.table.table :columns="[
        ['label' => __('employees.assets.asset_code')],
        ['label' => __('employees.assets.asset_name')],
        ['label' => __('employees.assets.assigned_at')],
        ['label' => __('employees.assets.status')],
        ['label' => __('common.actions'), 'align' => 'end'],
    ]">
        @foreach ($tabData as $asset)
            @if ($editingId === (int) $asset->id)
                <x-ui.table.row>
                    <td colspan="5">
                        <form method="POST" action="{{ route('hr.employees.assets.update', [$employee, $asset]) }}">
                            @csrf
                            @method('PUT')
                            <h6 class="mb-3">{{ __('employees.actions.edit') }}</h6>
                            @include('pages.hr.employees._partials.asset-fields', ['asset' => $asset])
                            <div class="d-flex gap-2">
                                <x-ui.buttons.primary type="submit" icon="fe fe-save">{{ __('common.save') }}</x-ui.buttons.primary>
                                <x-ui.buttons.secondary :href="route('hr.employees.show', ['employee' => $employee, 'tab' => 'assets'])">
                                    {{ __('common.cancel') }}
                                </x-ui.buttons.secondary>
                            </div>
                        </form>
                    </td>
                </x-ui.table.row>
            @else
                <x-ui.table.row>
                    <td><code>{{ $asset->asset_code }}</code></td>
                    <td>
                        <strong>{{ $asset->asset_name }}</strong>
                        @if ($asset->category)
                            <div class="small text-muted">{{ $asset->category }}</div>
                        @endif
                    </td>
                    <td>{{ $asset->assigned_at?->format('Y-m-d') }}</td>
                    <td>{{ __('employees.assets.statuses.'.$asset->status) }}</td>
                    <td class="text-end">
                        @can('update', $employee)
                            <div class="d-inline-flex gap-2">
                                <x-ui.buttons.secondary size="sm" icon="fe fe-edit-2"
                                    :href="route('hr.employees.show', ['employee' => $employee, 'tab' => 'assets', 'edit_asset' => $asset->id])">
                                    {{ __('employees.actions.edit') }}
                                </x-ui.buttons.secondary>
                                <form method="POST" action="{{ route('hr.employees.assets.destroy', [$employee, $asset]) }}"
                                    onsubmit="return confirm(@json(__('employees.assets.delete_confirm')))">
                                    @csrf
                                    @method('DELETE')
                                    <x-ui.buttons.danger outline type="submit" size="sm" icon="fe fe-trash-2">
                                        {{ __('common.delete') }}
                                    </x-ui.buttons.danger>
                                </form>
                            </div>
                        @endcan
                    </td>
                </x-ui.table.row>
            @endif
        @endforeach
    </x-ui.table.table>
@endif
