{# templates/adminpanel/category_form.html #}
{% extends "adminpanel/base.html" %}
{% load widget_tweaks %}

{% block pagetitle %}{% if mode == "create" %}New category{% else %}Edit category{% endif %}{% endblock %}
{% block page_header %}{% if mode == "create" %}New category{% else %}Edit category{% endif %}{% endblock %}
{% block header_actions %}
  <div class="d-flex align-items-center gap-2">
    <a class="btn btn-light btn-sm" href="{% url 'admin_categories' %}">Back</a>
    <button class="btn btn-primary btn-sm" type="submit" form="catForm">Save</button>
  </div>
{% endblock %}

{% block content %}
<form id="catForm" method="post" class="mm-soft-card p-3">
  {% csrf_token %}

  {% if form.non_field_errors %}
    <div class="alert alert-danger">
      {% for e in form.non_field_errors %}{{ e }}<br>{% endfor %}
    </div>
  {% endif %}

  <div class="row g-3">
    <div class="col-md-6">
      <label class="form-label">Name</label>
      {% if form.name.errors %}
        {% render_field form.name|add_class:"form-control is-invalid" %}
        {% for e in form.name.errors %}<div class="invalid-feedback d-block">{{ e }}</div>{% endfor %}
      {% else %}
        {% render_field form.name|add_class:"form-control" %}
      {% endif %}
    </div>

    <div class="col-md-6">
      <label class="form-label">Parent</label>
      {% if form.parent.errors %}
        {% render_field form.parent|add_class:"form-select is-invalid" %}
        {% for e in form.parent.errors %}<div class="invalid-feedback d-block">{{ e }}</div>{% endfor %}
      {% else %}
        {% render_field form.parent|add_class:"form-select" %}
      {% endif %}
    </div>

    <div class="col-12">
      <label class="form-label">Description</label>
      {% if form.description.errors %}
        {% render_field form.description|add_class:"form-control is-invalid" rows="4" %}
        {% for e in form.description.errors %}<div class="invalid-feedback d-block">{{ e }}</div>{% endfor %}
      {% else %}
        {% render_field form.description|add_class:"form-control" rows="4" %}
      {% endif %}
      <div class="form-text">Short description for this category (optional).</div>
    </div>
  </div>

  <div class="d-flex justify-content-between align-items-center mt-3">
    <small class="text-muted">Slug is generated automatically from the name.</small>
    <div>
      <a class="btn btn-light" href="{% url 'admin_categories' %}">Cancel</a>
      <button class="btn btn-primary" type="submit">Save</button>
    </div>
  </div>
</form>
{% endblock %}
