{% extends "base.html" %}

{% block content %}
  <section class="section">
    <div class="container">
      <h1 class="title is-4">Business Plan Inbox</h1>
      <p class="subtitle is-6">Review, filter, and open generated business plan documents.</p>

      <article class="notification is-light">
        <div class="tags mb-1">
          <span class="tag is-light">Total {{ stats.total|default:0 }}</span>
          <span class="tag is-warning is-light">Draft {{ stats.draft|default:0 }}</span>
          <span class="tag is-success is-light">Final {{ stats.final|default:0 }}</span>
        </div>
      </article>

      <article class="box">
        <h2 class="title is-6">Filters</h2>
        <form method="get">
          <div class="columns is-multiline">
            <div class="column is-4">
              <label class="label is-size-7">Search</label>
              <input class="input is-small" name="q" value="{{ filters.q }}" placeholder="Title, source channel, command profile">
            </div>
            <div class="column is-3">
              <label class="label is-size-7">Status</label>
              <div class="select is-small is-fullwidth">
                <select name="status">
                  <option value="">All</option>
                  <option value="draft" {% if filters.status == "draft" %}selected{% endif %}>Draft</option>
                  <option value="final" {% if filters.status == "final" %}selected{% endif %}>Final</option>
                </select>
              </div>
            </div>
            <div class="column is-3">
              <label class="label is-size-7">Service</label>
              <div class="select is-small is-fullwidth">
                <select name="service">
                  <option value="">All</option>
                  {% for service in service_choices %}
                    <option value="{{ service }}" {% if filters.service == service %}selected{% endif %}>{{ service }}</option>
                  {% endfor %}
                </select>
              </div>
            </div>
            <div class="column is-2 is-flex is-align-items-flex-end">
              <button class="button is-small is-link is-light" type="submit">Apply</button>
            </div>
          </div>
        </form>
      </article>

      <article class="box">
        <div class="is-flex is-justify-content-space-between is-align-items-center mb-2">
          <h2 class="title is-6 mb-0">Documents</h2>
          <a class="button is-small is-light" href="{% url 'command_routing' %}">Command Routing</a>
        </div>
        <table class="table is-fullwidth is-striped is-size-7">
          <thead>
            <tr>
              <th>Title</th>
              <th>Status</th>
              <th>Service</th>
              <th>Channel</th>
              <th>Profile</th>
              <th>Revisions</th>
              <th>Updated</th>
              <th></th>
            </tr>
          </thead>
          <tbody>
            {% for doc in documents %}
              <tr>
                <td>{{ doc.title }}</td>
                <td>
                  {% if doc.status == "final" %}
                    <span class="tag is-success is-light">final</span>
                  {% else %}
                    <span class="tag is-warning is-light">draft</span>
                  {% endif %}
                </td>
                <td>{{ doc.source_service }}</td>
                <td><code>{{ doc.source_channel_identifier|default:"-" }}</code></td>
                <td>{% if doc.command_profile %}{{ doc.command_profile.name }}{% else %}-{% endif %}</td>
                <td>{{ doc.revision_count }}</td>
                <td>{{ doc.updated_at }}</td>
                <td>
                  <a class="button is-small is-link is-light" href="{% url 'business_plan_editor' doc_id=doc.id %}">Open</a>
                </td>
              </tr>
            {% empty %}
              <tr><td colspan="8">No business plan documents yet.</td></tr>
            {% endfor %}
          </tbody>
        </table>
      </article>
    </div>
  </section>
{% endblock %}
