@php
    use App\Modules\HR\Leave\Support\LeaveRequestStatus;
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('leave.title')],
    ];
    $emptyTitle = request()->hasAny(['leave_type_id', 'status', 'start_date_from', 'start_date_to', 'sort', 'dir'])
        ? __('common.no_matching_records')
        : __('leave.empty');
@endphp

<x-ui.layout.page
    :title="__('leave.title')"
    :subtitle="__('leave.subtitle')"
    page-icon="fe fe-calendar"
    :breadcrumbs="$breadcrumbs"
>
    <x-ui.operational.index-shell>
        <x-slot:toolbar>
            <x-ui.operational.action-bar>
                <x-slot:filters>
                    <x-ui.operational.index-filters :fields="[
                        [
                            'type' => 'search-select',
                            'name' => 'leave_type_id',
                            'label' => __('leave.fields.leave_type'),
                            'lookup' => 'leave_types',
                            'width' => '12rem',
                        ],
                        [
                            'type' => 'select',
                            'name' => 'status',
                            'label' => __('leave.fields.status'),
                            'options' => ['' => __('common.all')] + collect([
                                LeaveRequestStatus::PENDING,
                                LeaveRequestStatus::MANAGER_APPROVED,
                                LeaveRequestStatus::HR_APPROVED,
                                LeaveRequestStatus::REJECTED,
                                LeaveRequestStatus::CANCELLED,
                            ])->mapWithKeys(fn ($status) => [$status => __('leave.status.'.$status)])->all(),
                            'width' => '10rem',
                        ],
                        [
                            'type' => 'date',
                            'name' => 'start_date_from',
                            'label' => __('common.from'),
                            'width' => '9rem',
                        ],
                        [
                            'type' => 'date',
                            'name' => 'start_date_to',
                            'label' => __('common.to'),
                            'width' => '9rem',
                        ],
                    ]" />
                </x-slot:filters>
                <x-slot:secondary>
                    @can('exports.access')
                        <form method="POST" action="{{ route('exports.store') }}" class="d-inline">
                            @csrf
                            <input type="hidden" name="type" value="leave_report">
                            <x-ui.buttons.secondary type="submit" icon="fe fe-download">
                                {{ __('exports.actions.export_csv') }}
                            </x-ui.buttons.secondary>
                        </form>
                    @endcan
                </x-slot:secondary>
                <x-slot:primary>
                    <x-ui.operational.create-button
                        :href="route('hr.leave.requests.create')"
                        :label="__('leave.actions.new')"
                        permission="leave.create"
                    />
                </x-slot:primary>
            </x-ui.operational.action-bar>
        </x-slot:toolbar>

        <x-ui.table.table :columns="[
            ['label' => '#'],
            ['label' => __('leave.fields.employee')],
            ['label' => __('leave.fields.leave_type')],
            ['label' => __('leave.fields.start_date'), 'sort' => 'start_date'],
            ['label' => __('leave.fields.end_date'), 'sort' => 'end_date'],
            ['label' => __('leave.fields.total_days'), 'sort' => 'total_days'],
            ['label' => __('leave.fields.status'), 'sort' => 'status'],
            ['label' => __('common.actions'), 'align' => 'end', 'width' => '220px'],
        ]">
            @forelse ($requests as $req)
                @php
                    $lv = match ($req->status) {
                        LeaveRequestStatus::HR_APPROVED => 'success',
                        LeaveRequestStatus::REJECTED => 'danger',
                        LeaveRequestStatus::CANCELLED => 'neutral',
                        default => 'warning',
                    };
                @endphp
                <x-ui.table.row>
                    <td><code>{{ $req->id }}</code></td>
                    <td><strong>{{ $req->employee?->full_name ?? '—' }}</strong></td>
                    <td>{{ $req->leaveType?->name ?? '—' }}</td>
                    <td>{{ $req->start_date?->format('Y-m-d') }}</td>
                    <td>{{ $req->end_date?->format('Y-m-d') }}</td>
                    <td>{{ $req->total_days }}</td>
                    <td>
                        <x-ui.operational.status-badge
                            :label="__('leave.status.'.$req->status)"
                            :variant="$lv"
                        />
                    </td>
                    <td class="text-end">
                        @if ($req->status === LeaveRequestStatus::PENDING)
                            @can('leave.approve.manager')
                                <form method="POST" action="{{ route('hr.leave.requests.approve.manager', $req) }}" class="d-inline">
                                    @csrf
                                    <x-ui.buttons.primary type="submit" size="sm" icon="fe fe-check">
                                        {{ __('leave.actions.approve_manager') }}
                                    </x-ui.buttons.primary>
                                </form>
                            @endcan
                        @endif
                        @if ($req->status === LeaveRequestStatus::MANAGER_APPROVED)
                            @can('leave.approve.hr')
                                <form method="POST" action="{{ route('hr.leave.requests.approve.hr', $req) }}" class="d-inline">
                                    @csrf
                                    <x-ui.buttons.primary type="submit" size="sm" icon="fe fe-check">
                                        {{ __('leave.actions.approve_hr') }}
                                    </x-ui.buttons.primary>
                                </form>
                            @endcan
                        @endif
                        @if (in_array($req->status, [LeaveRequestStatus::PENDING, LeaveRequestStatus::MANAGER_APPROVED], true))
                            @can('leave.reject')
                                <form method="POST" action="{{ route('hr.leave.requests.reject', $req) }}" class="d-inline">
                                    @csrf
                                    <x-ui.buttons.danger outline type="submit" size="sm" icon="fe fe-x">
                                        {{ __('leave.actions.reject') }}
                                    </x-ui.buttons.danger>
                                </form>
                            @endcan
                            @can('cancel', $req)
                                <form method="POST" action="{{ route('hr.leave.requests.cancel', $req) }}" class="d-inline">
                                    @csrf
                                    <x-ui.buttons.secondary type="submit" size="sm" icon="fe fe-slash">
                                        {{ __('leave.actions.cancel') }}
                                    </x-ui.buttons.secondary>
                                </form>
                            @endcan
                        @endif
                        @if (! in_array($req->status, [LeaveRequestStatus::PENDING, LeaveRequestStatus::MANAGER_APPROVED], true))
                            —
                        @endif
                    </td>
                </x-ui.table.row>
            @empty
                <x-ui.table.row>
                    <td colspan="8" class="p-0">
                        <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="$requests" />
        </x-slot:pagination>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
