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

@section('content')
    <p style="margin: 12px 0;">
        <a href="{{ url('admin/octoparse') }}">&larr; Back to Octoparse dashboard</a>
    </p>
    <h1 style="margin-top: 0;">Octoparse — Import logs</h1>

    <table class="table table-condensed table-hover table-striped">
        <thead>
            <tr>
                <th>Started</th>
                <th>Finished</th>
                <th>Task</th>
                <th>Status</th>
                <th class="text-right">Fetched</th>
                <th class="text-right">Inserted</th>
                <th class="text-right">Updated</th>
                <th class="text-right">Failed</th>
                <th>New entries</th>
                <th>Mode</th>
                <th>Error</th>
            </tr>
        </thead>
        <tbody>
        @forelse ($logs as $log)
            @php
                $label = $tasks[$log->task_id]['label'] ?? $log->task_id;
                $badge = $log->status === 'success' ? 'success'
                    : ($log->status === 'failed' ? 'danger'
                        : ($log->status === 'partial' ? 'warning' : 'info'));
            @endphp
            <tr>
                <td>{{ optional($log->started_at)->format('Y-m-d H:i:s') }}</td>
                <td>{{ optional($log->finished_at)->format('Y-m-d H:i:s') ?: '—' }}</td>
                <td>
                    <a href="{{ url('admin/octoparse/tasks/' . $log->task_id) }}">{{ $label }}</a>
                    <br>
                    <small class="text-muted"><code>{{ $log->task_id }}</code></small>
                </td>
                <td><span class="label label-{{ $badge }}">{{ $log->status }}</span></td>
                <td class="text-right">{{ $log->rows_fetched }}</td>
                <td class="text-right">{{ $log->rows_inserted }}</td>
                <td class="text-right">{{ $log->rows_updated }}</td>
                <td class="text-right">{{ $log->rows_failed }}</td>
                <td>
                    @if ($log->new_record_ids === null)
                        <span class="text-muted" title="This import predates new-entries tracking">n/a</span>
                    @elseif ($log->rows_inserted > 0)
                        <a href="{{ url('admin/octoparse/logs/' . $log->id . '/new-entries') }}">
                            <span class="label label-success">{{ $log->rows_inserted }} new</span>
                        </a>
                    @else
                        <span class="text-muted">—</span>
                    @endif
                </td>
                <td>
                    @if ($log->dry_run)<span class="label label-default">dry-run</span>@endif
                    @if ($log->triggered_manually)<span class="label label-info">manual</span>@endif
                </td>
                <td>
                    @if ($log->error_message)
                        <span title="{{ $log->error_message }}">
                            {{ \Illuminate\Support\Str::limit($log->error_message, 80) }}
                        </span>
                    @else
                        —
                    @endif
                </td>
            </tr>
        @empty
            <tr>
                <td colspan="11" class="text-center text-muted">No imports recorded yet.</td>
            </tr>
        @endforelse
        </tbody>
    </table>

    <div class="text-center">
        {{ $logs->links() }}
    </div>
@stop
