@php
    use App\Modules\HR\Leave\Support\LeaveRequestStatus;
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('leave.title')],
    ];
@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: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')],
            ['label' => __('leave.fields.end_date')],
            ['label' => __('leave.fields.total_days')],
            ['label' => __('leave.fields.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="__('leave.empty')" />
                    </td>
                </x-ui.table.row>
            @endforelse
        </x-ui.table.table>

        <x-slot:pagination>
            @if ($requests->hasPages())
                <div class="d-flex justify-content-center">{{ $requests->links() }}</div>
            @endif
        </x-slot:pagination>
    </x-ui.operational.index-shell>
</x-ui.layout.page>
