@layout('theme::layouts/admin')

@section('title')Forums@endsection
@section('page_title')Forums@endsection

@section('header_actions')
<a href="/admin/forums/create" class="btn btn-primary">New forum</a>
@endsection

@section('content')
{#
    The forum table lived here twice, written out in full, with thirty-two
    inline style attributes between the two copies. It is one partial now.
    Reasoning is in docs/design/2026-08-06-the-design-system.md.
#}

@notempty($viewSaved ?? null)
<div class="admin-alert admin-alert-success">Saved. Every category following the site changed with it.</div>
@endnotempty

{#
    🚨 "How forums look by default" moved to the theme on 2026-08-10.

    It is a `display_style` token now, edited in Appearance (or Admin →
    Themes), because how a listing is drawn is appearance and a member with
    their own theme should get their own default. Leaving the control here
    would have left a select that still looked authoritative and no longer
    decided anything — the exact bug `display_mode` caused in the other
    direction and cost a migration to undo.

    The precedence chain is unchanged: a reader's own toggle wins, then a
    category's explicit mode, then the theme.
#}

@php
    $loose = $grouped[0] ?? [];
    $nothingAtAll = empty($categories) && empty($loose);
@endphp

{# Nothing at all: the state a new install opens on. #}
@if($nothingAtAll)
<div class="panel">
    <div class="empty">
        <span class="empty-title">No forums yet</span>
        <p class="empty-note">
            A forum is where topics live. Create the first one and it will
            appear on the front page straight away — you can group forums into
            categories later.
        </p>
        <a href="/admin/forums/create" class="btn btn-primary">Create the first forum</a>
    </div>
</div>
@endif

@if(!$nothingAtAll)

@notempty($loose)
<div class="panel">
    <div class="panel-header">
        <span>Uncategorised</span>
        <span class="admin-cell-note">Shown above every category on the front page</span>
    </div>
    @include('forum::admin/forums/partials/table', ['rows' => $loose])
</div>
@endnotempty

@each ($categories as $category)
@php $rows = $grouped[$category['id']] ?? []; @endphp
<div class="panel">
    <div class="panel-header">
        <span>{{ $category['title'] }}</span>
        <div class="admin-row-actions">
            <form method="POST" action="/admin/forums/{{ $category['id'] }}/move-up" class="inline-form">
                @csrf
                <button type="submit" class="reorder-btn" title="Move category up"
                        aria-label="Move {{ $category['title'] }} up">&uarr;</button>
            </form>
            <form method="POST" action="/admin/forums/{{ $category['id'] }}/move-down" class="inline-form">
                @csrf
                <button type="submit" class="reorder-btn" title="Move category down"
                        aria-label="Move {{ $category['title'] }} down">&darr;</button>
            </form>
            <a href="/admin/forums/{{ $category['id'] }}/edit" class="btn btn-default btn-sm">Edit</a>
        </div>
    </div>

    @notempty($rows)
    @include('forum::admin/forums/partials/table', ['rows' => $rows])
    @endnotempty

    {#
        A category with no forums in it. Said with the way out of it, because
        an empty category is almost always one somebody made a moment ago and
        has not filled yet.
    #}
    @empty($rows)
    <div class="empty">
        <span class="empty-title">Nothing in {{ $category['title'] }} yet</span>
        <p class="empty-note">A category with no forums in it is hidden from the front page.</p>
        <a href="/admin/forums/create" class="btn btn-default btn-sm">Add a forum here</a>
    </div>
    @endempty
</div>
@endeach

@endif
@endsection
