@extends('theme::layout')

@php
    $rendered = $article->rendered();
    $bodyHtml = $rendered['html'];
    $toc = $rendered['toc'];
@endphp

@section('content')
    @themeslot('article_top')
    <article class="post" itemscope itemtype="https://schema.org/BlogPosting">
        <header class="post__header">
            <h1 class="post__title" itemprop="headline">{{ $article->title }}</h1>
            <p class="post__meta">
                @if($article->published_at)
                    <time datetime="{{ $article->published_at->toIso8601String() }}" itemprop="datePublished">
                        {{ $article->published_at->format('Y-m-d H:i') }}
                    </time>
                @endif
                @if($article->category)
                    <span aria-hidden="true"> · </span>
                    <a class="post__category" href="{{ url('/category-'.$article->category->id.'.shtml') }}" rel="category tag">
                        {{ $article->category->name }}
                    </a>
                @endif
                @if($article->user)
                    <span aria-hidden="true"> · </span>
                    <span itemprop="author" itemscope itemtype="https://schema.org/Person">
                        <span itemprop="name">{{ $article->user->name ?: $article->user->username }}</span>
                    </span>
                @endif
                <span aria-hidden="true"> · </span>
                <span>{{ $article->views }} {{ __('frontend.common.reads') }}</span>
            </p>
        </header>

        @if($article->hasCover() && ($coverUrl = $article->coverUrl()))
            <figure class="post__cover">
                <img src="{{ $coverUrl }}" alt="{{ $article->title }}" itemprop="image">
            </figure>
        @endif

        @if(count($toc) >= 2)
            <nav class="post__toc" aria-labelledby="post-toc-heading">
                <h2 id="post-toc-heading" class="post__toc-title">{{ __('frontend.article.toc') }}</h2>
                <ol class="post__toc-list">
                    @foreach($toc as $item)
                        <li class="post__toc-item post__toc-item--h{{ $item['level'] }}">
                            <a href="#{{ $item['id'] }}">{{ $item['text'] }}</a>
                        </li>
                    @endforeach
                </ol>
            </nav>
        @endif

        <div class="post__content entry-content" itemprop="articleBody">
            {!! $bodyHtml !!}
        </div>

        @if($article->tags->isNotEmpty())
            <footer class="post__footer">
                <ul class="post__tags">
                    @foreach($article->tags as $tag)
                        <li>
                            <a class="chip" rel="tag" href="{{ url('/tag/'.rawurlencode($tag->name)) }}">{{ $tag->name }}</a>
                        </li>
                    @endforeach
                </ul>
            </footer>
        @endif
    </article>
    @themeslot('article_bottom')

    <section class="comments panel" aria-labelledby="comments-heading">
        <h2 id="comments-heading" class="comments__title">{{ __('frontend.common.comments') }}</h2>
        @forelse($article->comments as $comment)
            <article class="comment">
                <header class="comment__meta">
                    @include('theme::partials.comment-author')
                    @if($comment->published_at)
                        <time datetime="{{ $comment->published_at->toIso8601String() }}">
                            {{ $comment->published_at->format('Y-m-d H:i') }}
                        </time>
                    @endif
                </header>
                <div class="comment__body">{!! $comment->content !!}</div>
            </article>
        @empty
            <p class="meta">{{ __('frontend.common.no_comments') }}</p>
        @endforelse

        @if(!$article->close_comment)
            <form class="stack comments__form" method="post" action="{{ url('/post.php') }}">
                @csrf
                <input type="hidden" name="action" value="addcomment">
                <input type="hidden" name="article_id" value="{{ $article->id }}">
                <label for="comment-author">{{ __('frontend.common.nickname') }}</label>
                <input id="comment-author" name="author" required maxlength="50" value="{{ old('author', auth()->user()?->username) }}">
                <label for="comment-url">{{ __('frontend.common.website') }}</label>
                <input id="comment-url" name="url" type="url" value="{{ old('url', auth()->user()?->url) }}">
                <label for="comment-content">{{ __('frontend.common.content') }}</label>
                <textarea id="comment-content" name="content" rows="5" required>{{ old('content') }}</textarea>
                <button type="submit">{{ __('frontend.common.submit_comment') }}</button>
            </form>
        @endif
    </section>
@endsection
