@extends('admin.layouts.app')
@section('title', 'Blog Yönetimi')
@section('page-title', 'Blog Yazıları')

@section('content')

<div class="page-header">
    <div>
        <h5 style="font-size:18px;font-weight:700;color:var(--text);margin:0">Blog Yazıları</h5>
        <div style="font-size:13px;color:var(--text2);margin-top:2px">{{ $stats['total'] }} yazı</div>
    </div>
    <div class="page-header-actions">
        <form action="{{ route('admin.blog.index') }}" method="GET" class="d-flex gap-2">
            <div class="search-input-wrap">
                <i class="bi bi-search"></i>
                <input type="text" name="q" value="{{ $q }}" placeholder="Başlık ara..." class="form-control" style="width:200px">
            </div>
            <button class="btn btn-secondary btn-sm"><i class="bi bi-search"></i></button>
        </form>
        <form action="{{ route('admin.blog.bulk-generate') }}" method="POST">
            @csrf
            <input type="hidden" name="count" value="3">
            <button type="submit" class="btn btn-sm"
                    style="background:linear-gradient(135deg,var(--accent),var(--accent-lt));color:#fff;border:none"
                    onclick="return confirm('3 yeni AI blog yazısı üretilsin mi? (2-3 dakika sürebilir)')">
                <i class="bi bi-stars"></i> AI ile 3 Yazı Üret
            </button>
        </form>
        <a href="{{ route('admin.blog.create') }}" class="btn btn-primary btn-sm">
            <i class="bi bi-plus-lg"></i> Yeni Yazı
        </a>
    </div>
</div>

{{-- Stats --}}
<div class="row g-3 mb-4">
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-card-top">
                <div><div class="stat-value">{{ $stats['total'] }}</div><div class="stat-label mt-1">Toplam</div></div>
                <div class="stat-icon violet"><i class="bi bi-journal-richtext"></i></div>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-card-top">
                <div><div class="stat-value" style="color:var(--success)">{{ $stats['published'] }}</div><div class="stat-label mt-1">Yayında</div></div>
                <div class="stat-icon green"><i class="bi bi-check-circle-fill"></i></div>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-card-top">
                <div><div class="stat-value" style="color:var(--text2)">{{ $stats['draft'] }}</div><div class="stat-label mt-1">Taslak</div></div>
                <div class="stat-icon cyan"><i class="bi bi-file-earmark-text"></i></div>
            </div>
        </div>
    </div>
    <div class="col-6 col-md-3">
        <div class="stat-card">
            <div class="stat-card-top">
                <div><div class="stat-value" style="color:var(--accent-lt)">{{ $stats['ai'] }}</div><div class="stat-label mt-1">AI Üretimi</div></div>
                <div class="stat-icon violet"><i class="bi bi-stars"></i></div>
            </div>
        </div>
    </div>
</div>

{{-- Tablo --}}
<div class="table-wrap">
    <div class="table-responsive">
        <table class="table">
            <thead>
                <tr>
                    <th style="padding-left:20px">Başlık</th>
                    <th>Anime</th>
                    <th style="text-align:center">Durum</th>
                    <th style="text-align:center">Kaynak</th>
                    <th style="text-align:center">Görüntülenme</th>
                    <th>Tarih</th>
                    <th style="text-align:right;padding-right:20px">İşlem</th>
                </tr>
            </thead>
            <tbody>
                @forelse($posts as $post)
                <tr>
                    <td style="padding-left:20px;max-width:280px">
                        <div style="font-size:13.5px;font-weight:700;color:var(--text);white-space:nowrap;overflow:hidden;text-overflow:ellipsis">{{ $post->title }}</div>
                        @if($post->focus_keyword)
                            <div style="font-size:12px;color:var(--text3)">{{ $post->focus_keyword }}</div>
                        @endif
                    </td>
                    <td>
                        @if($post->anime)
                            <span style="font-size:13px;color:var(--info)">{{ Str::limit($post->anime->title,25) }}</span>
                        @else
                            <span style="color:var(--text3)">—</span>
                        @endif
                    </td>
                    <td style="text-align:center">
                        @if($post->status === 'published')
                            <span class="badge bg-success">Yayında</span>
                        @elseif($post->status === 'generating')
                            <span class="badge bg-warning" style="color:#000">Üretiliyor</span>
                        @else
                            <span class="badge bg-secondary">Taslak</span>
                        @endif
                    </td>
                    <td style="text-align:center">
                        @if($post->ai_generated)
                            <span style="font-size:12.5px;color:var(--accent-lt);font-weight:700">
                                <i class="bi bi-stars"></i> AI
                            </span>
                        @else
                            <span style="font-size:12.5px;color:var(--text2)">
                                <i class="bi bi-pencil"></i> Manuel
                            </span>
                        @endif
                    </td>
                    <td style="text-align:center;font-size:13px;color:var(--text)">{{ number_format($post->views) }}</td>
                    <td style="font-size:12.5px;color:var(--text2)">{{ $post->created_at->format('d.m.Y') }}</td>
                    <td style="text-align:right;padding-right:20px">
                        <div class="d-flex gap-2 justify-content-end">
                            @if($post->status === 'published')
                            <a href="{{ route('blog.show', $post->slug) }}" target="_blank" class="btn btn-sm btn-secondary" title="Görüntüle">
                                <i class="bi bi-eye"></i>
                            </a>
                            @endif
                            <a href="{{ route('admin.blog.edit', $post) }}" class="btn btn-sm btn-secondary">
                                <i class="bi bi-pencil"></i>
                            </a>
                            <form action="{{ route('admin.blog.destroy', $post) }}" method="POST"
                                  onsubmit="return confirm('Bu yazı silinsin mi?')">
                                @csrf @method('DELETE')
                                <button class="btn btn-sm btn-outline-danger"><i class="bi bi-trash3"></i></button>
                            </form>
                        </div>
                    </td>
                </tr>
                @empty
                <tr>
                    <td colspan="7">
                        <div class="empty-state">
                            <i class="bi bi-journal-richtext"></i>
                            <h6>Henüz blog yazısı yok</h6>
                            <p>Yeni yazı ekleyin veya AI ile otomatik üretin.</p>
                            <a href="{{ route('admin.blog.create') }}" class="btn btn-primary btn-sm mt-2">
                                <i class="bi bi-plus-lg"></i> Yeni Yazı
                            </a>
                        </div>
                    </td>
                </tr>
                @endforelse
            </tbody>
        </table>
    </div>
</div>

@if($posts->hasPages())
<div class="mt-3">{{ $posts->appends(['q'=>$q])->links('admin.partials.pagination') }}</div>
@endif

@endsection
