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

{% block title %}{% trans "Create Standard" %}{% 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">{% trans "Create New Standard" %}</h1>
            <p class="text-muted mb-0">{% trans "Add a new compliance standard" %}</p>
        </div>
        <div>
            {% if department_id %}
            <a href="{% url 'standards:department_standards' pk=department_id %}" class="btn btn-outline-secondary">
                {% action_icon "back" %} {% trans "Back to Department Standards" %}
            </a>
            {% else %}
            <a href="{% url 'standards:dashboard' %}" class="btn btn-outline-secondary">
                {% action_icon "back" %} {% trans "Back to Dashboard" %}
            </a>
            {% endif %}
        </div>
    </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 "Standard Information" %}</h5>
                </div>
                <div class="card-body">
                    <form method="post">
                        {% csrf_token %}

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

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

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

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

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

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


                        <div class="mb-3">
                            <label for="{{ form.department.id_for_label }}" class="form-label">
                                {{ form.department.label }}
                            </label>
                            {{ form.department }}
                            <small class="form-text text-muted">
                                {% trans "Leave empty to apply to all departments" %}
                            </small>
                            {% if form.department.errors %}
                            <div class="text-danger">{{ form.department.errors }}</div>
                            {% endif %}
                        </div>

                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.effective_date.id_for_label }}" class="form-label">
                                    {{ form.effective_date.label }}
                                </label>
                                {{ form.effective_date }}
                                {% if form.effective_date.errors %}
                                <div class="text-danger">{{ form.effective_date.errors }}</div>
                                {% endif %}
                            </div>

                            <div class="col-md-6 mb-3">
                                <label for="{{ form.review_date.id_for_label }}" class="form-label">
                                    {{ form.review_date.label }}
                                </label>
                                {{ form.review_date }}
                                {% if form.review_date.errors %}
                                <div class="text-danger">{{ form.review_date.errors }}</div>
                                {% endif %}
                            </div>
                        </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" %} {% trans "Create Standard" %}
                            </button>
                            {% if department_id %}
                            <a href="{% url 'standards:department_standards' pk=department_id %}" class="btn btn-secondary">
                                {% trans "Cancel" %}
                            </a>
                            {% else %}
                            <a href="{% url 'standards:dashboard' %}" class="btn btn-secondary">
                                {% trans "Cancel" %}
                            </a>
                            {% endif %}
                        </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 "Standard Code Format" %}</h6>
                    <p class="small text-muted">
                        {% trans "Use a unique code to identify this standard" %}<br>
                        <strong>{% trans "Examples:" %}</strong><br>
                        - STD-001<br>
                        - QM-05<br>
                        - PS-12
                    </p>

                    <h6 class="mt-3">{% trans "Department Assignment" %}</h6>
                    <p class="small text-muted">
                        {% trans "Leave the department field empty if this standard applies to all departments. Select a specific department only if the standard is department-specific." %}
                    </p>

                    <h6 class="mt-3">{% trans "Dates" %}</h6>
                    <p class="small text-muted">
                        {% trans "Effective date: When the standard becomes mandatory" %}<br>
                        {% trans "Review date: When the standard should be reviewed for updates" %}
                    </p>
                </div>
            </div>
        </div>
    </div>
</div>

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

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