@extends('layouts.app')

@section('title', 'Attendance Import Preview')
@section('heading', 'Attendance Import Preview')

@section('content')
    @include('attendance.top-submenu')

    <div class="mb-4 flex flex-wrap items-center justify-between gap-3">
        <div>
            <h2 class="text-2xl font-semibold text-slate-900">Preview Import #{{ $import->id }}</h2>
            <p class="mt-1 text-sm text-slate-500">{{ $import->original_file_name ?? 'N/A' }} — review valid and invalid rows before importing.</p>
        </div>
        <div class="flex flex-wrap gap-2">
            <a href="{{ route('attendance.import.index') }}" class="inline-flex items-center rounded-lg border border-slate-300 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Back</a>
            <form action="{{ route('attendance.import.cancel', $import) }}" method="POST">
                @csrf
                <button type="submit" class="inline-flex items-center rounded-lg border border-slate-300 bg-white px-4 py-2 text-sm font-medium text-slate-700 hover:bg-slate-50">Cancel</button>
            </form>
            <form action="{{ route('attendance.import.confirm', $import) }}" method="POST">
                @csrf
                <button type="submit" class="inline-flex items-center rounded-lg bg-teal-700 px-4 py-2 text-sm font-medium text-white hover:bg-teal-600 disabled:opacity-50" {{ $import->valid_rows < 1 ? 'disabled' : '' }}>
                    Confirm Import ({{ $import->valid_rows }})
                </button>
            </form>
        </div>
    </div>

    <div class="mb-5 grid gap-3 md:grid-cols-4">
        <x-card class="p-4"><p class="text-xs text-slate-500">Total Records</p><p class="text-lg font-semibold text-slate-900">{{ $import->total_rows }}</p></x-card>
        <x-card class="p-4"><p class="text-xs text-slate-500">Valid</p><p class="text-lg font-semibold text-emerald-600">{{ $import->valid_rows }}</p></x-card>
        <x-card class="p-4"><p class="text-xs text-slate-500">Duplicates</p><p class="text-lg font-semibold text-amber-600">{{ $import->duplicate_rows }}</p></x-card>
        <x-card class="p-4"><p class="text-xs text-slate-500">Invalid</p><p class="text-lg font-semibold text-red-600">{{ $import->invalid_rows }}</p></x-card>
    </div>

    <x-card class="p-0 overflow-hidden">
        <x-table>
            <x-slot name="head">
                <tr>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Row</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Employee</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Date</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">In / Out</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Status</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Result</th>
                    <th class="px-4 py-3 text-left text-xs font-medium uppercase text-slate-500">Message</th>
                </tr>
            </x-slot>
            @forelse($import->rows as $row)
                <tr class="hover:bg-slate-50">
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $row->row_number }}</td>
                    <td class="px-4 py-3 text-sm text-slate-900">{{ $row->employee_code }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $row->attendance_date?->toDateString() }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $row->check_in_time ?? '—' }} / {{ $row->check_out_time ?? '—' }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $row->attendance_status }}</td>
                    <td class="px-4 py-3 text-sm">
                        @if($row->status === 'valid')
                            <span class="text-emerald-600">Valid</span>
                        @elseif($row->status === 'duplicate')
                            <span class="text-amber-600">Duplicate</span>
                        @else
                            <span class="text-red-600">Invalid</span>
                        @endif
                    </td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ $row->error_message ?: 'Ready to import.' }}</td>
                </tr>
            @empty
                <tr>
                    <td colspan="7" class="px-4 py-8 text-center text-sm text-slate-500">No rows found in the uploaded file.</td>
                </tr>
            @endforelse
        </x-table>
    </x-card>
@endsection
