<%- include('../partials/head',{pageTitle,bodyClass:'admin-shell'}) %><%- include('nav') %>
<main class="admin-main">
  <h1>Categories</h1>
  <div class="dual-admin">
    <section class="admin-panel">
      <h2><%= editItem?'Edit category':'Add category' %></h2>
      <form method="post" action="<%= editItem?'/admin/categories/'+editItem.id+'?_method=PUT':'/admin/categories' %>" class="stack-form">
        <input type="hidden" name="_csrf" value="<%= csrfToken %>">
        <label><span>Name</span><input name="name" value="<%= editItem?.name||'' %>" required></label>
        <label><span>URL slug</span><input name="slug" value="<%= editItem?.slug||'' %>" placeholder="auto from name if blank"></label>
        <label><span>Badge</span><input name="badge" value="<%= editItem?.badge||'' %>" placeholder="optional label"></label>
        <label><span>Menu group</span><input name="menu_group" value="<%= editItem?.menu_group||'' %>" placeholder="e.g. SOLAR PV"></label>
        <label><span>Sort order</span><input type="number" name="sort_order" value="<%= editItem?.sort_order||0 %>"></label>
        <label class="full-row" data-media-field>
          <span>Category image</span>
          <input name="image_url" value="<%= editItem?.image_url||'' %>" data-media placeholder="Choose from the library">
        </label>
        <label><span>Description</span><textarea name="description" rows="5"><%= editItem?.description||'' %></textarea></label>
        <button class="btn btn-primary"><%= editItem?'Update category':'Create category' %></button>
      </form>
    </section>
    <section class="admin-panel">
      <h2>All categories</h2>
      <table>
        <tr><th>Name</th><th>Group</th><th>Badge</th><th></th></tr>
        <% items.forEach(item=>{ %>
          <tr>
            <td><%= item.name %></td>
            <td><%= item.menu_group %></td>
            <td><%= item.badge %></td>
            <td class="row-actions">
              <a href="/admin/categories?edit=<%= item.id %>">Edit</a>
              <form method="post" action="/admin/categories/<%= item.id %>/duplicate"><input type="hidden" name="_csrf" value="<%= csrfToken %>"><button>Copy</button></form>
              <form method="post" action="/admin/categories/<%= item.id %>/delete"><input type="hidden" name="_csrf" value="<%= csrfToken %>"><button>Delete</button></form>
            </td>
          </tr>
        <% }) %>
      </table>
    </section>
  </div>
</main>
</body></html>
