@php
    use App\Modules\HR\Attendance\Support\AttendanceExceptionSeverity;
    use App\Modules\HR\Attendance\Support\AttendanceExceptionStatus;
    use App\Modules\HR\Attendance\Support\AttendanceExceptionType;

    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('attendance.title'), 'url' => route('hr.attendance.index')],
        ['label' => __('attendance_exceptions.title')],
    ];
    $empOptions = $employees->mapWithKeys(fn ($e) => [$e->id => $e->full_name])->all();
    $typeOptions = collect(AttendanceExceptionType::all())
        ->mapWithKeys(fn ($t) => [$t => __('attendance_exceptions.types.'.$t)])
        ->all();
    $severityOptions = collect(AttendanceExceptionSeverity::all())
        ->mapWithKeys(fn ($t) => [$t => __('attendance_exceptions.severity.'.$t)])
        ->all();
    $statusOptions = [
        AttendanceExceptionStatus::PENDING => __('attendance_exceptions.status.pending'),
        AttendanceExceptionStatus::RESOLVED => __('attendance_exceptions.status.resolved'),
        AttendanceExceptionStatus::IGNORED => __('attendance_exceptions.status.ignored'),
    ];
@endphp

<x-ui.layout.page
    :title="__('attendance_exceptions.title')"
    :subtitle="__('attendance_exceptions.subtitle')"
    page-icon="fe fe-alert-triangle"
    :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>
        @can('exports.access')
            <form method="POST" action="{{ route('exports.store') }}" class="d-inline">
                @csrf
                <input type="hidden" name="type" value="attendance_exceptions_report">
                @if (request('type'))
                    <input type="hidden" name="filters[type]" value="{{ request('type') }}">
                @endif
                @if (request('severity'))
                    <input type="hidden" name="filters[severity]" value="{{ request('severity') }}">
                @endif
                @if (request('status'))
                    <input type="hidden" name="filters[status]" value="{{ request('status') }}">
                @endif
                @if (request('employee_id'))
                    <input type="hidden" name="filters[employee_id]" value="{{ request('employee_id') }}">
                @endif
                @if (request('from'))
                    <input type="hidden" name="filters[from]" value="{{ request('from') }}">
                @endif
                @if (request('to'))
                    <input type="hidden" name="filters[to]" value="{{ request('to') }}">
                @endif
                <x-ui.buttons.secondary type="submit" icon="fe fe-download">
                    {{ __('exports.actions.export_csv') }}
                </x-ui.buttons.secondary>
            </form>
        @endcan
    </x-slot:actions>

    <div class="card mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('hr.attendance.exceptions.index') }}" class="row g-3 align-items-end">
                <div class="col-md-3">
                    <x-ui.form.select
                        name="type"
                        :label="__('attendance_exceptions.filters.type')"
                        :options="$typeOptions"
                        :selected="request('type')"
                        :placeholder="__('attendance_exceptions.filters.all')"
                    />
                </div>
                <div class="col-md-2">
                    <x-ui.form.select
                        name="severity"
                        :label="__('attendance_exceptions.filters.severity')"
                        :options="$severityOptions"
                        :selected="request('severity')"
                        :placeholder="__('attendance_exceptions.filters.all')"
                    />
                </div>
                <div class="col-md-2">
                    <x-ui.form.select
                        name="status"
                        :label="__('attendance_exceptions.filters.status')"
                        :options="$statusOptions"
                        :selected="request('status')"
                        :placeholder="__('attendance_exceptions.filters.all')"
                    />
                </div>
                <div class="col-md-3">
                    <x-ui.form.search-select
                        name="employee_id"
                        lookup="employees"
                        :label="__('attendance_exceptions.filters.employee')"
                        :options="$empOptions"
                        :selected="request('employee_id')"
                        :placeholder="__('attendance_exceptions.filters.all')"
                        optional
                    />
                </div>
                <div class="col-md-2">
                    <x-ui.form.input name="from" type="date" :label="__('common.from')" :value="request('from')" />
                </div>
                <div class="col-md-2">
                    <x-ui.form.input name="to" type="date" :label="__('common.to')" :value="request('to')" />
                </div>
                <div class="col-12">
                    <x-ui.buttons.primary type="submit" icon="fe fe-filter">
                        {{ __('attendance_exceptions.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>{{ __('attendance_exceptions.table.employee') }}</th>
                        <th>{{ __('attendance_exceptions.table.type') }}</th>
                        <th>{{ __('attendance_exceptions.table.severity') }}</th>
                        <th>{{ __('attendance_exceptions.table.detected') }}</th>
                        <th>{{ __('attendance_exceptions.table.status') }}</th>
                        <th class="text-end">{{ __('attendance_exceptions.table.actions') }}</th>
                    </tr>
                </thead>
                <tbody>
                    @forelse ($paginator as $row)
                        <tr>
                            <td>{{ $row->employee?->full_name ?? '—' }}</td>
                            <td>{{ __('attendance_exceptions.types.'.$row->type) }}</td>
                            <td>
                                <span class="badge bg-light text-dark border">{{ __('attendance_exceptions.severity.'.$row->severity) }}</span>
                            </td>
                            <td>{{ $row->detected_at?->timezone(config('app.timezone'))->format('Y-m-d H:i') }}</td>
                            <td>{{ __('attendance_exceptions.status.'.$row->status) }}</td>
                            <td class="text-end">
                                @if ($row->status === AttendanceExceptionStatus::PENDING)
                                    @can('review', $row)
                                        <form method="POST" action="{{ route('hr.attendance.exceptions.resolve', $row->id) }}" class="d-inline">
                                            @csrf
                                            <button type="submit" class="btn btn-sm btn-outline-success">{{ __('attendance_exceptions.actions.resolve') }}</button>
                                        </form>
                                        <form method="POST" action="{{ route('hr.attendance.exceptions.ignore', $row->id) }}" class="d-inline">
                                            @csrf
                                            <button type="submit" class="btn btn-sm btn-outline-secondary">{{ __('attendance_exceptions.actions.ignore') }}</button>
                                        </form>
                                    @endcan
                                @endif
                                @if ($row->attendance_record_id && $row->attendanceRecord)
                                    @php($d = $row->attendanceRecord->attendance_date->toDateString())
                                    <a href="{{ route('hr.attendance.index', ['from' => $d, 'to' => $d]) }}" class="btn btn-sm btn-outline-primary">
                                        {{ __('attendance_exceptions.actions.view_record') }}
                                    </a>
                                @endif
                            </td>
                        </tr>
                    @empty
                        <tr>
                            <td colspan="6" class="text-center text-muted py-4">{{ __('attendance_exceptions.empty') }}</td>
                        </tr>
                    @endforelse
                </tbody>
            </table>
        </div>
        <div class="card-body border-top">{{ $paginator->links() }}</div>
    </div>
</x-ui.layout.page>
