{% extends "base.html" %}

{% block title %}BBS Menus{% endblock %}

{% block content %}
<div class="bbs-header">
  <h1><i class="bi bi-list-nested"></i> BBS Menus</h1>
</div>

<p class="text-muted">
  Data-driven menus shown to users on telnet/SSH/rlogin. The menu named
  <code>main</code> is the entry point after login. If no menus exist the
  hard-coded fallback is used.
</p>

<div class="mb-3">
  <a href="{{ url_for('menu_admin.new_menu') }}" class="btn btn-primary"><i class="bi bi-plus"></i> New menu</a>
  <a href="{{ url_for('menu_admin.list_screens') }}" class="btn btn-outline-primary"><i class="bi bi-tv"></i> ANSI screens (welcome / goodbye)</a>
  {% if menus|length > 0 %}
    <form method="post" action="{{ url_for('menu_admin.seed_samples') }}" class="d-inline">
      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
      <button class="btn btn-outline-secondary"><i class="bi bi-magic"></i> Add sample items (weather, ansi demo)</button>
    </form>
  {% endif %}
  {% if menus|length == 0 %}
    <form method="post" action="{{ url_for('menu_admin.seed_defaults') }}" class="d-inline">
      <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
      <button class="btn btn-outline-secondary"><i class="bi bi-magic"></i> Seed default 'main' menu</button>
    </form>
  {% endif %}
</div>

{% if roots %}
<div class="card mb-3">
  <div class="card-header"><i class="bi bi-diagram-3"></i> Menu tree (entry points → submenus)</div>
  <ul class="list-group list-group-flush" id="menu-tree">
  {% macro render_node(m, depth=0, visited=[]) %}
    {% if m.name not in visited %}
      {% set _ = visited.append(m.name) %}
      <li class="list-group-item" style="padding-left: {{ 1 + depth * 1.5 }}rem;">
        {% if depth > 0 %}<span class="text-muted">└─ </span>{% endif %}
        <a href="{{ url_for('menu_admin.edit_menu', menu_id=m.id) }}">
          <strong><code>{{ m.name }}</code></strong>
        </a>
        — {{ m.title }}
        {% if m.is_default %}<span class="badge bg-success ms-1">default entry</span>{% endif %}
        {% if m.min_access > 0 %}<span class="badge bg-warning ms-1">access ≥ {{ m.min_access }}</span>{% endif %}
        <small class="text-muted ms-2">({{ m.items.count() }} items)</small>
      </li>
      {% for c in children.get(m.name, []) %}
        {% set child_menu = name_to_menu.get(c.name) %}
        {% if child_menu %}
          {{ render_node(child_menu, depth + 1, visited) }}
        {% endif %}
      {% endfor %}
    {% endif %}
  {% endmacro %}
  {% for r in roots %}
    {{ render_node(r, 0, []) }}
  {% endfor %}
  </ul>
</div>
{% endif %}

<h4 class="mt-3">All menus</h4>
<table class="table table-dark table-hover">
  <thead><tr><th>Name</th><th>Title</th><th>Items</th><th>Default?</th><th>Min access</th><th></th></tr></thead>
  <tbody>
    {% for m in menus %}
    <tr>
      <td><code>{{ m.name }}</code></td>
      <td>{{ m.title }}</td>
      <td>{{ m.items.count() }}</td>
      <td>{% if m.is_default %}✓{% endif %}</td>
      <td>{{ m.min_access }}</td>
      <td>
        <a href="{{ url_for('menu_admin.edit_menu', menu_id=m.id) }}" class="btn btn-sm btn-primary"><i class="bi bi-pencil"></i></a>
        <form method="post" action="{{ url_for('menu_admin.delete_menu', menu_id=m.id) }}" class="d-inline" onsubmit="return confirm('Delete menu {{ m.name }}?')">
          <input type="hidden" name="csrf_token" value="{{ csrf_token() }}">
          <button class="btn btn-sm btn-danger"><i class="bi bi-trash"></i></button>
        </form>
      </td>
    </tr>
    {% else %}
    <tr><td colspan="6" class="text-muted">No menus yet — click "Seed default 'main' menu" or "New menu".</td></tr>
    {% endfor %}
  </tbody>
</table>
{% endblock %}
