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

{% block title %}{% trans "Create Standard" %}{% endblock %}

{% block extra_css %}
<style>
.page-header-gradient {
    background: linear-gradient(135deg, #005696 0%, #0069a8 50%, #007bbd 100%);
    color: white;
    padding: 1.5rem 2rem;
    border-radius: 1rem;
    margin-bottom: 1.5rem;
    box-shadow: 0 10px 15px -3px rgba(0, 86, 150, 0.2);
}

.form-section {
    background: #fff;
    border: 1px solid #e2e8f0;
    border-radius: 1rem;
    padding: 1.5rem;
    margin-bottom: 1.5rem;
    transition: all 0.3s ease;
}
.form-section:hover {
    border-color: #005696;
    box-shadow: 0 4px 12px rgba(0, 86, 150, 0.1);
}

.form-label {
    display: block;
    font-size: 0.875rem;
    font-weight: 600;
    color: #1e293b;
    margin-bottom: 0.5rem;
}

.form-control, .form-select {
    width: 100%;
    padding: 0.75rem 1rem;
    border: 1px solid #e2e8f0;
    border-radius: 0.75rem;
    font-size: 0.875rem;
    transition: all 0.2s ease;
}
.form-control:focus, .form-select:focus {
    outline: none;
    border-color: #005696;
    box-shadow: 0 0 0 3px rgba(0, 86, 150, 0.05);
}

.hh-btn-primary {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: #005696;
    color: white;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
    border: none;
    cursor: pointer;
}
.hh-btn-primary:hover {
    background: #007bbd;
}

.hh-btn-secondary {
    display: inline-flex;
    align-items: center;
    gap: 0.5rem;
    padding: 0.75rem 1.5rem;
    background: white;
    color: #64748b;
    border: 1px solid #e2e8f0;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
}
.hh-btn-secondary:hover {
    background: #f1f5f9;
    border-color: #005696;
}

.section-header {
    padding: 1rem 1.5rem;
    border-bottom: 2px solid #e2e8f0;
    background: linear-gradient(to right, #f8fafc, #f1f5f9);
    display: flex;
    align-items: center;
    gap: 0.75rem;
    margin: -1.5rem -1.5rem 1.5rem -1.5rem;
    border-radius: 1rem 1rem 0 0;
}

.section-icon {
    width: 40px;
    height: 40px;
    border-radius: 0.75rem;
    display: flex;
    align-items: center;
    justify-content: center;
}
</style>
{% endblock %}

{% block content %}
<div class="p-6">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="flex justify-between items-center">
            <div>
                <h1 class="text-2xl font-bold flex items-center gap-3">
                    <i data-lucide="plus-circle" class="w-6 h-6"></i>
                    {% trans "Create New Standard" %}
                </h1>
                <p class="text-blue-100 text-sm mt-1">{% trans "Add a new compliance standard" %}</p>
            </div>
            <div class="flex gap-2">
                {% if department_id %}
                <a href="{% url 'standards:department_standards' pk=department_id %}" class="hh-btn hh-btn-secondary bg-white/10 border-white/30 text-white hover:bg-white hover:text-navy">
                    <i data-lucide="arrow-left" class="w-4 h-4"></i>
                    {% trans "Back to Department Standards" %}
                </a>
                {% else %}
                <a href="{% url 'standards:dashboard' %}" class="hh-btn hh-btn-secondary bg-white/10 border-white/30 text-white hover:bg-white hover:text-navy">
                    <i data-lucide="arrow-left" class="w-4 h-4"></i>
                    {% trans "Back to Dashboard" %}
                </a>
                {% endif %}
            </div>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <div class="lg:col-span-2">
            <!-- Standard Form -->
            <div class="form-section p-0 overflow-hidden">
                <div class="section-header">
                    <div class="section-icon bg-navy/10">
                        <i data-lucide="clipboard-list" class="w-5 h-5 text-navy"></i>
                    </div>
                    <h5 class="text-lg font-semibold text-navy m-0">{% trans "Standard Information" %}</h5>
                </div>
                <div class="p-6 pt-0">
                    <form method="post">
                        {% csrf_token %}

                        {% if form.non_field_errors %}
                        <div class="bg-red-50 border border-red-200 text-red-800 p-4 rounded-xl mb-4">
                            <strong class="flex items-center gap-2 mb-2"><i data-lucide="alert-triangle" class="w-5 h-5"></i>{% trans "Please fix the following errors:" %}</strong>
                            <ul class="mb-0 mt-2 list-disc list-inside">
                                {% for error in form.non_field_errors %}
                                <li>{{ error }}</li>
                                {% endfor %}
                            </ul>
                        </div>
                        {% endif %}

                        {% if form.errors %}
                        <div class="bg-red-50 border border-red-200 text-red-800 p-4 rounded-xl mb-4">
                            <strong class="flex items-center gap-2 mb-2"><i data-lucide="alert-triangle" class="w-5 h-5"></i>{% trans "Please fix the errors below." %}</strong>
                        </div>
                        {% endif %}

                        <div class="mb-4">
                            <label for="{{ form.source.id_for_label }}" class="form-label">
                                {{ form.source.label }} <span class="text-red-500">*</span>
                            </label>
                            {{ form.source }}
                            {% if form.source.help_text %}
                            <p class="mt-1 text-xs text-slate">{{ form.source.help_text }}</p>
                            {% endif %}
                            {% if form.source.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.source.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.category.id_for_label }}" class="form-label">
                                {{ form.category.label }} <span class="text-red-500">*</span>
                            </label>
                            {{ form.category }}
                            {% if form.category.help_text %}
                            <p class="mt-1 text-xs text-slate">{{ form.category.help_text }}</p>
                            {% endif %}
                            {% if form.category.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.category.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.code.id_for_label }}" class="form-label">
                                {{ form.code.label }} <span class="text-red-500">*</span>
                            </label>
                            {{ form.code }}
                            {% if form.code.help_text %}
                            <p class="mt-1 text-xs text-slate">{{ form.code.help_text }}</p>
                            {% endif %}
                            {% if form.code.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.code.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.title.id_for_label }}" class="form-label">
                                {{ form.title.label }} <span class="text-red-500">*</span>
                            </label>
                            {{ form.title }}
                            {% if form.title.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.title.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.title_ar.id_for_label }}" class="form-label">
                                {{ form.title_ar.label }}
                            </label>
                            {{ form.title_ar }}
                            {% if form.title_ar.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.title_ar.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.description.id_for_label }}" class="form-label">
                                {{ form.description.label }} <span class="text-red-500">*</span>
                            </label>
                            {{ form.description }}
                            {% if form.description.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.description.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label class="form-label">
                                {% trans "Departments" %}
                            </label>
                            <p class="mt-1 text-xs text-slate mb-2">{% trans "Select departments this standard applies to. Leave all unchecked to apply to all departments." %}</p>
                            <div class="grid grid-cols-2 md:grid-cols-3 gap-2 max-h-48 overflow-y-auto border border-slate-200 rounded-lg p-3">
                                {% for dept in departments %}
                                <label class="flex items-center gap-2 cursor-pointer p-1.5 rounded hover:bg-slate-50">
                                    <input type="checkbox" name="departments" value="{{ dept.id }}" class="rounded border-slate-300 text-navy focus:ring-navy/20 w-4 h-4"
                                        {% if dept.id in form.departments.value or dept.id == form.departments.initial.0.id %}checked{% endif %}>
                                    <span class="text-sm text-slate-700">{{ dept.name }}</span>
                                </label>
                                {% empty %}
                                <p class="col-span-full text-xs text-slate-400">{% trans "No departments available" %}</p>
                                {% endfor %}
                            </div>
                            {% if form.departments.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.departments.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.activity_type.id_for_label }}" class="form-label">
                                {% trans "Activity Type" %}
                            </label>
                            {{ form.activity_type }}
                            {% if form.activity_type.help_text %}
                            <p class="mt-1 text-xs text-slate">{{ form.activity_type.help_text }}</p>
                            {% endif %}
                            {% if form.activity_type.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.activity_type.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="id_parent_standard" class="form-label">
                                {% trans "Parent Standard" %}
                            </label>
                            <select name="parent_standard" id="id_parent_standard" class="form-select">
                                <option value="">---------</option>
                            </select>
                            <p class="mt-1 text-xs text-slate">{% trans "Select a parent standard if this is a sub-standard (e.g., 4.3.1 under 4.3)" %}</p>
                            {% if form.parent_standard.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.parent_standard.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="grid grid-cols-2 gap-4 mb-4">
                            <div>
                                <label for="{{ form.effective_date.id_for_label }}" class="form-label">
                                    {{ form.effective_date.label }}
                                </label>
                                {{ form.effective_date }}
                                {% if form.effective_date.errors %}
                                <p class="mt-1 text-xs text-red-600">{{ form.effective_date.errors }}</p>
                                {% endif %}
                            </div>
                            <div>
                                <label for="{{ form.review_date.id_for_label }}" class="form-label">
                                    {{ form.review_date.label }}
                                </label>
                                {{ form.review_date }}
                                {% if form.review_date.errors %}
                                <p class="mt-1 text-xs text-red-600">{{ form.review_date.errors }}</p>
                                {% endif %}
                            </div>
                        </div>

                        <div class="grid grid-cols-2 gap-4 mb-4">
                            <div>
                                <label class="flex items-center gap-2 cursor-pointer">
                                    {{ form.is_heading }}
                                    <span class="text-sm font-medium text-navy">{{ form.is_heading.label }}</span>
                                </label>
                                <p class="mt-1 text-xs text-slate">{% trans "Section headers don't require assessment" %}</p>
                            </div>
                            <div>
                                <label class="flex items-center gap-2 cursor-pointer">
                                    {{ form.is_assessable }}
                                    <span class="text-sm font-medium text-navy">{{ form.is_assessable.label }}</span>
                                </label>
                                <p class="mt-1 text-xs text-slate">{% trans "Uncheck for informational standards only" %}</p>
                            </div>
                        </div>

                        <div class="mb-6">
                            <label class="flex items-center gap-2 cursor-pointer">
                                {{ form.is_active }}
                                <span class="text-sm font-medium text-navy">{{ form.is_active.label }}</span>
                            </label>
                        </div>

                        <div class="flex gap-3 pt-4 border-t border-slate-200">
                            <button type="submit" class="hh-btn hh-btn-primary">
                                <i data-lucide="save" class="w-4 h-4"></i>
                                {% trans "Create Standard" %}
                            </button>
                            {% if department_id %}
                            <a href="{% url 'standards:department_standards' pk=department_id %}" class="hh-btn hh-btn-secondary">
                                <i data-lucide="x" class="w-4 h-4"></i>
                                {% trans "Cancel" %}
                            </a>
                            {% else %}
                            <a href="{% url 'standards:dashboard' %}" class="hh-btn hh-btn-secondary">
                                <i data-lucide="x" class="w-4 h-4"></i>
                                {% trans "Cancel" %}
                            </a>
                            {% endif %}
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="lg:col-span-1">
            <!-- Help Section -->
            <div class="form-section p-0 overflow-hidden">
                <div class="section-header">
                    <div class="section-icon bg-blue/10">
                        <i data-lucide="help-circle" class="w-5 h-5 text-blue"></i>
                    </div>
                    <h5 class="text-lg font-semibold text-navy m-0">{% trans "Help" %}</h5>
                </div>
                <div class="p-6 pt-0">
                    <div class="mb-4">
                        <h6 class="text-sm font-semibold text-navy mb-2">{% trans "Standard Code Format" %}</h6>
                        <p class="text-xs text-slate mb-2">
                            {% trans "Use a unique code to identify this standard" %}
                        </p>
                        <div class="text-xs text-slate space-y-1">
                            <p><strong>{% trans "Examples:" %}</strong></p>
                            <p class="ml-2">• STD-001</p>
                            <p class="ml-2">• QM-05</p>
                            <p class="ml-2">• PS-12</p>
                        </div>
                    </div>

                    <div class="mb-4">
                        <h6 class="text-sm font-semibold text-navy mb-2">{% trans "Department Assignment" %}</h6>
                        <p class="text-xs text-slate">
                            {% trans "Select one or more departments this standard applies to. Leave all departments unchecked if the standard is global and applies to all departments." %}
                        </p>
                    </div>

                    <div>
                        <h6 class="text-sm font-semibold text-navy mb-2">{% trans "Dates" %}</h6>
                        <p class="text-xs text-slate">
                            {% 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>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();
    
    const formInputs = document.querySelectorAll('input[type="text"], input[type="date"], select, textarea');
    formInputs.forEach(input => {
        if (!input.classList.contains('form-control') && input.type !== 'checkbox') {
            input.classList.add('form-control');
        }
    });

    const checkboxes = document.querySelectorAll('input[type="checkbox"]');
    checkboxes.forEach(checkbox => {
        if (!checkbox.classList.contains('rounded')) {
            checkbox.classList.add('rounded', 'border-slate-300', 'text-navy', 'focus:ring-navy/20', 'w-4', 'h-4');
        }
    });

    const sourceSelect = document.querySelector('select[name="source"]');
    const categorySelect = document.querySelector('select[name="category"]');
    const parentSelect = document.querySelector('select[name="parent_standard"]');
    const allCategories = JSON.parse('{{ categories_json|escapejs }}');
    const allParents = JSON.parse('{{ parents_json|escapejs }}');
    const currentParent = '{{ form.parent_standard.value|default:"" }}';

    function updateCategories() {
        const sourceId = sourceSelect.value;
        categorySelect.innerHTML = '<option value="">---------</option>';
        allCategories.forEach(function(cat) {
            if (!sourceId || cat.source_id === sourceId) {
                const opt = document.createElement('option');
                opt.value = cat.id;
                opt.textContent = cat.name;
                categorySelect.appendChild(opt);
            }
        });
    }

    function updateParents() {
        const sourceId = sourceSelect.value;
        parentSelect.innerHTML = '<option value="">---------</option>';
        allParents.forEach(function(p) {
            if (!sourceId || p.source_id === sourceId) {
                const opt = document.createElement('option');
                opt.value = p.id;
                opt.textContent = p.code + ': ' + p.title;
                if (p.id === currentParent) opt.selected = true;
                parentSelect.appendChild(opt);
            }
        });
    }

    if (sourceSelect && categorySelect) {
        sourceSelect.addEventListener('change', function() {
            updateCategories();
            updateParents();
        });
    }

    updateParents();
});
</script>
{% endblock %}
