@extends('admin.layout')

@section('header-title', 'Yorum Detayı')

@push('styles')
<link href="https://cdn.quilljs.com/1.3.6/quill.snow.css" rel="stylesheet">
<style>
    .page-header {
        margin-bottom: 32px;
    }
    .page-title {
        font-size: 24px;
        font-weight: 800;
        color: #1f2937;
    }
    .page-subtitle {
        font-size: 14px;
        color: #6b7280;
        margin-top: 4px;
    }

    .card {
        background: white;
        border: 1px solid #e5e7eb;
        border-radius: 10px;
        overflow: hidden;
        box-shadow: 0 1px 2px rgba(0,0,0,0.05);
        margin-bottom: 20px;
    }
    .card-header {
        background: #f9fafb;
        border-bottom: 1px solid #e5e7eb;
        padding: 20px;
    }
    .card-title {
        font-size: 16px;
        font-weight: 700;
        color: #1f2937;
        margin: 0;
    }
    .card-body {
        padding: 20px;
    }

    .flash-msg {
        padding: 16px;
        background: #d1fae5;
        border: 1px solid #10b981;
        border-radius: 8px;
        color: #065f46;
        margin-bottom: 20px;
        font-size: 14px;
    }

    .comment-bubble {
        background: #f9fafb;
        border: 1px solid #e5e7eb;
        border-radius: 8px;
        padding: 16px;
        margin-bottom: 12px;
    }
    .comment-bubble.admin-reply {
        background: #eff6ff;
        border-color: #bfdbfe;
        margin-left: 32px;
    }
    .comment-meta {
        display: flex;
        align-items: center;
        gap: 12px;
        margin-bottom: 8px;
        font-size: 13px;
        color: #6b7280;
    }
    .avatar {
        width: 36px;
        height: 36px;
        border-radius: 50%;
        background: #1e40af;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-weight: 700;
        font-size: 14px;
        flex-shrink: 0;
    }
    .avatar.admin {
        background: #7c3aed;
    }
    .author-name {
        font-weight: 600;
        color: #1f2937;
    }
    .comment-content {
        font-size: 14px;
        color: #374151;
        line-height: 1.6;
    }
    .admin-badge {
        margin-left: auto;
        padding: 2px 8px;
        background: #7c3aed;
        color: white;
        border-radius: 12px;
        font-size: 11px;
        font-weight: 600;
    }

    .form-group {
        margin-bottom: 16px;
    }
    .form-control {
        width: 100%;
        padding: 10px 12px;
        border: 1px solid #e5e7eb;
        border-radius: 6px;
        font-size: 14px;
        font-family: inherit;
        outline: none;
    }
    .form-control:focus {
        border-color: #1e40af;
        box-shadow: 0 0 0 3px rgba(30, 64, 175, 0.1);
    }
    textarea.form-control {
        resize: vertical;
        min-height: 150px;
    }
    .form-error {
        color: #ef4444;
        font-size: 12px;
        margin-top: 4px;
    }

    .btn {
        padding: 10px 20px;
        border: none;
        border-radius: 6px;
        font-weight: 600;
        font-size: 14px;
        cursor: pointer;
        text-decoration: none;
        display: inline-block;
        transition: all 0.2s;
    }
    .btn-primary {
        background: #1e40af;
        color: white;
    }
    .btn-primary:hover {
        background: #1e3a8a;
    }
    .btn-success {
        background: #10b981;
        color: white;
    }
    .btn-success:hover {
        background: #059669;
    }
    .btn-warning {
        background: #f59e0b;
        color: white;
    }
    .btn-warning:hover {
        background: #d97706;
    }
    .btn-danger {
        background: #fee2e2;
        color: #dc2626;
    }
    .btn-danger:hover {
        background: #fecaca;
    }
    .btn-secondary {
        background: #e5e7eb;
        color: #1f2937;
    }
    .btn-secondary:hover {
        background: #d1d5db;
    }
    .btn-block {
        width: 100%;
        text-align: center;
    }

    .info-list {
        font-size: 13px;
        color: #374151;
        line-height: 1.8;
    }
    .info-list a {
        color: #1e40af;
        text-decoration: none;
    }
    .info-list a:hover {
        text-decoration: underline;
    }

    .badge {
        display: inline-block;
        padding: 6px 14px;
        border-radius: 20px;
        font-size: 13px;
        font-weight: 600;
    }
    .badge-green {
        background: #d1fae5;
        color: #059669;
    }
    .badge-yellow {
        background: #fef3c7;
        color: #d97706;
    }

    .action-buttons {
        display: flex;
        flex-direction: column;
        gap: 8px;
    }

    .grid-2 {
        display: grid;
        grid-template-columns: 2fr 1fr;
        gap: 24px;
        max-width: 900px;
    }

    /* Quill Editor Customization */
    #reply_editor {
        min-height: 150px;
        background: white;
        border-radius: 8px;
    }
    #reply_editor .ql-toolbar {
        border-top-left-radius: 8px;
        border-top-right-radius: 8px;
        border-color: #e5e7eb;
    }
    #reply_editor .ql-container {
        border-bottom-left-radius: 8px;
        border-bottom-right-radius: 8px;
        border-color: #e5e7eb;
        min-height: 130px;
        font-size: 14px;
    }
    #reply_editor .ql-editor {
        min-height: 130px;
        padding: 12px;
    }
    #reply_editor .ql-editor.ql-blank::before {
        color: #9ca3af;
        font-style: normal;
    }

    @media (max-width: 768px) {
        .grid-2 {
            grid-template-columns: 1fr;
        }
    }
</style>
@endpush

@section('content')
<div class="page-header">
    <div>
        <h1 class="page-title">💬 Yorum Detayı</h1>
        <p class="page-subtitle">{{ $comment->post->title }}</p>
    </div>
</div>

@if(session('success'))
    <div class="flash-msg">
        {{ session('success') }}
    </div>
@endif

<div class="grid-2">
    {{-- Main Content --}}
    <div>
        {{-- Original Comment --}}
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">Yorum</h2>
            </div>
            <div class="card-body">
                <div class="comment-bubble">
                    <div class="comment-meta">
                        <div class="avatar">{{ substr($comment->author_name, 0, 1) }}</div>
                        <div>
                            <div class="author-name">{{ $comment->author_name }}</div>
                            <div>{{ $comment->author_email }} · {{ $comment->created_at->format('d.m.Y H:i') }}</div>
                        </div>
                    </div>
                    <div class="comment-content">
                        {!! nl2br(e($comment->content)) !!}
                    </div>
                </div>

                {{-- Existing Replies --}}
                @if($comment->replies && $comment->replies->count() > 0)
                    <h3 style="font-size: 14px; font-weight: 600; color: #1f2937; margin: 16px 0 12px;">Cevaplar</h3>
                    @foreach($comment->replies as $reply)
                        <div class="comment-bubble {{ $reply->user_id ? 'admin-reply' : '' }}">
                            <div class="comment-meta">
                                <div class="avatar {{ $reply->user_id ? 'admin' : '' }}">
                                    {{ $reply->user_id ? 'A' : substr($reply->author_name ?? 'U', 0, 1) }}
                                </div>
                                <div>
                                    <div class="author-name">
                                        {{ $reply->user_id ? $reply->user->name . ' (Admin)' : ($reply->author_name ?? 'Kullanıcı') }}
                                    </div>
                                    <div>{{ $reply->created_at->format('d.m.Y H:i') }}</div>
                                </div>
                                @if($reply->user_id)
                                    <span class="admin-badge">YÖNETİCİ</span>
                                @endif
                            </div>
                            <div class="comment-content">
                                {!! nl2br(e($reply->content)) !!}
                            </div>
                            <div style="margin-top: 8px;">
                                <form method="POST" action="{{ route('admin.blog-comments.destroy', $reply) }}" style="display: inline;" onsubmit="return confirm('Bu cevabı silmek istediğinizden emin misiniz?');">
                                    @csrf
                                    @method('DELETE')
                                    <button type="submit" class="btn btn-danger" style="padding: 4px 10px; font-size: 12px;">Sil</button>
                                </form>
                            </div>
                        </div>
                    @endforeach
                @endif
            </div>
        </div>

        {{-- Admin Reply Form --}}
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">Admin Cevabı</h2>
            </div>
            <div class="card-body">
                <form method="POST" action="{{ route('admin.blog-comments.reply', $comment) }}" id="replyForm">
                    @csrf
                    <div class="form-group">
                        <div id="reply_editor" placeholder="Cevabınızı yazın..."></div>
                        <input type="hidden" id="content" name="content">
                        @error('content')
                            <div class="form-error">{{ $message }}</div>
                        @enderror
                    </div>
                    <button type="submit" class="btn btn-primary">
                        Cevabı Gönder
                    </button>
                </form>
            </div>
        </div>
    </div>

    {{-- Sidebar --}}
    <div>
        {{-- Comment Info --}}
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">📋 Yorum Bilgisi</h2>
            </div>
            <div class="card-body">
                <div class="info-list">
                    <div><strong>Yazı:</strong> <a href="{{ route('blog.show', $comment->post) }}" target="_blank">{{ Str::limit($comment->post->title, 40) }}</a></div>
                    <div><strong>Tarih:</strong> {{ $comment->created_at->format('d.m.Y H:i') }}</div>
                    <div><strong>IP:</strong> {{ $comment->author_ip ?? 'Bilinmiyor' }}</div>
                    @if($comment->parent)
                        <div><strong>Cevap:</strong> {{ $comment->parent->author_name }}'in yorumuna</div>
                    @endif
                </div>
            </div>
        </div>

        {{-- Status & Actions --}}
        <div class="card">
            <div class="card-header">
                <h2 class="card-title">📊 Durum</h2>
            </div>
            <div class="card-body">
                <div style="margin-bottom: 16px;">
                    @if($comment->is_approved)
                        <span class="badge badge-green">✓ Onaylı</span>
                    @else
                        <span class="badge badge-yellow">⏳ Bekliyor</span>
                    @endif
                </div>
                <div class="action-buttons">
                    @if(!$comment->is_approved)
                        <form method="POST" action="{{ route('admin.blog-comments.approve', $comment) }}">
                            @csrf
                            <button type="submit" class="btn btn-success btn-block">
                                ✓ Onayla
                            </button>
                        </form>
                    @else
                        <form method="POST" action="{{ route('admin.blog-comments.reject', $comment) }}">
                            @csrf
                            <button type="submit" class="btn btn-warning btn-block">
                                ✗ Reddet
                            </button>
                        </form>
                    @endif
                    <a href="{{ route('admin.blog-comments.edit', $comment) }}" class="btn btn-primary btn-block">
                        Düzenle
                    </a>
                    <form method="POST" action="{{ route('admin.blog-comments.destroy', $comment) }}" onsubmit="return confirm('Bu yorumu silmek istediğinizden emin misiniz?');">
                        @csrf
                        @method('DELETE')
                        <button type="submit" class="btn btn-danger btn-block">
                            🗑️ Sil
                        </button>
                    </form>
                </div>
            </div>
        </div>

        <a href="{{ route('admin.blog-comments.index') }}" class="btn btn-secondary btn-block">
            ← Yorumlara Dön
        </a>
    </div>
</div>

<!-- Quill Editor for Reply -->
<script src="https://cdn.quilljs.com/1.3.6/quill.min.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Initialize Quill editor
    var quill = new Quill('#reply_editor', {
        theme: 'snow',
        modules: {
            toolbar: [
                ['bold', 'italic', 'underline', 'strike'],
                ['blockquote', 'code-block'],
                [{ 'list': 'ordered'}, { 'list': 'bullet' }],
                [{ 'color': [] }, { 'background': [] }],
                ['link'],
                ['clean']
            ]
        },
        placeholder: 'Cevabınızı yazın...'
    });

    // Form submission handler
    document.getElementById('replyForm').addEventListener('submit', function(e) {
        // Get HTML content from Quill editor
        var content = quill.root.innerHTML.trim();
        
        // Set the hidden input value
        document.getElementById('content').value = content;
        
        // Validate content
        if (!content || content === '<p><br></p>' || content === '<p></p>') {
            e.preventDefault();
            alert('Cevap içeriği boş olamaz.');
            return false;
        }
    });
});
</script>
@endsection
