{% extends 'layouts/base.html' %}
{% load i18n %}
{% load action_icons %}

{% block title %}{% if category %}{% trans "Update Category" %}{% else %}{% trans "Create Category" %}{% endif %}{% endblock %}

{% block content %}
<div class="container-fluid px-4">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h3 mb-0">{% if category %}{% trans "Update Category" %}{% else %}{% trans "Create Category" %}{% endif %}</h1>
            <p class="text-muted mb-0">{% if category %}{% trans "Edit standard category" %}{% else %}{% trans "Add new standard category" %}{% endif %}</p>
        </div>
        <a href="{% url 'standards:category_list' %}" class="btn btn-outline-secondary">
            {% action_icon "back" %} {% trans "Back to Categories" %}
        </a>
    </div>

    <div class="row">
        <div class="col-12 col-lg-8">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{% trans "Category Information" %}</h5>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}

                        <div class="mb-3">
                            <label for="{{ form.name.id_for_label }}" class="form-label">
                                {{ form.name.label }} <span class="text-danger">*</span>
                            </label>
                            {{ form.name }}
                            {% if form.name.errors %}
                            <div class="text-danger">{{ form.name.errors }}</div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.name_ar.id_for_label }}" class="form-label">
                                {{ form.name_ar.label }}
                            </label>
                            {{ form.name_ar }}
                            {% if form.name_ar.errors %}
                            <div class="text-danger">{{ form.name_ar.errors }}</div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.order.id_for_label }}" class="form-label">
                                {{ form.order.label }} <span class="text-danger">*</span>
                            </label>
                            {{ form.order }}
                            <small class="form-text text-muted">
                                {% trans "Lower numbers appear first in lists" %}
                            </small>
                            {% if form.order.errors %}
                            <div class="text-danger">{{ form.order.errors }}</div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.description.id_for_label }}" class="form-label">
                                {{ form.description.label }}
                            </label>
                            {{ form.description }}
                            {% if form.description.errors %}
                            <div class="text-danger">{{ form.description.errors }}</div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <div class="form-check">
                                {{ form.is_active }}
                                <label for="{{ form.is_active.id_for_label }}" class="form-check-label">
                                    {{ form.is_active.label }}
                                </label>
                            </div>
                        </div>

                        <div class="d-flex gap-2">
                            <button type="submit" class="btn btn-primary">
                                {% action_icon "save" %} {% if category %}{% trans "Update Category" %}{% else %}{% trans "Create Category" %}{% endif %}
                            </button>
                            <a href="{% url 'standards:category_list' %}" class="btn btn-secondary">
                                {% trans "Cancel" %}
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="col-12 col-lg-4">
            <div class="card">
                <div class="card-header">
                    <h5 class="card-title mb-0">{% trans "Help" %}</h5>
                </div>
                <div class="card-body">
                    <h6>{% trans "Category Order" %}</h6>
                    <p class="small text-muted">
                        {% trans "Use the order field to control how categories appear in lists and dropdowns. Lower numbers appear first." %}<br><br>
                        <strong>{% trans "Example:" %}</strong><br>
                        1 - Patient Safety<br>
                        2 - Quality Management<br>
                        3 - Infection Control
                    </p>

                    <h6 class="mt-3">{% trans "Active Status" %}</h6>
                    <p class="small text-muted">
                        {% trans "Only active categories can be used when creating new standards. Inactive categories remain in the system but are not available for selection." %}
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    // Initialize form fields with Bootstrap classes
    var formInputs = document.querySelectorAll('input[type="text"], input[type="number"], select, textarea');
    for (var i = 0; i < formInputs.length; i++) {
        if (!formInputs[i].classList.contains('form-control')) {
            formInputs[i].classList.add('form-control');
        }
    }

    // Initialize checkboxes with Bootstrap classes
    var checkboxes = document.querySelectorAll('input[type="checkbox"]');
    for (var i = 0; i < checkboxes.length; i++) {
        if (!checkboxes[i].classList.contains('form-check-input')) {
            checkboxes[i].classList.add('form-check-input');
        }
    }
});
</script>
{% endblock %}
