@extends('layouts.app')

@section('title', 'Import — '.$batch->original_filename)

@section('content')
    <x-page-header
        :title="$batch->original_filename"
        subtitle="Import record">
        <x-slot:breadcrumbs>
            <li class="breadcrumb-item"><a href="{{ route('imports.index') }}">Imports</a></li>
            <li class="breadcrumb-item active" aria-current="page">{{ $batch->original_filename }}</li>
        </x-slot:breadcrumbs>

        @if (! $batch->status->isFinished())
            <a href="{{ route('imports.preview', $batch) }}" class="btn btn-primary">
                <i class="bi bi-eye" aria-hidden="true"></i>Continue to Preview
            </a>
        @endif
    </x-page-header>

    <div class="row g-3">
        <div class="col-xl-8">
            <div class="d-flex flex-column gap-3">

                <x-panel title="Summary">
                    <div class="d-flex align-items-center justify-content-between mb-3 pb-3 border-bottom">
                        <x-status-badge :status="$batch->status" class="fs-6" />
                    </div>

                    <x-detail-list>
                        <x-detail-row label="Entity Type">
                            {{ \App\Services\ImportService::moduleClass($batch->module)::label() }}
                        </x-detail-row>
                        <x-detail-row label="Total Rows">{{ $batch->total_rows }}</x-detail-row>
                        <x-detail-row label="Imported">
                            <span class="{{ $batch->success_rows > 0 ? 'text-success fw-medium' : '' }}">{{ $batch->success_rows }}</span>
                        </x-detail-row>
                        <x-detail-row label="Failed">
                            <span class="{{ $batch->failed_rows > 0 ? 'text-danger fw-medium' : '' }}">{{ $batch->failed_rows }}</span>
                        </x-detail-row>
                    </x-detail-list>

                    @if ($batch->error_summary)
                        <hr>
                        <div class="alert alert-danger mb-0" role="alert">
                            <i class="bi bi-exclamation-triangle" aria-hidden="true"></i>
                            {{ $batch->error_summary }}
                        </div>
                    @endif
                </x-panel>

                @if ($batch->documents_summary)
                    @php $documentCounts = $batch->documents_summary['counts']; @endphp
                    <x-panel title="Documents" subtitle="Invoices matched to imported assets by Asset Tag.">
                        <x-detail-list>
                            <x-detail-row label="Attached">
                                <span class="{{ $documentCounts['matched'] > 0 ? 'text-success fw-medium' : '' }}">{{ $documentCounts['matched'] }}</span>
                            </x-detail-row>
                            <x-detail-row label="Missing">{{ $documentCounts['missing'] }}</x-detail-row>
                            <x-detail-row label="Rejected">
                                <span class="{{ $documentCounts['rejected'] > 0 ? 'text-danger fw-medium' : '' }}">{{ $documentCounts['rejected'] }}</span>
                            </x-detail-row>
                            <x-detail-row label="Unmatched">
                                <span class="{{ $documentCounts['unmatched'] > 0 ? 'text-warning-emphasis fw-medium' : '' }}">{{ $documentCounts['unmatched'] }}</span>
                            </x-detail-row>
                        </x-detail-list>

                        @if ($documentCounts['rejected'] > 0 || $documentCounts['unmatched'] > 0)
                            <hr>
                            <a href="{{ route('imports.documents-report', $batch) }}" class="btn btn-outline-secondary">
                                <i class="bi bi-download" aria-hidden="true"></i>Download Document Problem Report
                            </a>
                        @endif
                    </x-panel>
                @endif

                @if ($batch->error_report_path)
                    <x-panel title="Re-import Failed Rows" subtitle="Fix the flagged cells and upload the corrected file.">
                        <p class="ams-text-xs text-secondary">
                            The error report is the same file, with an extra column explaining what
                            went wrong on each row. Fix the flagged rows in that file and upload it
                            again from the Import screen — rows that already succeeded are not
                            duplicated as long as their unique fields (tag, code or serial number,
                            depending on what you're importing) stay the same.
                        </p>

                        <div class="d-flex flex-wrap gap-2">
                            <a href="{{ route('imports.errors', $batch) }}" class="btn btn-outline-secondary">
                                <i class="bi bi-download" aria-hidden="true"></i>Download Error Report
                            </a>

                            <a href="{{ route('imports.create', ['module' => $batch->module]) }}" class="btn btn-primary">
                                <i class="bi bi-arrow-repeat" aria-hidden="true"></i>Re-import
                            </a>
                        </div>
                    </x-panel>
                @endif

            </div>
        </div>

        <div class="col-xl-4">
            <div class="d-flex flex-column gap-3">
                <x-panel title="Record">
                    <x-detail-list>
                        <x-detail-row label="Uploaded By">{{ $batch->user?->name ?? 'Unknown' }}</x-detail-row>
                        <x-detail-row label="Uploaded On">
                            {{ $batch->created_at?->format(setting('datetime_format', 'd M Y, H:i')) }}
                        </x-detail-row>
                        <x-detail-row label="Started">
                            {{ $batch->started_at?->format(setting('datetime_format', 'd M Y, H:i')) }}
                        </x-detail-row>
                        <x-detail-row label="Finished">
                            {{ $batch->finished_at?->format(setting('datetime_format', 'd M Y, H:i')) }}
                        </x-detail-row>
                    </x-detail-list>
                </x-panel>
            </div>
        </div>
    </div>
@endsection
