@extends('admin.layout')

@section('header-title', 'Forum Konusu Düzenle')

@section('content')
<div class="page-header mb-4">
    <h1 class="page-title">Forum Konusu Düzenle</h1>
    <p class="text-muted">{{ $forumTopic->title }}</p>
</div>

<div class="row">
    <div class="col-lg-8">
        <div class="card">
            <div class="card-body">
                <form action="{{ route('admin.forum-topics.update', $forumTopic) }}" method="POST">
                    @csrf
                    @method('PUT')

                    <div class="mb-3">
                        <label class="form-label">Konu Başlığı <span class="text-danger">*</span></label>
                        <input type="text" name="title" value="{{ old('title', $forumTopic->title) }}" class="form-control @error('title') is-invalid @enderror" required autofocus>
                        @error('title')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="mb-3">
                        <label class="form-label">Kategori <span class="text-danger">*</span></label>
                        <select name="category_id" class="form-select @error('category_id') is-invalid @enderror" required>
                            @foreach($categories as $id => $name)
                                <option value="{{ $id }}" {{ old('category_id', $forumTopic->category_id) == $id ? 'selected' : '' }}>{{ $name }}</option>
                            @endforeach
                        </select>
                        @error('category_id')
                            <div class="invalid-feedback">{{ $message }}</div>
                        @enderror
                    </div>

                    <div class="row">
                        <div class="col-md-6">
                            <div class="form-check form-switch mb-3">
                                <input type="checkbox" name="is_sticky" class="form-check-input" id="is_sticky" {{ old('is_sticky', $forumTopic->is_sticky) ? 'checked' : '' }}>
                                <label class="form-check-label" for="is_sticky">
                                    <strong>Sabit Konu</strong>
                                    <div class="form-text">Sabit konular her zaman üstte görünür</div>
                                </label>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="form-check form-switch mb-3">
                                <input type="checkbox" name="is_locked" class="form-check-input" id="is_locked" {{ old('is_locked', $forumTopic->is_locked) ? 'checked' : '' }}>
                                <label class="form-check-label" for="is_locked">
                                    <strong>Kilitli Konu</strong>
                                    <div class="form-text">Kilitli konulara yanıt yazılamaz</div>
                                </label>
                            </div>
                        </div>
                    </div>

                    <div class="d-flex gap-2 mt-3">
                        <button type="submit" class="btn btn-primary">
                            <i class="ti ti-check me-2"></i>Güncelle
                        </button>
                        <a href="{{ route('admin.forum-topics.index') }}" class="btn btn-secondary">İptal</a>
                        <form action="{{ route('admin.forum-topics.destroy', $forumTopic) }}" method="POST" class="ms-auto" onsubmit="return confirm('Bu konuyu silmek istediğinizden emin misiniz?');">
                            @csrf
                            @method('DELETE')
                            <button type="submit" class="btn btn-danger">
                                <i class="ti ti-trash me-2"></i>Sil
                            </button>
                        </form>
                    </div>
                </form>
            </div>
        </div>

        <div class="card mt-3">
            <div class="card-header">
                <h3 class="card-title">Konu İçeriği</h3>
            </div>
            <div class="card-body">
                <div class="p-3 bg-light rounded" style="white-space: pre-wrap;">{{ $forumTopic->content }}</div>
            </div>
        </div>

        <div class="card mt-3">
            <div class="card-header">
                <h3 class="card-title">Yanıtlar ({{ $forumTopic->posts->count() }})</h3>
            </div>
            <div class="list-group list-group-flush">
                @forelse($forumTopic->posts as $post)
                <div class="list-group-item">
                    <div class="d-flex justify-content-between align-items-start">
                        <div>
                            <div class="d-flex align-items-center gap-2 mb-2">
                                <strong>{{ $post->user->name }}</strong>
                                <span class="text-muted" style="font-size:12px;">{{ $post->created_at->format('d.m.Y H:i') }}</span>
                            </div>
                            <div style="white-space: pre-wrap;">{{ $post->content }}</div>
                        </div>
                        <div class="text-end">
                            <div class="text-success">👍 {{ $post->likes }}</div>
                            <div class="text-danger">👎 {{ $post->dislikes }}</div>
                        </div>
                    </div>
                </div>
                @empty
                <div class="list-group-item text-center text-muted">Henüz yanıt yok.</div>
                @endforelse
            </div>
        </div>
    </div>

    <div class="col-lg-4">
        <div class="card">
            <div class="card-header">
                <h3 class="card-title">Konu Bilgileri</h3>
            </div>
            <div class="card-body">
                <table class="table table-sm table-vcenter">
                    <tr>
                        <td class="text-muted w-50">Slug:</td>
                        <td><code>{{ $forumTopic->slug }}</code></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Yazar:</td>
                        <td><strong>{{ $forumTopic->user->name }}</strong></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Kategori:</td>
                        <td>{{ $forumTopic->category->name }}</td>
                    </tr>
                    <tr>
                        <td class="text-muted">Yanıt Sayısı:</td>
                        <td><strong>{{ $forumTopic->reply_count }}</strong></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Görüntülenme:</td>
                        <td><strong>{{ $forumTopic->views }}</strong></td>
                    </tr>
                    <tr>
                        <td class="text-muted">Son Yanıt:</td>
                        <td>{{ $forumTopic->last_reply_at?->diffForHumans() ?? '-' }}</td>
                    </tr>
                    <tr>
                        <td class="text-muted">Durum:</td>
                        <td>
                            @if($forumTopic->is_sticky)
                                <span class="badge bg-warning">Sabit</span>
                            @endif
                            @if($forumTopic->is_locked)
                                <span class="badge bg-danger">Kilitli</span>
                            @endif
                            @if(!$forumTopic->is_sticky && !$forumTopic->is_locked)
                                <span class="badge bg-success">Normal</span>
                            @endif
                        </td>
                    </tr>
                    <tr>
                        <td class="text-muted">Oluşturulma:</td>
                        <td>{{ $forumTopic->created_at->format('d.m.Y H:i') }}</td>
                    </tr>
                </table>
            </div>
        </div>

        <div class="card mt-3">
            <div class="card-header">
                <h3 class="card-title">Hızlı İşlemler</h3>
            </div>
            <div class="card-body">
                <form action="{{ route('admin.forum-topics.toggle_sticky', $forumTopic) }}" method="POST" class="mb-2">
                    @csrf
                    <button type="submit" class="btn btn-{{ $forumTopic->is_sticky ? 'warning' : 'outline-warning' }} w-100">
                        <i class="ti ti-pin me-2"></i>{{ $forumTopic->is_sticky ? 'Sabeti Kaldır' : 'Sabit Yap' }}
                    </button>
                </form>
                <form action="{{ route('admin.forum-topics.toggle_locked', $forumTopic) }}" method="POST">
                    @csrf
                    <button type="submit" class="btn btn-{{ $forumTopic->is_locked ? 'danger' : 'outline-danger' }} w-100">
                        <i class="ti ti-lock me-2"></i>{{ $forumTopic->is_locked ? 'Kilidi Aç' : 'Kilit Yap' }}
                    </button>
                </form>
            </div>
        </div>
    </div>
</div>
@endsection
