@extends('admin.layout')

@section('header-title', 'Forum Gönderileri')

@section('content')
<div class="page-title">
    <h1>Forum Gönderileri</h1>
    <p>Tüm forum gönderilerini yönetin</p>
</div>

<div style="background:white;border-radius:12px;padding:24px;border:1px solid #e5e7eb;">
    {{-- Filters --}}
    <form method="GET" style="display:flex;gap:12px;margin-bottom:24px;flex-wrap:wrap;">
        <input type="text" name="search" placeholder="İçerik ara..." value="{{ request('search') }}"
               style="flex:1;min-width:200px;padding:10px 14px;border:1px solid #e5e7eb;border-radius:8px;font-size:14px;">
        <select name="topic" style="padding:10px 14px;border:1px solid #e5e7eb;border-radius:8px;font-size:14px;min-width:150px;">
            <option value="">Tüm Konular</option>
            @foreach($topics as $id => $title)
                <option value="{{ $id }}" {{ request('topic') == $id ? 'selected' : '' }}>{{ Str::limit($title, 40) }}</option>
            @endforeach
        </select>
        <button type="submit" style="padding:10px 20px;background:#3b82f6;color:white;border:none;border-radius:8px;font-weight:600;cursor:pointer;">
            Ara
        </button>
        @if(request()->hasAny(['search', 'topic']))
            <a href="{{ route('admin.forum-posts.index') }}" style="padding:10px 20px;background:#f3f4f6;color:#374151;border:none;border-radius:8px;font-weight:600;text-decoration:none;">
                Temizle
            </a>
        @endif
    </form>

    {{-- Posts Table --}}
    @if($posts->count() > 0)
        <div style="overflow-x:auto;">
            <table style="width:100%;border-collapse:collapse;">
                <thead>
                    <tr style="background:#f9fafb;border-bottom:2px solid #e5e7eb;">
                        <th style="padding:14px 16px;text-align:left;font-size:13px;font-weight:600;color:#6b7280;">Kullanıcı</th>
                        <th style="padding:14px 16px;text-align:left;font-size:13px;font-weight:600;color:#6b7280;">İçerik</th>
                        <th style="padding:14px 16px;text-align:left;font-size:13px;font-weight:600;color:#6b7280;">Konu</th>
                        <th style="padding:14px 16px;text-align:left;font-size:13px;font-weight:600;color:#6b7280;">Tarih</th>
                        <th style="padding:14px 16px;text-align:center;font-size:13px;font-weight:600;color:#6b7280;">İşlemler</th>
                    </tr>
                </thead>
                <tbody>
                    @foreach($posts as $post)
                        <tr style="border-bottom:1px solid #f3f4f6;transition:background 0.15s;" onmouseover="this.style.background='#f9fafb'" onmouseout="this.style.background='white'">
                            <td style="padding:14px 16px;">
                                <div style="display:flex;align-items:center;gap:10px;">
                                    <div style="width:32px;height:32px;border-radius:50%;background:linear-gradient(135deg, hsl({{ crc32($post->user->name ?? 'A') % 360 }}, 60%, 45%) 0%, hsl({{ (crc32($post->user->name ?? 'A') % 360) + 30 }}, 60%, 55%) 100%);display:flex;align-items:center;justify-content:center;color:white;font-weight:600;font-size:12px;flex-shrink:0;">
                                        {{ substr($post->user->name ?? 'A', 0, 1) }}
                                    </div>
                                    <span style="font-size:14px;font-weight:500;color:#1f2937;">{{ $post->user->name ?? 'Silinen Kullanıcı' }}</span>
                                </div>
                            </td>
                            <td style="padding:14px 16px;">
                                <div style="font-size:13px;color:#4b5563;line-height:1.5;max-width:400px;overflow:hidden;text-overflow:ellipsis;white-space:nowrap;">
                                    {{ Str::limit(strip_tags($post->content), 100) }}
                                </div>
                                @if($post->reply_to_id)
                                    <span style="display:inline-block;margin-top:4px;padding:2px 8px;background:#fef3c7;color:#92400e;font-size:11px;border-radius:4px;">
                                        Yanıt
                                    </span>
                                @endif
                            </td>
                            <td style="padding:14px 16px;">
                                <a href="{{ route('forum.topic', [$post->topic->category->slug ?? '#', $post->topic->slug ?? '#']) }}#post-{{ $post->id }}"
                                   target="_blank"
                                   style="font-size:13px;color:#3b82f6;text-decoration:none;font-weight:500;">
                                    {{ Str::limit($post->topic->title ?? 'Silinmiş Konu', 35) }}
                                </a>
                            </td>
                            <td style="padding:14px 16px;">
                                <span style="font-size:13px;color:#6b7280;">{{ $post->created_at->translatedFormat('d M Y, H:i') }}</span>
                            </td>
                            <td style="padding:14px 16px;text-align:center;">
                                <div style="display:flex;gap:6px;justify-content:center;">
                                    <a href="{{ route('admin.forum-posts.edit', $post) }}"
                                       style="padding:6px 12px;background:#eff6ff;color:#1d4ed8;border:none;border-radius:6px;font-size:12px;font-weight:600;text-decoration:none;cursor:pointer;">
                                        Düzenle
                                    </a>
                                    <form method="POST" action="{{ route('admin.forum-posts.destroy', $post) }}"
                                          onsubmit="return confirm('Bu gönderiyi silmek istediğinizden emin misiniz?')"
                                          style="display:inline;">
                                        @csrf
                                        @method('DELETE')
                                        <button type="submit"
                                                style="padding:6px 12px;background:#fef2f2;color:#dc2626;border:none;border-radius:6px;font-size:12px;font-weight:600;cursor:pointer;">
                                            Sil
                                        </button>
                                    </form>
                                </div>
                            </td>
                        </tr>
                    @endforeach
                </tbody>
            </table>
        </div>

        {{-- Pagination --}}
        <div style="margin-top:24px;display:flex;justify-content:center;">
            {{ $posts->appends(request()->only(['search', 'topic']))->links() }}
        </div>
    @else
        <div style="text-align:center;padding:48px;color:#9ca3af;">
            <div style="font-size:48px;margin-bottom:12px;">💬</div>
            <p style="font-size:16px;">Henüz gönderi bulunmuyor.</p>
        </div>
    @endif
</div>
@endsection
