@extends('site.layout')

@php
    $iftaUi = [
        'ps' => ['title' => 'شرعي مسايل، پوښتنې او لارښوونې', 'intro' => 'پوښتنې او د کارن ځوابونه د ادارې تر کتنې وروسته خپرېږي؛ رسمي ځوابونه سمدستي ښکاره کېږي.', 'samples' => 'نمونه پوښتنې', 'latest' => 'وروستۍ مسئلې', 'search' => 'لټون...', 'ask' => 'مسئله وپوښتئ', 'ask_title' => 'خپله پوښتنه وليکئ', 'login_note' => 'د پوښتنې او ځواب لپاره حساب ته ننوځئ.', 'title_label' => 'عنوان', 'category' => 'کتګوري', 'body' => 'پوښتنه', 'submit' => 'د کتنې لپاره ثبت کړئ', 'reply' => 'ځواب وليکئ', 'reply_submit' => 'ځواب ولېږئ', 'official' => 'رسمي ځواب', 'no_questions' => 'تر اوسه پوښتنه نه ده خپره شوې.', 'statuses' => ['pending' => 'د کتنې لاندې', 'open' => 'پرانيستې', 'answered' => 'ځواب شوې', 'closed' => 'تړل شوې']],
        'fa' => ['title' => 'مسائل شرعی، پرسش‌ها و رهنمودها', 'intro' => 'پرسش‌ها و پاسخ‌های کاربران پس از بررسی اداره نشر می‌شود؛ پاسخ رسمی فوری نمایش داده می‌شود.', 'samples' => 'پرسش‌های نمونه', 'latest' => 'مسائل تازه', 'search' => 'جستجو...', 'ask' => 'مسئله بپرسید', 'ask_title' => 'پرسش خود را بنویسید', 'login_note' => 'برای پرسش و پاسخ وارد حساب شوید.', 'title_label' => 'عنوان', 'category' => 'کتگوری', 'body' => 'پرسش', 'submit' => 'ارسال برای بررسی', 'reply' => 'پاسخ بنویسید', 'reply_submit' => 'ارسال پاسخ', 'official' => 'پاسخ رسمی', 'no_questions' => 'هنوز پرسشی نشر نشده است.', 'statuses' => ['pending' => 'در حال بررسی', 'open' => 'باز', 'answered' => 'پاسخ‌شده', 'closed' => 'بسته']],
        'ar' => ['title' => 'مسائل شرعية وأسئلة وإرشادات', 'intro' => 'تنشر أسئلة المستخدمين وردودهم بعد مراجعة الإدارة؛ أما الجواب الرسمي فيظهر مباشرة.', 'samples' => 'أسئلة نموذجية', 'latest' => 'أحدث المسائل', 'search' => 'بحث...', 'ask' => 'اسأل مسألة', 'ask_title' => 'اكتب سؤالك', 'login_note' => 'سجل الدخول للسؤال والرد.', 'title_label' => 'العنوان', 'category' => 'القسم', 'body' => 'السؤال', 'submit' => 'إرسال للمراجعة', 'reply' => 'اكتب ردا', 'reply_submit' => 'إرسال الرد', 'official' => 'جواب رسمي', 'no_questions' => 'لم ينشر أي سؤال بعد.', 'statuses' => ['pending' => 'قيد المراجعة', 'open' => 'مفتوح', 'answered' => 'مجاب', 'closed' => 'مغلق']],
        'en' => ['title' => 'Sharia questions, answers, and guidance', 'intro' => 'User questions and community replies are published after admin review; official answers appear immediately.', 'samples' => 'Sample questions', 'latest' => 'Latest issues', 'search' => 'Search...', 'ask' => 'Ask a question', 'ask_title' => 'Write your question', 'login_note' => 'Login to ask or reply.', 'title_label' => 'Title', 'category' => 'Category', 'body' => 'Question', 'submit' => 'Submit for review', 'reply' => 'Write a reply', 'reply_submit' => 'Send reply', 'official' => 'Official answer', 'no_questions' => 'No questions have been posted yet.', 'statuses' => ['pending' => 'Under review', 'open' => 'Open', 'answered' => 'Answered', 'closed' => 'Closed']],
    ][$currentLocale ?? 'ps'];
@endphp

@section('title', 'دارالافتاء | ' . $ui['brand'])

@section('content')
    <section class="page-hero">
        <div class="container page-hero__grid">
            <div>
                <span class="section-kicker">دارالافتاء</span>
                <h1>{{ $iftaUi['title'] }}</h1>
                <p>{{ $iftaUi['intro'] }}</p>
            </div>
            <figure class="page-hero__media">
                <img src="{{ asset('images/madrasa/optimized/gathering.jpg') }}" alt="{{ $iftaUi['title'] }}">
            </figure>
        </div>
    </section>

    <section class="section-pad">
        <div class="container ifta-grid">
            <div class="fatwa-panel">
                <div class="section-heading section-heading--compact">
                    <div>
                        <span class="section-kicker">{{ $iftaUi['samples'] }}</span>
                        <h2>{{ $iftaUi['latest'] }}</h2>
                    </div>
                    <label class="search-box">
                        <span class="sr-only">{{ $iftaUi['search'] }}</span>
                        <input type="search" placeholder="{{ $iftaUi['search'] }}" data-filter-input data-filter-target=".question-list a">
                    </label>
                </div>

                <div class="question-list">
                    @foreach ($fatwas as $fatwa)
                        <a href="#" data-filter-text="{{ $fatwa['title'] }} {{ $fatwa['category'] }}">
                            <span>{{ $fatwa['category'] }}</span>
                            <strong>{{ $fatwa['title'] }}</strong>
                            <small>{{ $fatwa['date'] }}</small>
                        </a>
                    @endforeach
                </div>
            </div>

            @auth
                <form class="form-panel" method="post" action="{{ route('questions.store') }}">
                    @csrf
                    <span class="section-kicker">{{ $iftaUi['ask'] }}</span>
                    <h2>{{ $iftaUi['ask_title'] }}</h2>
                    <label>
                        <span>{{ $iftaUi['title_label'] }}</span>
                        <input name="title" value="{{ old('title') }}" required>
                        @error('title') <small>{{ $message }}</small> @enderror
                    </label>
                    <label>
                        <span>{{ $iftaUi['category'] }}</span>
                        <input name="category" value="{{ old('category', 'general') }}" required>
                        @error('category') <small>{{ $message }}</small> @enderror
                    </label>
                    <label>
                        <span>{{ $iftaUi['body'] }}</span>
                        <textarea name="body" rows="7" required>{{ old('body') }}</textarea>
                        @error('body') <small>{{ $message }}</small> @enderror
                    </label>
                    <button class="btn btn-primary" type="submit"><i data-lucide="send" aria-hidden="true"></i> {{ $iftaUi['submit'] }}</button>
                </form>
            @else
                <div class="form-panel">
                    <span class="section-kicker">{{ $iftaUi['ask'] }}</span>
                    <h2>{{ $iftaUi['login_note'] }}</h2>
                    <a class="btn btn-primary" href="{{ route('login') }}"><i data-lucide="log-in" aria-hidden="true"></i> {{ $ui['login'] }}</a>
                    <a class="auth-link" href="{{ route('register') }}">{{ $ui['register'] }}</a>
                </div>
            @endauth
        </div>
    </section>

    <section class="section-pad section-pad--light">
        <div class="container ifta-thread-list">
            @forelse ($questions as $question)
                <article class="ifta-thread">
                    <div class="ifta-thread__head">
                        <span>{{ $question->category }} · {{ $iftaUi['statuses'][$question->status] ?? $question->status }}</span>
                        <h2>{{ $question->title }}</h2>
                        <p>{{ $question->body }}</p>
                        <small>{{ $question->user->name }} · {{ $question->created_at->diffForHumans() }}</small>
                    </div>

                    <div class="reply-thread">
                        @foreach ($question->replies as $reply)
                            <div class="reply-item @if($reply->is_official) is-official @endif">
                                <strong>{{ $reply->user->name }} @if($reply->is_official) · {{ $iftaUi['official'] }} @endif</strong>
                                <p>{{ $reply->body }}</p>
                            </div>
                        @endforeach
                    </div>

                    @auth
                        <form class="reply-form" method="post" action="{{ route('questions.replies.store', $question) }}">
                            @csrf
                            <label>
                                <span>{{ $iftaUi['reply'] }}</span>
                                <textarea name="body" rows="3" required></textarea>
                            </label>
                            <button class="btn btn-ghost" type="submit">{{ $iftaUi['reply_submit'] }}</button>
                        </form>
                    @endauth
                </article>
            @empty
                <div class="ifta-thread">{{ $iftaUi['no_questions'] }}</div>
            @endforelse

            {{ $questions->links() }}
        </div>
    </section>
@endsection
