{% extends "layouts/base.html" %}
{% load i18n %}

{% block title %}{% if source %}{% trans "Edit Source" %}{% else %}{% trans "Create Source" %}{% endif %} - PX360{% 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: 2px 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: 2px 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.1);
}

.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: 2px solid #e2e8f0;
    border-radius: 0.75rem;
    font-weight: 600;
    transition: all 0.2s ease;
}
.hh-btn-secondary:hover {
    background: #f1f5f9;
    border-color: #005696;
}

.checkbox-wrapper {
    display: flex;
    align-items: center;
    gap: 0.75rem;
    padding: 1rem;
    background: #eef6fb;
    border-radius: 0.75rem;
    cursor: pointer;
    transition: all 0.2s ease;
}
.checkbox-wrapper:hover {
    background: #e0f2fe;
}

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

.form-input[readonly] {
    background: #f1f5f9;
    cursor: not-allowed;
}

textarea.form-input {
    resize: vertical;
    min-height: 120px;
}

@keyframes fadeIn {
    from { opacity: 0; transform: translateY(20px); }
    to { opacity: 1; transform: translateY(0); }
}

.animate-in {
    animation: fadeIn 0.5s ease-out forwards;
}
</style>
{% endblock %}

{% block content %}
<div class="px-4 py-6">
    <!-- Breadcrumb -->
    <nav class="mb-4 animate-in">
        <ol class="flex items-center gap-2 text-sm text-slate">
            <li><a href="{% url 'px_sources:source_list' %}" class="text-blue hover:text-navy font-medium">{% trans "PX Sources" %}</a></li>
            <li><i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i></li>
            <li class="text-navy font-semibold">
                {% if source %}{% trans "Edit Source" %}{% else %}{% trans "Create Source" %}{% endif %}
            </li>
        </ol>
    </nav>

    <!-- Page Header -->
    <div class="page-header-gradient animate-in">
        <div class="flex justify-between items-center">
            <div>
                <h1 class="text-2xl font-bold flex items-center gap-3">
                    <i data-lucide="{% if source %}edit{% else %}plus-circle{% endif %}" class="w-6 h-6"></i>
                    {% if source %}{% trans "Edit Source" %}{% else %}{% trans "Create Source" %}{% endif %}
                </h1>
                {% if source %}
                <p class="text-blue-100 text-sm mt-1">{{ source.get_localized_name }} <span class="text-white/70">({{ source.code }})</span></p>
                {% endif %}
            </div>
            <a href="{% url 'px_sources:source_list' %}" 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 List" %}
            </a>
        </div>
    </div>

    <!-- Form Section -->
    <div class="max-w-4xl animate-in">
        <div class="form-section">
            <div class="flex items-center gap-3 mb-6 pb-4 border-b border-slate-200">
                <div class="w-10 h-10 bg-blue/10 rounded-xl flex items-center justify-center">
                    <i data-lucide="file-text" class="w-5 h-5 text-blue"></i>
                </div>
                <h2 class="text-lg font-bold text-navy">{% trans "Source Information" %}</h2>
            </div>
            
            <form method="post">
                {% csrf_token %}

                <div class="grid grid-cols-1 md:grid-cols-3 gap-5 mb-5">
                    <div class="md:col-span-1">
                        <label for="code" class="form-label">
                            {% trans "Source Code" %}
                        </label>
                        <input type="text" id="code" name="code"
                               value="{{ source.code|default:'' }}"
                               placeholder="{% trans 'Auto-generated' %}"
                               class="form-input"
                               {% if source %}readonly{% endif %}>
                        {% if not source %}
                        <p class="text-xs text-slate mt-1">{% trans "Auto-generated from name if left blank" %}</p>
                        {% endif %}
                    </div>
                    <div class="md:col-span-2">
                        <label for="name_en" class="form-label">
                            {% trans "Name (English)" %} <span class="text-red-500">*</span>
                        </label>
                        <input type="text" id="name_en" name="name_en"
                               value="{{ source.name_en|default:'' }}" required
                               placeholder="{% trans 'e.g., Patient Portal' %}"
                               class="form-input">
                    </div>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-5 mb-5">
                    <div>
                        <label for="name_ar" class="form-label">
                            {% trans "Name (Arabic)" %}
                        </label>
                        <input type="text" id="name_ar" name="name_ar"
                               value="{{ source.name_ar|default:'' }}"
                               placeholder="{% trans 'e.g., بوابة المريض' %}"
                               dir="rtl"
                               class="form-input">
                    </div>
                    <div>
                        <label for="source_type" class="form-label">
                            {% trans "Source Type" %}
                        </label>
                        <select id="source_type" name="source_type" class="form-control">
                            {% for value, label in source_types %}
                            <option value="{{ value }}" {% if source.source_type == value %}selected{% endif %}>
                                {{ label }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                </div>

                <div class="mb-5">
                    <label for="description" class="form-label">
                        {% trans "Description" %}
                    </label>
                    <textarea id="description" name="description" rows="4"
                              placeholder="{% trans 'Enter source description...' %}"
                              class="form-input">{{ source.description|default:'' }}</textarea>
                </div>

                <div class="grid grid-cols-1 md:grid-cols-2 gap-5 mb-5">
                    <div>
                        <label for="contact_email" class="form-label">
                            {% trans "Contact Email" %}
                        </label>
                        <input type="email" id="contact_email" name="contact_email"
                               value="{{ source.contact_email|default:'' }}"
                               placeholder="contact@example.com"
                               class="form-input">
                    </div>
                    <div>
                        <label for="contact_phone" class="form-label">
                            {% trans "Contact Phone" %}
                        </label>
                        <input type="text" id="contact_phone" name="contact_phone"
                               value="{{ source.contact_phone|default:'' }}"
                               placeholder="+966 XX XXX XXXX"
                               class="form-input">
                    </div>
                </div>

                <div class="mb-6">
                    <label class="checkbox-wrapper">
                        <input type="checkbox" id="is_active" name="is_active" value="true"
                               {% if source.is_active|default_if_none:True %}checked{% endif %}
                               class="w-5 h-5 rounded border-slate-300 text-navy focus:ring-blue">
                        <div>
                            <span class="text-navy font-semibold">{% trans "Active" %}</span>
                            <p class="text-slate text-sm m-0">{% trans "Source is available for selection" %}</p>
                        </div>
                    </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>
                        {% if source %}{% trans "Save Changes" %}{% else %}{% trans "Create Source" %}{% endif %}
                    </button>
                    <a href="{% url 'px_sources:source_list' %}" class="hh-btn hh-btn-secondary">
                        <i data-lucide="x" class="w-4 h-4"></i>
                        {% trans "Cancel" %}
                    </a>
                </div>
            </form>
        </div>
    </div>
</div>

<script>
document.addEventListener('DOMContentLoaded', function() {
    lucide.createIcons();

    // Auto-generate code preview from name
    const nameEnInput = document.getElementById('name_en');
    const codeInput = document.getElementById('code');
    
    if (nameEnInput && codeInput && !codeInput.value) {
        nameEnInput.addEventListener('blur', function() {
            const name = this.value.trim().toUpperCase();
            if (name && !codeInput.value) {
                const words = name.split(' ');
                let code;
                if (words.length >= 2) {
                    code = words.slice(0, 3).map(w => w.substring(0, 4)).join('-');
                } else {
                    code = name.substring(0, 10).replace(' ', '-');
                }
                codeInput.value = code;
            }
        });
    }
});
</script>
{% endblock %}
