@php
    use App\Modules\HR\Attendance\Support\AttendanceCorrectionStatus;

    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('attendance.title'), 'url' => route('hr.attendance.index')],
        ['label' => __('attendance_corrections.title')],
    ];
    $statusFilterOptions = [
        AttendanceCorrectionStatus::PENDING => __('attendance_corrections.status.pending'),
        'all' => __('attendance_corrections.filters.all'),
        AttendanceCorrectionStatus::APPROVED => __('attendance_corrections.status.approved'),
        AttendanceCorrectionStatus::REJECTED => __('attendance_corrections.status.rejected'),
    ];
@endphp

<x-ui.layout.page
    :title="__('attendance_corrections.title')"
    :subtitle="__('attendance_corrections.subtitle')"
    page-icon="fe fe-edit-3"
    :breadcrumbs="$breadcrumbs"
>
    <x-slot:actions>
        <x-ui.buttons.secondary :href="route('hr.attendance.index')" icon="fe fe-arrow-left">
            {{ __('attendance.actions.dashboard') }}
        </x-ui.buttons.secondary>
    </x-slot:actions>

    <div class="alert alert-secondary border-0 small mb-4">{{ __('attendance_corrections.hr_help') }}</div>

    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('hr.attendance.corrections.index') }}" class="row g-3 align-items-end">
                <div class="col-md-4">
                    <x-ui.form.select
                        name="status"
                        :label="__('attendance_corrections.filters.status')"
                        :options="$statusFilterOptions"
                        :selected="$status"
                    />
                </div>
                <div class="col-md-4">
                    <x-ui.buttons.primary type="submit" icon="fe fe-filter">
                        {{ __('attendance_corrections.filters.apply') }}
                    </x-ui.buttons.primary>
                </div>
            </form>
        </div>
    </div>

    <div class="card border-0 shadow-sm">
        <div class="table-responsive">
            <table class="table table-hover align-middle mb-0">
                <thead>
                    <tr>
                        <th>#</th>
                        <th>{{ __('attendance.fields.employee') }}</th>
                        <th>{{ __('attendance_corrections.fields.date') }}</th>
                        <th>{{ __('attendance_corrections.fields.type') }}</th>
                        <th>{{ __('attendance_corrections.fields.status') }}</th>
                        <th>{{ __('attendance_corrections.fields.reason') }}</th>
                        <th class="text-end">{{ __('common.actions') }}</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($paginator as $row)
                        <tr>
                            <td><code>{{ $row->id }}</code></td>
                            <td><strong>{{ $row->employee?->full_name ?? '—' }}</strong></td>
                            <td>{{ $row->attendanceRecord?->attendance_date?->format('Y-m-d') ?? '—' }}</td>
                            <td>{{ __('attendance_corrections.types.'.$row->type) }}</td>
                            <td>
                                <span class="badge bg-secondary">{{ __('attendance_corrections.status.'.$row->status) }}</span>
                            </td>
                            <td class="small text-muted">{{ \Illuminate\Support\Str::limit($row->reason, 80) }}</td>
                            <td class="text-end">
                                @if ($row->status === AttendanceCorrectionStatus::PENDING)
                                    <div class="d-flex flex-column gap-2 align-items-end">
                                        <form method="POST" action="{{ route('hr.attendance.corrections.approve', $row->id) }}" class="w-100" style="max-width: 280px;">
                                            @csrf
                                            <x-ui.form.field name="reviewer_note" :label="__('attendance_corrections.fields.reviewer_note')" required>
                                                <textarea name="reviewer_note" class="form-control form-control-sm" rows="2" required></textarea>
                                            </x-ui.form.field>
                                            <x-ui.buttons.primary type="submit" size="sm" icon="fe fe-check" class="mt-1">
                                                {{ __('attendance_corrections.actions.approve') }}
                                            </x-ui.buttons.primary>
                                        </form>
                                        <form method="POST" action="{{ route('hr.attendance.corrections.reject', $row->id) }}" class="w-100" style="max-width: 280px;">
                                            @csrf
                                            <x-ui.form.field name="reviewer_note" :label="__('attendance_corrections.fields.reviewer_note')" required>
                                                <textarea name="reviewer_note" class="form-control form-control-sm" rows="2" required></textarea>
                                            </x-ui.form.field>
                                            <x-ui.buttons.danger outline type="submit" size="sm" icon="fe fe-x" class="mt-1">
                                                {{ __('attendance_corrections.actions.reject') }}
                                            </x-ui.buttons.danger>
                                        </form>
                                    </div>
                                @else
                                    —
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="7" class="text-center text-muted py-4">{{ __('attendance_corrections.empty') }}</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
    </div>

    @if ($paginator->hasPages())
        <div class="mt-3 d-flex justify-content-center">{{ $paginator->withQueryString()->links() }}</div>
    @endif
</x-ui.layout.page>
