{% extends "base.html" %}
{% block title %}{{ page.title }} — Wiki{% endblock %}
{% block content %}
{% include "wiki/_layout.html" %}

<div class="wiki-toolbar">
    <h1 class="m-0"><i class="bi bi-file-text"></i> {{ page.title }}</h1>
    <div class="ms-auto d-flex gap-2">
        <a href="{{ url_for('wiki.history', slug=page.slug) }}"
           class="btn btn-sm btn-outline-secondary">
           <i class="bi bi-clock-history"></i> History</a>
        {% if current_user.is_authenticated %}
            {% if not page.is_locked or current_user.is_admin %}
            <a href="{{ url_for('wiki.edit', slug=page.slug) }}"
               class="btn btn-sm btn-primary">
               <i class="bi bi-pencil"></i> Edit</a>
            {% else %}
            <button class="btn btn-sm btn-outline-secondary" disabled
                    title="This page is locked. Only sysops can edit it.">
                <i class="bi bi-lock"></i> Locked</button>
            {% endif %}
        {% endif %}
        {% if current_user.is_authenticated and current_user.is_admin %}
        <div class="dropdown d-inline">
            <button class="btn btn-sm btn-outline-secondary dropdown-toggle"
                    data-bs-toggle="dropdown">
                <i class="bi bi-gear"></i> Admin
            </button>
            <ul class="dropdown-menu dropdown-menu-end" style="background-color:var(--theme-bg-dark);border:1px solid var(--theme-primary);">
                <li>
                    <form method="post" action="{{ url_for('wiki.toggle_lock', slug=page.slug) }}" class="px-2 py-1">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        <button class="btn btn-sm btn-outline-warning w-100">
                            <i class="bi bi-{{ 'unlock' if page.is_locked else 'lock' }}"></i>
                            {{ 'Unlock' if page.is_locked else 'Lock' }}
                        </button>
                    </form>
                </li>
                <li>
                    <form method="post" action="{{ url_for('wiki.delete', slug=page.slug) }}" class="px-2 py-1"
                          onsubmit="return confirm('Soft-delete this page? It can be restored.');">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        <button class="btn btn-sm btn-outline-danger w-100">
                            <i class="bi bi-trash"></i> Delete
                        </button>
                    </form>
                </li>
                <li>
                    <form method="post" action="{{ url_for('wiki.rename', slug=page.slug) }}" class="px-2 py-1"
                          onsubmit="return confirm('Rename this page?');">
                        <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
                        <input type="text" name="new_slug" placeholder="new-slug" class="form-control form-control-sm mb-1">
                        <button class="btn btn-sm btn-outline-info w-100">
                            <i class="bi bi-arrow-left-right"></i> Rename
                        </button>
                    </form>
                </li>
            </ul>
        </div>
        {% endif %}
    </div>
</div>

<div class="wiki-meta">
    <i class="bi bi-clock"></i>
    Last edited {{ page.updated_at.strftime('%Y-%m-%d %H:%M UTC') }}
    {% if page.updated_by %}by <a href="{{ url_for('profile.view_user', username=page.updated_by.username) }}">{{ page.updated_by.username }}</a>{% endif %}
    · <a href="{{ url_for('wiki.history', slug=page.slug) }}">history</a>
    · {{ page.view_count or 0 }} view{{ '' if (page.view_count or 0) == 1 else 's' }}
    {% if page.is_locked %}· <span class="badge bg-warning text-dark"><i class="bi bi-lock"></i> Locked</span>{% endif %}
</div>

<article class="wiki-body">
    {{ body_html }}
</article>
{% endblock %}
