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

{% block title %}{% if source %}{% trans "Update Source" %}{% else %}{% trans "Create Source" %}{% 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 source %}{% trans "Update Source" %}{% else %}{% trans "Create Source" %}{% endif %}</h1>
            <p class="text-muted mb-0">{% if source %}{% trans "Edit standard source" %}{% else %}{% trans "Add new standard source" %}{% endif %}</p>
        </div>
        <a href="{% url 'standards:source_list' %}" class="btn btn-outline-secondary">
            {% action_icon "back" %} {% trans "Back to Sources" %}
        </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 "Source 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.code.id_for_label }}" class="form-label">
                                {{ form.code.label }} <span class="text-danger">*</span>
                            </label>
                            {{ form.code }}
                            <small class="form-text text-muted">
                                {% trans "Unique code for the source (e.g., CBAHI, JCI, ISO)" %}
                            </small>
                            {% if form.code.errors %}
                            <div class="text-danger">{{ form.code.errors }}</div>
                            {% endif %}
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.website.id_for_label }}" class="form-label">
                                {{ form.website.label }}
                            </label>
                            {{ form.website }}
                            <small class="form-text text-muted">
                                {% trans "Official website URL (optional)" %}
                            </small>
                            {% if form.website.errors %}
                            <div class="text-danger">{{ form.website.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 source %}{% trans "Update Source" %}{% else %}{% trans "Create Source" %}{% endif %}
                            </button>
                            <a href="{% url 'standards:source_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 "Source Code" %}</h6>
                    <p class="small text-muted">
                        {% trans "Use a unique code to identify the standard source organization." %}<br><br>
                        <strong>{% trans "Examples:" %}</strong><br>
                        - CBAHI (Central Board for Accreditation of Healthcare Institutions)<br>
                        - JCI (Joint Commission International)<br>
                        - ISO (International Organization for Standardization)
                    </p>

                    <h6 class="mt-3">{% trans "Active Status" %}</h6>
                    <p class="small text-muted">
                        {% trans "Only active sources can be used when creating new standards. Inactive sources 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
    const formInputs = document.querySelectorAll('input[type="text"], input[type="url"], 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 %}
