@extends('admin.layouts.main')

@section('content')
    <h1>Octoparse — Tasks &amp; data</h1>

    @if(!$syncEnabled)
        <div class="alert alert-warning">
            <strong>Scheduled sync is disabled.</strong>
            Set <code>OCTOPARSE_SYNC_ENABLED=true</code> in <code>.env</code> to enable the daily cron import.
        </div>
    @endif

    @if (count($tasks) === 0)
        <div class="alert alert-info">
            No Octoparse tasks registered yet. Add entries under <code>tasks</code> in <code>config/octoparse.php</code>.
        </div>
    @else
        <div class="row" style="display: flex; flex-wrap: wrap;">
            @foreach ($tasks as $taskId => $taskConfig)
                @php
                    $countRow = $counts[$taskId] ?? null;
                    $log = $lastLogs[$taskId] ?? null;
                    $enabled = (bool) ($taskConfig['enabled'] ?? false);
                @endphp
                <div class="col-md-6 col-lg-4" style="margin-bottom: 20px; display: flex;">
                    <div class="panel panel-default" style="display: flex; flex-direction: column; width: 100%;">
                        <div class="panel-heading">
                            <strong>{{ $taskConfig['label'] ?? $taskId }}</strong>
                            @if (!$enabled)
                                <span class="label label-default pull-right">disabled</span>
                            @endif
                        </div>
                        <div class="panel-body" style="flex: 1;">
                            <p style="margin: 0 0 6px 0;">
                                <small class="text-muted">Task ID:</small>
                                <code>{{ $taskId }}</code>
                            </p>
                            @if (!empty($taskConfig['unique_key_field']))
                                <p style="margin: 0 0 6px 0;">
                                    <small class="text-muted">Unique key:</small>
                                    <code>{{ $taskConfig['unique_key_field'] }}</code>
                                </p>
                            @endif
                            <p style="margin: 0 0 6px 0;">
                                <small class="text-muted">Stored records:</small>
                                <strong>{{ $countRow->total ?? 0 }}</strong>
                            </p>
                            <p style="margin: 0 0 6px 0;">
                                <small class="text-muted">Last import:</small>
                                @if ($log)
                                    <span style="margin-right: 5px;" class="label label-{{ $log->status === 'success' ? 'success' : ($log->status === 'failed' ? 'danger' : ($log->status === 'partial' ? 'warning' : 'info')) }}">
                                        {{ $log->status }}
                                    </span>
                                    <small>{{ optional($log->started_at)->format('Y-m-d H:i') }}</small>
                                @else
                                    <em>never</em>
                                @endif
                            </p>
                        </div>
                        <div class="panel-footer">
                            <a href="{{ url('admin/octoparse/tasks/' . $taskId) }}" class="btn btn-default btn-sm">
                                View records
                            </a>
                            @if ($canRun && $enabled)
                                <form method="post" action="{{ url('admin/octoparse/run/' . $taskId) }}" style="display:inline;">
                                    @csrf
                                    <button type="submit" class="btn btn-primary btn-sm">Run now</button>
                                </form>
                            @endif
                        </div>
                    </div>
                </div>
            @endforeach
        </div>

        @if ($canRun)
            <div style="margin-top: 12px;">
                <form method="post" action="{{ url('admin/octoparse/run') }}" style="display:inline;">
                    @csrf
                    <button type="submit" class="btn btn-primary">Run all enabled tasks</button>
                </form>
                <a href="{{ url('admin/octoparse/logs') }}" class="btn btn-default">View import logs</a>
            </div>
        @endif
    @endif
@stop
