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

{% block title %}{% if source %}{% trans "Update Source" %}{% else %}{% trans "Create Source" %}{% endif %}{% endblock %}

{% block extra_css %}
<style>
    :root {
        --hh-navy: #005696;
        --hh-blue: #007bbd;
        --hh-light: #eef6fb;
        --hh-slate: #64748b;
    }

    .page-header-gradient {
        background: linear-gradient(135deg, var(--hh-navy) 0%, #0069a8 50%, var(--hh-blue) 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);
    }

    .section-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        transition: all 0.3s ease;
    }
    .section-card:hover {
        border-color: #005696;
        box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
    }

    .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;
    }

    .section-icon {
        width: 40px;
        height: 40px;
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }

    .btn-transition {
        transition: all 0.2s ease-in-out;
    }
</style>
{% endblock %}

{% block content %}
<div class="px-6 py-4">
    <div class="page-header-gradient">
        <div class="flex justify-between items-center">
            <div>
                <h1 class="text-2xl font-bold mb-1">{% if source %}{% trans "Update Source" %}{% else %}{% trans "Create Source" %}{% endif %}</h1>
                <p class="text-blue-100 text-sm">{% if source %}{% trans "Edit standard source" %}{% else %}{% trans "Add new standard source" %}{% endif %}</p>
            </div>
            <div class="flex gap-2">
                <a href="{% url 'standards:source_list' %}" class="btn-transition inline-flex items-center px-4 py-2.5 bg-white/10 border-2 border-white/30 text-white font-semibold rounded-xl hover:bg-white hover:text-navy transition">
                    <i data-lucide="arrow-left" class="w-4 h-4 me-2"></i>{% trans "Back to Sources" %}
                </a>
            </div>
        </div>
    </div>

    <div class="grid grid-cols-1 lg:grid-cols-3 gap-6">
        <div class="lg:col-span-2">
            <div class="section-card">
                <div class="section-header">
                    <div class="section-icon bg-navy/10">
                        <i data-lucide="folder" class="w-5 h-5 text-navy"></i>
                    </div>
                    <h5 class="text-lg font-semibold text-navy">{% trans "Source Information" %}</h5>
                </div>
                <div class="p-6">
                    <form method="post">
                        {% csrf_token %}

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

                        <div class="mb-4">
                            <label for="{{ form.name_ar.id_for_label }}" class="block text-sm font-semibold text-slate mb-2">
                                {{ form.name_ar.label }}
                            </label>
                            {{ form.name_ar }}
                            {% if form.name_ar.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.name_ar.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.code.id_for_label }}" class="block text-sm font-semibold text-slate mb-2">
                                {{ form.code.label }} <span class="text-red-500">*</span>
                            </label>
                            {{ form.code }}
                            <p class="mt-1 text-xs text-slate">{% trans "Unique code for the source (e.g., CBAHI, JCI, ISO)" %}</p>
                            {% 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.website.id_for_label }}" class="block text-sm font-semibold text-slate mb-2">
                                {{ form.website.label }}
                            </label>
                            {{ form.website }}
                            <p class="mt-1 text-xs text-slate">{% trans "Official website URL (optional)" %}</p>
                            {% if form.website.errors %}
                            <p class="mt-1 text-xs text-red-600">{{ form.website.errors }}</p>
                            {% endif %}
                        </div>

                        <div class="mb-4">
                            <label for="{{ form.description.id_for_label }}" class="block text-sm font-semibold text-slate mb-2">
                                {{ form.description.label }}
                            </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-6">
                            <label class="flex items-center">
                                {{ form.is_active }}
                                <span class="ml-2 text-sm text-slate-700">{{ form.is_active.label }}</span>
                            </label>
                        </div>

                        <div class="flex gap-3">
                            <button type="submit" class="btn-transition inline-flex items-center px-6 py-2.5 bg-green-500 text-white font-semibold rounded-xl hover:bg-green-600 transition">
                                <i data-lucide="save" class="w-4 h-4 me-2"></i>{% if source %}{% trans "Update Source" %}{% else %}{% trans "Create Source" %}{% endif %}
                            </button>
                            <a href="{% url 'standards:source_list' %}" class="btn-transition inline-flex items-center px-6 py-2.5 border border-slate-200 text-slate rounded-xl font-semibold hover:bg-slate-50 transition">
                                {% trans "Cancel" %}
                            </a>
                        </div>
                    </form>
                </div>
            </div>
        </div>

        <div class="lg:col-span-1">
            <div class="section-card">
                <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">{% trans "Help" %}</h5>
                </div>
                <div class="p-6">
                    <div class="mb-4">
                        <h6 class="text-sm font-semibold text-navy mb-2">{% trans "Source Code" %}</h6>
                        <p class="text-xs text-slate mb-2">
                            {% trans "Use a unique code to identify the standard source organization." %}
                        </p>
                        <div class="text-xs text-slate space-y-1">
                            <p><strong>{% trans "Examples:" %}</strong></p>
                            <p class="ml-2">• CBAHI (Central Board for Accreditation of Healthcare Institutions)</p>
                            <p class="ml-2">• JCI (Joint Commission International)</p>
                            <p class="ml-2">• ISO (International Organization for Standardization)</p>
                        </div>
                    </div>

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

<script>
document.addEventListener('DOMContentLoaded', function() {
    const formInputs = document.querySelectorAll('input[type="text"], input[type="url"], select, textarea');
    formInputs.forEach(input => {
        if (!input.classList.contains('form-control') && input.type !== 'checkbox') {
            input.classList.add('w-full', 'px-4', 'py-2.5', 'border', 'border-slate-200', 'rounded-lg', 'focus:ring-2', 'focus:ring-navy/20', 'outline-none', 'transition');
        }
    });

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