@extends('layouts.portal')

@section('portal-content')
    <section class="section">
        <div class="section-heading">
            <span class="eyebrow">Админка</span>
            <h2>Новости</h2>
            <p>Новости отображаются в лаунчере. Картинка загружается в S3-хранилище.</p>
        </div>

        <div class="portal-grid">
            <article class="content-card">
                <h3>Статистика</h3>

                <dl class="stats-list stats-list--compact">
                    <div>
                        <dt>Всего</dt>
                        <dd>{{ $newsStats['total'] }}</dd>
                    </div>
                    <div>
                        <dt>Опубликовано</dt>
                        <dd>{{ $newsStats['published'] }}</dd>
                    </div>
                    <div>
                        <dt>Черновиков</dt>
                        <dd>{{ $newsStats['draft'] }}</dd>
                    </div>
                </dl>
            </article>
        </div>
    </section>

    <section class="section">
        <article class="content-card">
            <span class="eyebrow">Новая запись</span>
            <h2>Добавить новость</h2>

            <form class="stack-form" method="POST" action="{{ route('admin.news.store') }}" enctype="multipart/form-data">
                @csrf

                <label class="form-field">
                    <span>Заголовок</span>
                    <input class="form-control @error('title') is-invalid @enderror" type="text" name="title" value="{{ old('title') }}" maxlength="120" required>
                    @error('title')<span class="form-error">{{ $message }}</span>@enderror
                </label>

                <label class="form-field">
                    <span>Порядок вывода</span>
                    <input class="form-control @error('sort_order') is-invalid @enderror" type="number" name="sort_order" value="{{ old('sort_order', 0) }}" min="0" max="9999" required>
                    @error('sort_order')<span class="form-error">{{ $message }}</span>@enderror
                </label>

                <label class="checkbox-inline">
                    <input type="checkbox" name="is_published" value="1" @checked(old('is_published', true))>
                    <span>Сразу опубликовать</span>
                </label>

                <label class="form-field">
                    <span>Текст новости</span>
                    <textarea class="form-control form-control--textarea @error('body') is-invalid @enderror" name="body" required>{{ old('body') }}</textarea>
                    @error('body')<span class="form-error">{{ $message }}</span>@enderror
                </label>

                <label class="form-field">
                    <span>Картинка</span>
                    <input class="form-control @error('image') is-invalid @enderror" type="file" name="image" accept="image/*">
                    <small>Максимум 2 МБ. Необязательно.</small>
                    @error('image')<span class="form-error">{{ $message }}</span>@enderror
                </label>

                <button class="button button--gold" type="submit">Добавить новость</button>
            </form>
        </article>
    </section>

    <section class="section">
        <div class="section-heading">
            <span class="eyebrow">Текущие записи</span>
            <h2>Редактирование</h2>
        </div>

        <div class="account-list">
            @forelse ($newsItems as $newsItem)
                <article class="content-card news-card">
                    <div class="account-card__header">
                        <div>
                            <h3>{{ $newsItem->title }}</h3>
                            <p>
                                Порядок: {{ $newsItem->sort_order }}
                                · {{ $newsItem->created_by_username ?? '—' }}
                                @if ($newsItem->updated_at)
                                    · {{ $newsItem->updated_at->format('d.m.Y H:i') }}
                                @endif
                            </p>
                        </div>

                        <span class="tag">{{ $newsItem->is_published ? 'Опубликовано' : 'Черновик' }}</span>
                    </div>

                    @if ($newsItem->image_url)
                        <img src="{{ $newsItem->image_url }}" alt="{{ $newsItem->title }}" style="max-width: 320px; border-radius: 6px; margin-bottom: 1rem;">
                    @endif

                    <form class="stack-form" method="POST" action="{{ route('admin.news.update', $newsItem) }}" enctype="multipart/form-data">
                        @csrf
                        @method('PATCH')

                        <label class="form-field">
                            <span>Заголовок</span>
                            <input class="form-control" type="text" name="title" value="{{ $newsItem->title }}" maxlength="120" required>
                        </label>

                        <label class="form-field">
                            <span>Порядок вывода</span>
                            <input class="form-control" type="number" name="sort_order" value="{{ $newsItem->sort_order }}" min="0" max="9999" required>
                        </label>

                        <label class="checkbox-inline">
                            <input type="checkbox" name="is_published" value="1" @checked($newsItem->is_published)>
                            <span>Опубликовано</span>
                        </label>

                        <label class="form-field">
                            <span>Текст новости</span>
                            <textarea class="form-control form-control--textarea" name="body" required>{{ $newsItem->body }}</textarea>
                        </label>

                        <label class="form-field">
                            <span>Заменить картинку</span>
                            <input class="form-control" type="file" name="image" accept="image/*">
                            <small>Оставь пустым, чтобы сохранить текущую.</small>
                        </label>

                        <div class="form-actions">
                            <button class="button button--gold" type="submit">Сохранить</button>
                        </div>
                    </form>

                    <form method="POST" action="{{ route('admin.news.destroy', $newsItem) }}">
                        @csrf
                        @method('DELETE')

                        <button class="button button--ghost" type="submit" onclick="return confirm('Удалить эту новость?')">Удалить</button>
                    </form>
                </article>
            @empty
                <article class="content-card empty-state">
                    <h3>Новостей пока нет</h3>
                    <p>Добавь первую новость — она сразу появится в лаунчере.</p>
                </article>
            @endforelse
        </div>
    </section>
@endsection
