@php
    use App\Modules\HR\Attendance\Support\AttendanceRecordStatus;
    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('common.hr')],
        ['label' => __('attendance.title')],
    ];
    $employeeOptions = $employees->mapWithKeys(fn ($e) => [$e->id => $e->full_name])->all();
    $statusSelectOptions = collect($manualStatusOptions)
        ->mapWithKeys(fn (string $s) => [$s => __('attendance.status.'.$s)])
        ->all();
@endphp

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

        @can('attendance.view')
            <x-ui.buttons.secondary :href="route('hr.attendance.exceptions.index')" icon="fe fe-alert-triangle">
                {{ __('nav.item.attendance_exceptions') }}
            </x-ui.buttons.secondary>
        @endcan
        @can('attendance.manage')
            <x-ui.buttons.secondary :href="route('hr.attendance.devices.index')" icon="fe fe-cpu">
                {{ __('attendance.actions.devices') }}
            </x-ui.buttons.secondary>
        @endcan
        @can('exports.access')
            <form method="POST" action="{{ route('exports.store') }}" class="d-inline">
                @csrf
                <input type="hidden" name="type" value="attendance_report">
                <input type="hidden" name="filters[from]" value="{{ $from }}">
                <input type="hidden" name="filters[to]" value="{{ $to }}">
                <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.index') }}" class="row g-3 align-items-end">
                <div class="col-md-4">
                    <x-ui.form.input name="from" type="date" :label="__('common.from')" :value="$from" required />
                </div>
                <div class="col-md-4">
                    <x-ui.form.input name="to" type="date" :label="__('common.to')" :value="$to" required />
                </div>
                <div class="col-md-4">
                    <x-ui.buttons.primary type="submit" icon="fe fe-filter">
                        {{ __('common.filter') }}
                    </x-ui.buttons.primary>
                </div>
            </form>
        </div>
    </div>

    <x-ui.table.table :columns="[
        ['label' => __('attendance.fields.date')],
        ['label' => __('attendance.fields.employee')],
        ['label' => __('attendance.fields.check_in')],
        ['label' => __('attendance.fields.check_out')],
        ['label' => __('attendance.fields.source')],
        ['label' => __('attendance.fields.status')],
        ['label' => __('common.actions'), 'align' => 'end', 'width' => '220px'],
    ]">
        @forelse ($records as $record)
            <x-ui.table.row>
                <td>{{ $record->attendance_date?->format('Y-m-d') }}</td>
                <td><strong>{{ $record->employee?->full_name ?? '—' }}</strong></td>
                <td>{{ $record->check_in_at?->format('Y-m-d H:i') ?? '—' }}</td>
                <td>{{ $record->check_out_at?->format('Y-m-d H:i') ?? '—' }}</td>
                <td>{{ __('attendance.source.'.$record->source) }}</td>
                <td>
                    <span class="badge bg-secondary">{{ __('attendance.status.'.$record->status) }}</span>
                </td>
                <td class="text-end">
                    @can('registerCheckout', $record)
                        @if ($record->check_out_at === null)
                            <form
                                method="POST"
                                action="{{ route('hr.attendance.records.checkout.update', $record) }}"
                                class="d-flex gap-1 justify-content-end align-items-center"
                            >
                                @csrf
                                @method('PATCH')
                                <input
                                    type="datetime-local"
                                    name="check_out_at"
                                    class="form-control form-control-sm"
                                    style="max-width: 11rem;"
                                    required
                                >
                                <button type="submit" class="btn btn-sm btn-outline-primary">
                                    {{ __('attendance.actions.update_checkout') }}
                                </button>
                            </form>
                        @endif
                    @endcan
                    @can('updateCheckout', $record)
                        @if ($record->check_out_at !== null)
                            <form
                                method="POST"
                                action="{{ route('hr.attendance.records.checkout.update', $record) }}"
                                class="d-flex gap-1 justify-content-end align-items-center"
                            >
                                @csrf
                                @method('PATCH')
                                <input
                                    type="datetime-local"
                                    name="check_out_at"
                                    class="form-control form-control-sm"
                                    style="max-width: 11rem;"
                                    value="{{ $record->check_out_at->format('Y-m-d\TH:i') }}"
                                    required
                                >
                                <button type="submit" class="btn btn-sm btn-outline-secondary">
                                    {{ __('attendance.actions.update_checkout') }}
                                </button>
                            </form>
                        @endif
                    @endcan
                </td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row>
                <td colspan="7" class="text-center text-muted py-4">
                    {{ __('attendance.empty') }}
                </td>
            </x-ui.table.row>
        @endforelse
    </x-ui.table.table>

    @if ($records->hasPages())
        <div class="mt-3 d-flex justify-content-center">
            {{ $records->withQueryString()->links() }}
        </div>
    @endif

    @can('attendance.create')
        <div class="card mt-4">
            <div class="card-header">
                <strong>{{ __('attendance.manual.heading') }}</strong>
                <div class="text-muted small">{{ __('attendance.manual.hint') }}</div>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ route('hr.attendance.store') }}" class="row g-3">
                    @csrf
                    <div class="col-md-6">
                        <x-ui.form.search-select
                            name="employee_id"
                            lookup="employees"
                            :label="__('attendance.fields.employee')"
                            :options="$employeeOptions"
                            :selected="old('employee_id')"
                            :placeholder="__('common.select')"
                            required
                        />
                    </div>
                    <div class="col-md-6">
                        <x-ui.form.input
                            name="attendance_date"
                            type="date"
                            :label="__('attendance.fields.date')"
                            :value="old('attendance_date', now()->toDateString())"
                            required
                        />
                    </div>
                    <div class="col-md-6">
                        <x-ui.form.input
                            name="check_in_at"
                            type="datetime-local"
                            :label="__('attendance.fields.check_in')"
                            :value="old('check_in_at')"
                        />
                    </div>
                    @can('registerCheckout', App\Modules\HR\Attendance\Models\AttendanceRecord::class)
                        <div class="col-md-6">
                            <x-ui.form.input
                                name="check_out_at"
                                type="datetime-local"
                                :label="__('attendance.fields.check_out')"
                                :value="old('check_out_at')"
                            />
                        </div>
                    @endcan
                    <div class="col-md-6">
                        <x-ui.form.select
                            name="status"
                            :label="__('attendance.fields.status')"
                            :options="$statusSelectOptions"
                            :selected="old('status', AttendanceRecordStatus::PRESENT)"
                            required
                        />
                    </div>
                    <div class="col-12">
                        <x-ui.form.input name="notes" :label="__('attendance.fields.notes')" :value="old('notes')" optional />
                    </div>
                    <div class="col-12">
                        <x-ui.buttons.primary type="submit" icon="fe fe-save">
                            {{ __('attendance.actions.save') }}
                        </x-ui.buttons.primary>
                    </div>
                </form>
            </div>
        </div>
    @endcan
</x-ui.layout.page>
