<h1>Knowledge Base</h1>

<% if (forbidden) { %>
  <div class="alert alert-danger">Your role does not have permission to view the knowledge base.</div>
<% } else { %>
<% if (error) { %><div class="alert alert-danger"><%= error %></div><% } %>

<div class="card">
  <table>
    <thead><tr><th>Title</th><th>Category</th><th>Status</th><th></th></tr></thead>
    <tbody>
      <% for (const a of articles) { %>
      <tr>
        <td><%= a.title %> <span style="color:var(--muted);font-size:11px">/<%= a.slug %></span></td>
        <td><%= a.category || '—' %></td>
        <td><span class="badge <%= a.is_published ? 'badge-ok' : 'badge-warn' %>"><%= a.is_published ? 'Published' : 'Draft' %></span></td>
        <td>
          <form method="post" action="/support/kb/<%= a.id %>">
            <input type="hidden" name="isPublished" value="<%= a.is_published ? 'false' : 'true' %>">
            <button class="link-btn" type="submit"><%= a.is_published ? 'Unpublish' : 'Publish' %></button>
          </form>
        </td>
      </tr>
      <% } %>
      <% if (!articles.length) { %><tr><td colspan="4" style="color:var(--muted)">No articles yet.</td></tr><% } %>
    </tbody>
  </table>
</div>

<div class="card" style="max-width:560px">
  <h2 style="font-size:15px;margin-top:0">New Article</h2>
  <form method="post" action="/support/kb">
    <label>Slug (unique, URL-safe)</label>
    <input type="text" name="slug" required>
    <label>Title</label>
    <input type="text" name="title" required>
    <label>Category</label>
    <input type="text" name="category">
    <label>Body</label>
    <textarea name="body" rows="6" required></textarea>
    <label><input type="checkbox" name="isPublished" value="true"> Publish immediately</label>
    <button class="btn" type="submit" style="width:100%;margin-top:16px">Create Article</button>
  </form>
</div>

<% } %>
