@extends('layouts.app')

@section('title', 'Attendance Import Result')
@section('heading', 'Attendance Import Result')

@section('content')
    @include('attendance.top-submenu')

    @php
        $summary = $import->summary_json ?? [];
        $failedCount = (int) ($summary['failed_records'] ?? $import->failed_rows);
        $dupCount = (int) ($summary['duplicate_records'] ?? $import->duplicate_rows);
        $successCount = (int) ($summary['successfully_imported'] ?? $import->success_rows);
    @endphp

    <div class="mb-4 flex flex-wrap items-center justify-between gap-3">
        <div>
            <h2 class="text-2xl font-semibold text-slate-900">Import #{{ $import->id }}</h2>
            <p class="mt-1 text-sm text-slate-500">
                {{ $import->original_file_name ?? 'N/A' }}
                @if($import->creator)
                    — by {{ $import->creator->name }}
                @endif
                @if($import->completed_at)
                    — {{ $import->completed_at->format('d M Y H:i') }}
                @endif
            </p>
        </div>
        <div class="flex flex-wrap gap-2">
            @if($failedCount > 0 || $dupCount > 0)
                <a href="{{ route('attendance.import.errors', $import) }}" 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">
                    Download Error Report
                </a>
            @endif
            <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 to Imports</a>
        </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">Successfully Imported</p><p class="text-lg font-semibold text-emerald-600">{{ $successCount }}</p></x-card>
        <x-card class="p-4"><p class="text-xs text-slate-500">Duplicate Records</p><p class="text-lg font-semibold text-amber-600">{{ $dupCount }}</p></x-card>
        <x-card class="p-4"><p class="text-xs text-slate-500">Failed Records</p><p class="text-lg font-semibold text-red-600">{{ $failedCount }}</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">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->attendance_status }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">{{ ucfirst($row->status) }}</td>
                    <td class="px-4 py-3 text-sm text-slate-600">
                        @if($row->status === 'imported')
                            Imported successfully.
                        @else
                            {{ $row->error_message ?: '—' }}
                        @endif
                    </td>
                </tr>
            @empty
                <tr>
                    <td colspan="6" class="px-4 py-8 text-center text-sm text-slate-500">No row results found yet.</td>
                </tr>
            @endforelse
        </x-table>
    </x-card>
@endsection
