@extends('admin.layouts.app')
@section('title', 'Import #' . $import->id)
@section('page-title', 'Import #' . $import->id)

@section('content')
<div class="mb-3">
    <a href="{{ route('admin.import.index') }}" class="text-muted small"><i class="bi bi-arrow-left me-1"></i>Import Listesi</a>
</div>

<div class="row g-4">
    <div class="col-lg-6">
        <div class="card mb-4">
            <h6 class="mb-3">Import Detayı</h6>
            <table class="table table-dark table-sm mb-0">
                <tr><td class="text-muted small">Anime</td><td class="small">{{ $import->anime_title ?: 'Anime #'.$import->watch_id }}</td></tr>
                <tr><td class="text-muted small">Watch ID</td><td class="small">{{ $import->watch_id }}</td></tr>
                <tr><td class="text-muted small">URL</td><td class="small text-truncate" style="max-width:200px">{{ $import->source_url }}</td></tr>
                <tr>
                    <td class="text-muted small">Durum</td>
                    <td><span class="badge bg-{{ $import->status_color }}">{{ $import->status_label }}</span></td>
                </tr>
                <tr><td class="text-muted small">Bölümler</td><td class="small">{{ $import->done_episodes }} / {{ $import->total_episodes }}</td></tr>
                <tr><td class="text-muted small">Hatalı</td><td class="small {{ $import->failed_episodes > 0 ? 'text-danger' : '' }}">{{ $import->failed_episodes }}</td></tr>
                <tr><td class="text-muted small">Oluşturulma</td><td class="small">{{ $import->created_at->format('d.m.Y H:i') }}</td></tr>
                @if($import->anime)
                <tr>
                    <td class="text-muted small">Anime Kaydı</td>
                    <td><a href="{{ route('admin.animes.show', $import->anime) }}" class="small text-info">{{ $import->anime->title }}</a></td>
                </tr>
                @endif
            </table>
        </div>

        @if($import->total_episodes > 0)
        <div class="card mb-4">
            <div class="d-flex justify-content-between mb-2">
                <span class="small">İlerleme</span>
                <span class="small">%{{ $import->progress_percent }}</span>
            </div>
            <div class="progress mb-2" style="height:8px">
                <div class="progress-bar bg-{{ $import->status_color }}" style="width:{{ $import->progress_percent }}%"></div>
            </div>
            <div class="text-muted small">{{ $import->done_episodes }}/{{ $import->total_episodes }} bölüm tamamlandı</div>
        </div>
        @endif

        @if($import->current_step)
        <div class="card mb-4">
            <h6 class="mb-2">Şu An</h6>
            <p class="small mb-0 text-info">{{ $import->current_step }}</p>
        </div>
        @endif

        @if($import->error_log)
        <div class="card mb-4">
            <h6 class="mb-2 text-danger">Hata Logu</h6>
            <pre style="background:var(--surface);padding:10px;border-radius:6px;font-size:.75rem;color:var(--danger);max-height:200px;overflow-y:auto">{{ $import->error_log }}</pre>
        </div>
        @endif

        <form method="POST" action="{{ route('admin.import.destroy', $import) }}" onsubmit="return confirm('Bu import job silinsin mi?')">
            @csrf @method('DELETE')
            <button class="btn btn-outline-danger btn-sm"><i class="bi bi-trash me-1"></i>Job'u Sil</button>
        </form>
    </div>

    <div class="col-lg-6">
        <div class="card">
            <h6 class="mb-3">Bu Job İçin Python Komutları</h6>
            <p class="text-muted small">Sadece bu job'u çalıştırmak için:</p>
            <pre style="background:var(--surface);border:1px solid var(--border2);border-radius:8px;padding:12px;font-size:.78rem;color:var(--info)">cd C:\Users\yusuf\Desktop\anizium_scraper
python bot.py --job-id {{ $import->id }}</pre>

            <p class="text-muted small mt-3">Tüm bekleyen job'ları çalıştırmak için:</p>
            <pre style="background:var(--surface);border:1px solid var(--border2);border-radius:8px;padding:12px;font-size:.78rem;color:var(--info)">python bot.py</pre>

            @if($import->status === 'pending')
            <div class="alert alert-warning small mt-3 mb-0">
                <i class="bi bi-clock me-1"></i>Bu job bekliyor. Python script'i başlatın.
            </div>
            @elseif($import->status === 'done')
            <div class="alert alert-success small mt-3 mb-0">
                <i class="bi bi-check-circle me-1"></i>Tamamlandı!
                @if($import->anime)
                    <a href="{{ route('admin.animes.show', $import->anime) }}" class="alert-link">Anime'yi görüntüle →</a>
                @endif
            </div>
            @elseif(in_array($import->status, ['downloading', 'uploading', 'fetching']))
            <div class="alert alert-info small mt-3 mb-0">
                <i class="bi bi-arrow-clockwise me-1"></i>İşlem devam ediyor... Sayfayı yenile.
            </div>
            @endif
        </div>
    </div>
</div>
@endsection
@push('scripts')
@if(in_array($import->status, ['fetching', 'downloading', 'uploading']))
<script>
// Aktif job ise 5 saniyede bir yenile
setTimeout(() => location.reload(), 5000);
</script>
@endif
@endpush
