{% extends base_layout %}
{% load i18n %}
{% load static %}

{% block title %}{{ _("New Inquiry")}} - PX360{% endblock %}

{% block extra_css %}
<style>
    .form-section {
        background: #fff;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 25px;
        margin-bottom: 20px;
    }
    .form-section-title {
        font-size: 1.1rem;
        font-weight: 600;
        color: #495057;
        margin-bottom: 20px;
        padding-bottom: 10px;
        border-bottom: 2px solid #17a2b8;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="mb-4">
        {% if source_user %}
        <a href="{% url 'px_sources:source_user_inquiry_list' %}" class="btn btn-outline-secondary btn-sm mb-3">
            <i class="bi bi-arrow-left me-1"></i> {{ _("Back to My Inquiries")}}
        </a>
        {% else %}
        <a href="{% url 'complaints:inquiry_list' %}" class="btn btn-outline-secondary btn-sm mb-3">
            <i class="bi bi-arrow-left me-1"></i> {{ _("Back to Inquiries")}}
        </a>
        {% endif %}
        <h2 class="mb-1">
            <i class="bi bi-plus-circle text-info me-2"></i>
            {{ _("Create New Inquiry")}}
        </h2>
        <p class="text-muted mb-0">{{ _("Create a new patient inquiry or request")}}</p>
    </div>

    <form method="post" action="{% url 'complaints:inquiry_create' %}" id="inquiryForm">
        {% csrf_token %}
        
        {% if form.non_field_errors %}
        <div class="alert alert-danger">
            {{ form.non_field_errors }}
        </div>
        {% endif %}
        
        <div class="row">
            <div class="col-lg-8">
                <!-- Organization Information -->
                <div class="form-section">
                    <h5 class="form-section-title">
                        <i class="bi bi-hospital me-2"></i>{{ _("Organization") }}
                    </h5>
                    
                    <div class="mb-3">
                        {{ form.hospital.label_tag }}
                        {{ form.hospital }}
                        {% if form.hospital.help_text %}
                        <small class="form-text text-muted">{{ form.hospital.help_text }}</small>
                        {% endif %}
                        {% for error in form.hospital.errors %}
                        <div class="invalid-feedback d-block">{{ error }}</div>
                        {% endfor %}
                    </div>
                    
                    <div class="mb-3">
                        {{ form.department.label_tag }}
                        {{ form.department }}
                        {% for error in form.department.errors %}
                        <div class="invalid-feedback d-block">{{ error }}</div>
                        {% endfor %}
                    </div>
                </div>

                <!-- Contact/Patient Information -->
                <div class="form-section">
                    <h5 class="form-section-title">
                        <i class="bi bi-person-fill me-2"></i>{{ _("Contact Information")}}
                    </h5>
                    
                    <!-- Patient Field -->
                    <div class="mb-3">
                        {{ form.patient.label_tag }}
                        {{ form.patient }}
                        {% for error in form.patient.errors %}
                        <div class="invalid-feedback d-block">{{ error }}</div>
                        {% endfor %}
                    </div>

                    <div class="mb-3">
                        {{ form.contact_name.label_tag }}
                        {{ form.contact_name }}
                        {% for error in form.contact_name.errors %}
                        <div class="invalid-feedback d-block">{{ error }}</div>
                        {% endfor %}
                    </div>

                    <div class="row">
                        <div class="col-md-6 mb-3">
                            {{ form.contact_phone.label_tag }}
                            {{ form.contact_phone }}
                            {% for error in form.contact_phone.errors %}
                            <div class="invalid-feedback d-block">{{ error }}</div>
                            {% endfor %}
                        </div>
                        <div class="col-md-6 mb-3">
                            {{ form.contact_email.label_tag }}
                            {{ form.contact_email }}
                            {% for error in form.contact_email.errors %}
                            <div class="invalid-feedback d-block">{{ error }}</div>
                            {% endfor %}
                        </div>
                    </div>
                </div>

                <!-- Inquiry Details -->
                <div class="form-section">
                    <h5 class="form-section-title">
                        <i class="bi bi-file-text me-2"></i>{{ _("Inquiry Details")}}
                    </h5>
                    
                    <div class="mb-3">
                        {{ form.category.label_tag }}
                        {{ form.category }}
                        {% for error in form.category.errors %}
                        <div class="invalid-feedback d-block">{{ error }}</div>
                        {% endfor %}
                    </div>

                    <div class="mb-3">
                        {{ form.subject.label_tag }}
                        {{ form.subject }}
                        {% for error in form.subject.errors %}
                        <div class="invalid-feedback d-block">{{ error }}</div>
                        {% endfor %}
                    </div>

                    <div class="mb-3">
                        {{ form.message.label_tag }}
                        {{ form.message }}
                        {% for error in form.message.errors %}
                        <div class="invalid-feedback d-block">{{ error }}</div>
                        {% endfor %}
                    </div>
                </div>
            </div>

            <!-- Sidebar -->
            <div class="col-lg-4">

                <!-- Help Information -->
                <div class="alert alert-info">
                    <h6 class="alert-heading">
                        <i class="bi bi-info-circle me-2"></i>{{ _("Help")}}
                    </h6>
                    <p class="mb-0 small">
                        {{ _("Use this form to create a new inquiry from a patient or visitor.")}}
                    </p>
                    <hr class="my-2">
                    <p class="mb-0 small">
                        {{ _("Fill in the inquiry details. Fields marked with * are required.")}}
                    </p>
                </div>

                <!-- Action Buttons -->
                <div class="d-grid gap-2">
                    <button type="submit" class="btn btn-primary btn-lg">
                        <i class="bi bi-check-circle me-2"></i>{{ _("Create Inquiry")}}
                    </button>
                    {% if source_user %}
                    <a href="{% url 'px_sources:source_user_inquiry_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i>{{ _("Cancel") }}
                    </a>
                    {% else %}
                    <a href="{% url 'complaints:inquiry_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-2"></i>{{ _("Cancel") }}
                    </a>
                    {% endif %}
                </div>
            </div>
        </div>
    </form>
</div>
{% endblock %}

{% block extra_js %}
<script>
// Department loading
document.getElementById('{{ form.hospital.id_for_label }}')?.addEventListener('change', function() {
    const hospitalId = this.value;
    const departmentSelect = document.getElementById('{{ form.department.id_for_label }}');
    
    if (!hospitalId) {
        departmentSelect.innerHTML = '<option value="">{{ _("Select department")}}</option>';
        return;
    }
    
    fetch(`/complaints/ajax/departments/?hospital_id=${hospitalId}`)
        .then(response => response.json())
        .then(data => {
            departmentSelect.innerHTML = '<option value="">{{ _("Select department")}}</option>';
            data.departments.forEach(dept => {
                const option = document.createElement('option');
                option.value = dept.id;
                option.textContent = dept.name_en || dept.name;
                departmentSelect.appendChild(option);
            });
        })
        .catch(error => console.error('Error loading departments:', error));
});

// Patient search (optional - for better UX)
const patientSelect = document.getElementById('{{ form.patient.id_for_label }}');
if (patientSelect) {
    patientSelect.addEventListener('change', function() {
        const selectedOption = this.options[this.selectedIndex];
        if (!selectedOption || selectedOption.value === '') {
            document.getElementById('patient-results').style.display = 'none';
        }
    });
}
</script>
{% endblock %}
