@extends('admin.layouts.main', ['fluid' => true])

@section('content')
    <p style="margin: 12px 0;">
        <a href="{{ url('admin/octoparse/logs') }}">&larr; Back to Octoparse import logs</a>
    </p>
    <h1 style="margin-top: 0;">
        Octoparse — New entries from import #{{ $log->id }}
    </h1>
    <p>
        <small class="text-muted">Started at:</small>
        <code>{{ optional($log->started_at)->format('Y-m-d H:i:s') }}</code>
        &middot;
        <small class="text-muted">Finished at:</small>
        <code>{{ optional($log->finished_at)->format('Y-m-d H:i:s') ?: '—' }}</code>
        &middot;
        <small class="text-muted">Status:</small>
        @php
            $badge = $log->status === 'success' ? 'success'
                : ($log->status === 'failed' ? 'danger'
                    : ($log->status === 'partial' ? 'warning' : 'info'));
        @endphp
        <span class="label label-{{ $badge }}">{{ $log->status }}</span>
    </p>

    @if (!$trackingAvailable)
        <div class="alert alert-info">
            This import predates per-run new-entries tracking — only summary counts are available.
            Future imports will list their newly-inserted records here.
        </div>
    @elseif ($recordsByTask->isEmpty())
        <div class="alert alert-info">
            <strong>No new entries were imported in this run.</strong>
            All scraped rows already existed and were re-upserted as updates.
        </div>
    @else
        <div class="alert alert-success">
            <strong>{{ $log->rows_inserted }}</strong> new
            {{ $log->rows_inserted === 1 ? 'record was' : 'records were' }} inserted in this run,
            across {{ $recordsByTask->count() }}
            {{ $recordsByTask->count() === 1 ? 'task' : 'tasks' }}.
        </div>

        @foreach ($recordsByTask as $taskId => $records)
            @php
                $taskConfig = $tasks[$taskId] ?? [];
                $label = $taskConfig['label'] ?? $taskId;
            @endphp
            <h3 style="margin-top: 24px;">
                {{ $label }}
                <small><a href="{{ url('admin/octoparse/tasks/' . $taskId) }}">view all records →</a></small>
            </h3>
            <p style="margin: 0 0 8px 0;">
                <small class="text-muted">Task ID:</small> <code>{{ $taskId }}</code>
                &middot;
                <small class="text-muted">New rows:</small> <strong>{{ $records->count() }}</strong>
            </p>

            <table class="table table-condensed table-hover table-striped">
                <thead>
                    <tr>
                        <th style="width: 60px;">ID</th>
                        <th>External key</th>
                        <th>Payload (preview)</th>
                        <th style="width: 150px;">Scraped at</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach ($records as $record)
                        @php
                            $payloadJson = json_encode(
                                $record->payload,
                                JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE
                            );
                            $payloadPreview = mb_substr($payloadJson, 0, 240);
                            $modalId = 'octoparse-new-row-' . $record->id;
                        @endphp
                        <tr>
                            <td>{{ $record->id }}</td>
                            <td style="word-break: break-all;">{{ $record->external_key }}</td>
                            <td>
                                <code style="white-space: pre-wrap;">{{ $payloadPreview }}@if (mb_strlen($payloadJson) > 240)…@endif</code>
                                <br>
                                <a href="#" data-toggle="modal" data-target="#{{ $modalId }}">View full payload</a>
                                <div class="modal fade" id="{{ $modalId }}" tabindex="-1" role="dialog">
                                    <div class="modal-dialog modal-lg" role="document">
                                        <div class="modal-content">
                                            <div class="modal-header">
                                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                                    <span aria-hidden="true">&times;</span>
                                                </button>
                                                <h4 class="modal-title">Record #{{ $record->id }} — payload</h4>
                                            </div>
                                            <div class="modal-body">
                                                <pre style="max-height: 480px; overflow: auto;">{{ $payloadJson }}</pre>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                            </td>
                            <td>{{ optional($record->scraped_at)->format('Y-m-d H:i') ?: '—' }}</td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        @endforeach
    @endif
@stop
