@extends('admin.layout')

@section('title', 'Darul Ifta')

@section('content')
    <div class="admin-heading">
        <div>
            <span>Moderation</span>
            <h1>Darul Ifta Questions</h1>
        </div>
    </div>

    <section class="admin-question-list">
        @forelse ($questions as $question)
            <article class="admin-card admin-question-card">
                <div class="admin-question-head">
                    <div>
                        <div class="moderation-badges">
                            <span class="status-pill">{{ strtoupper($question->locale) }}</span>
                            <span class="status-pill">{{ $question->category }}</span>
                            <span @class(['status-pill', 'is-pending' => ! $question->is_approved, 'is-live' => $question->is_approved])>
                                {{ $question->is_approved ? 'Published' : 'Pending review' }}
                            </span>
                            <span class="status-pill">{{ ucfirst($question->status) }}</span>
                        </div>
                        <h2>{{ $question->title }}</h2>
                        <p>{{ $question->body }}</p>
                        <small>{{ $question->user->name }} · {{ $question->created_at->diffForHumans() }}</small>
                    </div>
                    <form method="post" action="{{ route('admin.questions.destroy', $question) }}">
                        @csrf
                        @method('delete')
                        <button class="btn btn-ghost" type="submit">Delete</button>
                    </form>
                </div>

                <form class="admin-form-grid" method="post" action="{{ route('admin.questions.update', $question) }}">
                    @csrf
                    @method('put')
                    <input name="category" value="{{ $question->category }}" required>
                    <select name="status">
                        <option value="pending" @selected($question->status === 'pending')>Pending</option>
                        <option value="open" @selected($question->status === 'open')>Open</option>
                        <option value="answered" @selected($question->status === 'answered')>Answered</option>
                        <option value="closed" @selected($question->status === 'closed')>Closed</option>
                    </select>
                    <label class="check-row">
                        <input type="checkbox" name="is_approved" value="1" @checked($question->is_approved)>
                        <span>Publish question</span>
                    </label>
                    <button class="btn btn-primary" type="submit">Update</button>
                </form>

                <form class="admin-reply-create" method="post" action="{{ route('admin.questions.replies.store', $question) }}">
                    @csrf
                    <label>
                        <span>Official answer</span>
                        <textarea name="body" rows="3" placeholder="Write an official answer. It will be published immediately." required></textarea>
                    </label>
                    <button class="btn btn-primary" type="submit">Publish official answer</button>
                </form>

                <div class="reply-thread">
                    @foreach ($question->replies as $reply)
                        <form class="reply-item" method="post" action="{{ route('admin.replies.update', $reply) }}">
                            @csrf
                            @method('put')
                            <strong>
                                {{ $reply->user->name }}
                                @if($reply->is_official) · Official @endif
                                · {{ $reply->is_approved ? 'Published' : 'Pending' }}
                            </strong>
                            <textarea name="body" rows="3">{{ $reply->body }}</textarea>
                            <label class="check-row">
                                <input type="checkbox" name="is_official" value="1" @checked($reply->is_official)>
                                <span>Official answer</span>
                            </label>
                            <label class="check-row">
                                <input type="checkbox" name="is_approved" value="1" @checked($reply->is_approved)>
                                <span>Publish reply</span>
                            </label>
                            <button class="btn btn-ghost" type="submit">Save reply</button>
                        </form>
                    @endforeach
                </div>
            </article>
        @empty
            <div class="admin-card">No questions yet.</div>
        @endforelse
    </section>

    {{ $questions->links() }}
@endsection
