{% extends "base.html" %}
{% set active = "documents" %}
{% block title %}Cortex — Documents{% endblock %}

{% block content %}
<h2 class="page-title">Documents</h2>

<div class="card card-spaced">
    <form method="GET" action="/documents" class="form-row">
        <div>
            <label for="q">Search</label>
            <input type="text" id="q" name="q" value="{{ query }}" placeholder="Search knowledge...">
        </div>
        <div>
            <label for="doc_type">Type</label>
            <select id="doc_type" name="doc_type">
                <option value="">All</option>
                {% for t in ['decision','lesson','fix','session','research','source','synthesis','idea'] %}
                <option value="{{ t }}" {{ 'selected' if doc_type == t }}>{{ t|capitalize }}</option>
                {% endfor %}
            </select>
        </div>
        <div>
            <label for="project">Project</label>
            <select id="project" name="project">
                <option value="">All</option>
                {% for p in project_names %}
                <option value="{{ p }}" {{ 'selected' if project == p }}>{{ p }}</option>
                {% endfor %}
            </select>
        </div>
        <button type="submit" class="btn btn-primary">Search</button>
    </form>
</div>

<div class="card">
    <div class="card-header">{{ documents|length }} result(s)</div>
    <table>
        <thead><tr><th>Type</th><th>Title</th><th>Project</th><th>Tags</th></tr></thead>
        <tbody>
        {% for doc in documents %}
        <tr>
            <td><span class="badge badge-{{ doc.type }}">{{ doc.type }}</span></td>
            <td><a href="/documents/{{ doc.id }}">{{ doc.title }}</a></td>
            <td>{{ doc.project }}</td>
            <td>{{ doc.tags }}</td>
        </tr>
        {% else %}
        <tr><td colspan="4" class="table-empty">No documents found.</td></tr>
        {% endfor %}
        </tbody>
    </table>
</div>
{% endblock %}
