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

    $breadcrumbs = [
        ['label' => __('common.home'), 'url' => route('dashboard.index')],
        ['label' => __('me.attendance_title')],
    ];
@endphp

<x-ui.layout.page
    :title="__('me.attendance_title')"
    :subtitle="__('me.attendance_subtitle')"
    page-icon="fe fe-clock"
    :breadcrumbs="$breadcrumbs"
>
    <x-slot:actions>
        @can('self_service.team_attendance')
            <x-ui.buttons.secondary :href="route('me.team.attendance.index')" icon="fe fe-users">
                {{ __('me.team_attendance.title') }}
            </x-ui.buttons.secondary>
        @endcan
        @can('create', \App\Modules\HR\Attendance\Models\AttendanceCorrectionRequest::class)
            <x-ui.buttons.secondary :href="route('me.attendance.corrections.index')" icon="fe fe-edit-3">
                {{ __('attendance_corrections.title') }}
            </x-ui.buttons.secondary>
        @endcan
    </x-slot:actions>

    @error('attendance')
        <div class="alert alert-danger border-0 shadow-sm">{{ $message }}</div>
    @enderror

    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body d-flex flex-column flex-md-row align-items-md-center justify-content-between gap-3">
            <div>
                <h5 class="mb-1">{{ __('attendance.self.today_title') }}</h5>
                @if ($todayRecord?->check_out_at)
                    <p class="text-muted mb-0">
                        {{ __('attendance.self.completed_summary', [
                            'in' => $todayRecord->check_in_at?->format('H:i'),
                            'out' => $todayRecord->check_out_at?->format('H:i'),
                        ]) }}
                    </p>
                @elseif ($todayRecord?->check_in_at)
                    <p class="text-muted mb-0">
                        {{ __('attendance.self.working_summary', [
                            'time' => $todayRecord->check_in_at->format('H:i'),
                        ]) }}
                    </p>
                @else
                    <p class="text-muted mb-0">{{ __('attendance.self.not_checked_in') }}</p>
                @endif
            </div>
            <div>
                @if (! $todayRecord?->check_in_at)
                    <form method="POST" action="{{ route('me.attendance.check-in') }}">
                        @csrf
                        <x-ui.buttons.primary type="submit" icon="fe fe-log-in">
                            {{ __('attendance.self.check_in') }}
                        </x-ui.buttons.primary>
                    </form>
                @elseif (! $todayRecord?->check_out_at)
                    <form method="POST" action="{{ route('me.attendance.check-out') }}">
                        @csrf
                        <x-ui.buttons.primary type="submit" icon="fe fe-log-out">
                            {{ __('attendance.self.check_out') }}
                        </x-ui.buttons.primary>
                    </form>
                @endif
            </div>
        </div>
    </div>

    @foreach ($missingCheckoutRecords as $missingRecord)
        <div class="alert alert-warning border-0 shadow-sm d-flex flex-column flex-md-row justify-content-between gap-2">
            <span>
                {{ __('attendance.self.missing_checkout_prompt', [
                    'date' => $missingRecord->attendance_date?->format('Y-m-d'),
                ]) }}
            </span>
            <a class="alert-link text-nowrap" href="{{ route('me.attendance.corrections.create', [
                'record' => $missingRecord->id,
                'type' => \App\Modules\HR\Attendance\Support\AttendanceCorrectionType::MISSING_CHECKOUT,
            ]) }}">
                {{ __('attendance.self.provide_checkout') }}
            </a>
        </div>
    @endforeach

    <div class="card border-0 shadow-sm mb-4">
        <div class="card-body">
            <form method="GET" action="{{ route('me.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.check_in')],
        ['label' => __('attendance.fields.check_out')],
        ['label' => __('attendance.fields.source')],
        ['label' => __('attendance.fields.status')],
        ['label' => __('attendance.fields.late')],
        ['label' => __('attendance.fields.overtime')],
        ['label' => __('attendance.fields.missing_checkout')],
    ]">
        @forelse ($paginator as $record)
            @php
                $ot = (int) ($record->overtime_minutes ?? 0);
                $otH = intdiv($ot, 60);
                $otM = $ot % 60;
                $otLabel = $ot > 0 ? sprintf('%d:%02d', $otH, $otM) : '—';
            @endphp
            <x-ui.table.row>
                <td>{{ $record->attendance_date?->format('Y-m-d') }}</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>
                    @if ($record->status === AttendanceRecordStatus::LATE)
                        <span class="badge bg-warning text-dark">{{ __('common.yes') }}</span>
                    @else
                        <span class="text-muted">—</span>
                    @endif
                </td>
                <td>{{ $otLabel }}</td>
                <td>
                    @if ($record->missing_checkout)
                        <span class="badge bg-danger">{{ __('common.yes') }}</span>
                    @else
                        <span class="text-muted">—</span>
                    @endif
                </td>
            </x-ui.table.row>
        @empty
            <x-ui.table.row>
                <td colspan="8" class="text-center text-muted py-4">
                    {{ __('attendance.empty') }}
                </td>
            </x-ui.table.row>
        @endforelse
    </x-ui.table.table>

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