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

{% block title %}{% if is_create %}Create{% else %}Edit{% endif %} Feedback - PX360{% endblock %}

{% block extra_css %}
<style>
    .form-card {
        border: 1px solid #dee2e6;
        border-radius: 8px;
        margin-bottom: 20px;
    }
    .form-card-header {
        background: #f8f9fa;
        border-bottom: 1px solid #dee2e6;
        padding: 15px 20px;
        font-weight: 600;
    }
    .form-card-body {
        padding: 20px;
    }
    .required-field::after {
        content: " *";
        color: #dc3545;
    }
    .form-help-text {
        font-size: 0.875rem;
        color: #6c757d;
        margin-top: 0.25rem;
    }
    .rating-input {
        display: flex;
        gap: 10px;
        align-items: center;
    }
    .rating-input input[type="radio"] {
        display: none;
    }
    .rating-input label {
        font-size: 2rem;
        color: #ddd;
        cursor: pointer;
        transition: color 0.2s;
    }
    .rating-input input[type="radio"]:checked ~ label,
    .rating-input label:hover,
    .rating-input label:hover ~ label {
        color: #ffc107;
    }
    .rating-input {
        flex-direction: row-reverse;
        justify-content: flex-end;
    }
</style>
{% endblock %}

{% block content %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <nav aria-label="breadcrumb">
                <ol class="breadcrumb mb-2">
                    <li class="breadcrumb-item"><a href="{% url 'feedback:feedback_list' %}">{% trans "Feedback" %}</a></li>
                    <li class="breadcrumb-item active">{% if is_create %}Create{% else %}Edit{% endif %}</li>
                </ol>
            </nav>
            <h2 class="mb-0">
                <i class="bi bi-chat-heart-fill text-primary me-2"></i>
                {% if is_create %}Create New Feedback{% else %}Edit Feedback{% endif %}
            </h2>
        </div>
        <div>
            <a href="{% url 'feedback:feedback_list' %}" class="btn btn-outline-secondary">
                <i class="bi bi-x-circle me-1"></i> Cancel
            </a>
        </div>
    </div>

    <div class="row">
        <div class="col-lg-8 mx-auto">
            <form method="post" id="feedbackForm">
                {% csrf_token %}
                
                <!-- Display form errors -->
                {% if form.non_field_errors %}
                <div class="alert alert-danger alert-dismissible fade show" role="alert">
                    {{ form.non_field_errors }}
                    <button type="button" class="btn-close" data-bs-dismiss="alert"></button>
                </div>
                {% endif %}

                <!-- Patient/Contact Information -->
                <div class="form-card">
                    <div class="form-card-header">
                        <i class="bi bi-person me-2"></i>Patient/Contact Information
                    </div>
                    <div class="form-card-body">
                        <!-- Anonymous Checkbox -->
                        <div class="mb-3">
                            <div class="form-check">
                                {{ form.is_anonymous }}
                                <label class="form-check-label" for="{{ form.is_anonymous.id_for_label }}">
                                    Submit as Anonymous Feedback
                                </label>
                            </div>
                            {% if form.is_anonymous.errors %}
                            <div class="text-danger small">{{ form.is_anonymous.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Patient Selection (shown when not anonymous) -->
                        <div class="mb-3" id="patientField">
                            <label for="{{ form.patient.id_for_label }}" class="form-label">{% trans "Patient" %}</label>
                            {{ form.patient }}
                            {% if form.patient.help_text %}
                            <div class="form-help-text">{{ form.patient.help_text }}</div>
                            {% endif %}
                            {% if form.patient.errors %}
                            <div class="text-danger small">{{ form.patient.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Anonymous Contact Fields (shown when anonymous) -->
                        <div id="anonymousFields" style="display: none;">
                            <div class="mb-3">
                                <label for="{{ form.contact_name.id_for_label }}" class="form-label required-field">{% trans "Contact Name" %}</label>
                                {{ form.contact_name }}
                                {% if form.contact_name.errors %}
                                <div class="text-danger small">{{ form.contact_name.errors }}</div>
                                {% endif %}
                            </div>

                            <div class="row">
                                <div class="col-md-6 mb-3">
                                    <label for="{{ form.contact_email.id_for_label }}" class="form-label">{% trans "Email" %}</label>
                                    {{ form.contact_email }}
                                    {% if form.contact_email.errors %}
                                    <div class="text-danger small">{{ form.contact_email.errors }}</div>
                                    {% endif %}
                                </div>
                                <div class="col-md-6 mb-3">
                                    <label for="{{ form.contact_phone.id_for_label }}" class="form-label">{% trans "Phone" %}</label>
                                    {{ form.contact_phone }}
                                    {% if form.contact_phone.errors %}
                                    <div class="text-danger small">{{ form.contact_phone.errors }}</div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- Feedback Details -->
                <div class="form-card">
                    <div class="form-card-header">
                        <i class="bi bi-chat-text me-2"></i>Feedback Details
                    </div>
                    <div class="form-card-body">
                        <!-- Feedback Type -->
                        <div class="mb-3">
                            <label for="{{ form.feedback_type.id_for_label }}" class="form-label required-field">{% trans "Feedback Type" %}</label>
                            {{ form.feedback_type }}
                            {% if form.feedback_type.errors %}
                            <div class="text-danger small">{{ form.feedback_type.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Title -->
                        <div class="mb-3">
                            <label for="{{ form.title.id_for_label }}" class="form-label required-field">{% trans "Title" %}</label>
                            {{ form.title }}
                            {% if form.title.errors %}
                            <div class="text-danger small">{{ form.title.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Message -->
                        <div class="mb-3">
                            <label for="{{ form.message.id_for_label }}" class="form-label required-field">{% trans "Message" %}</label>
                            {{ form.message }}
                            <div class="form-help-text">Please provide detailed feedback</div>
                            {% if form.message.errors %}
                            <div class="text-danger small">{{ form.message.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Category and Subcategory -->
                        <div class="row">
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.category.id_for_label }}" class="form-label required-field">{% trans "Category" %}</label>
                                {{ form.category }}
                                {% if form.category.errors %}
                                <div class="text-danger small">{{ form.category.errors }}</div>
                                {% endif %}
                            </div>
                            <div class="col-md-6 mb-3">
                                <label for="{{ form.subcategory.id_for_label }}" class="form-label">{% trans "Subcategory" %}</label>
                                {{ form.subcategory }}
                                {% if form.subcategory.errors %}
                                <div class="text-danger small">{{ form.subcategory.errors }}</div>
                                {% endif %}
                            </div>
                        </div>

                        <!-- Rating -->
                        <div class="mb-3">
                            <label class="form-label">{% trans "Rating (Optional)" %}</label>
                            <div class="rating-input">
                                <input type="radio" name="rating" value="5" id="rating5" {% if form.rating.value == 5 %}checked{% endif %}>
                                <label for="rating5"><i class="bi bi-star-fill"></i></label>
                                <input type="radio" name="rating" value="4" id="rating4" {% if form.rating.value == 4 %}checked{% endif %}>
                                <label for="rating4"><i class="bi bi-star-fill"></i></label>
                                <input type="radio" name="rating" value="3" id="rating3" {% if form.rating.value == 3 %}checked{% endif %}>
                                <label for="rating3"><i class="bi bi-star-fill"></i></label>
                                <input type="radio" name="rating" value="2" id="rating2" {% if form.rating.value == 2 %}checked{% endif %}>
                                <label for="rating2"><i class="bi bi-star-fill"></i></label>
                                <input type="radio" name="rating" value="1" id="rating1" {% if form.rating.value == 1 %}checked{% endif %}>
                                <label for="rating1"><i class="bi bi-star-fill"></i></label>
                            </div>
                            <div class="form-help-text">Rate your experience from 1 to 5 stars</div>
                            {% if form.rating.errors %}
                            <div class="text-danger small">{{ form.rating.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Priority -->
                        <div class="mb-3">
                            <label for="{{ form.priority.id_for_label }}" class="form-label">{% trans "Priority" %}</label>
                            {{ form.priority }}
                            {% if form.priority.errors %}
                            <div class="text-danger small">{{ form.priority.errors }}</div>
                            {% endif %}
                        </div>
                    </div>
                </div>

                <!-- Organization Information -->
                <div class="form-card">
                    <div class="form-card-header">
                        <i class="bi bi-building me-2"></i>Organization Information
                    </div>
                    <div class="form-card-body">
                        <!-- Hospital -->
                        <div class="mb-3">
                            <label for="{{ form.hospital.id_for_label }}" class="form-label required-field">{% trans "Hospital" %}</label>
                            {{ form.hospital }}
                            {% if form.hospital.errors %}
                            <div class="text-danger small">{{ form.hospital.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Department -->
                        <div class="mb-3">
                            <label for="{{ form.department.id_for_label }}" class="form-label">{% trans "Department" %}</label>
                            {{ form.department }}
                            <div class="form-help-text">Select the department related to this feedback (optional)</div>
                            {% if form.department.errors %}
                            <div class="text-danger small">{{ form.department.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Physician -->
                        <div class="mb-3">
                            <label for="{{ form.physician.id_for_label }}" class="form-label">{% trans "Physician" %}</label>
                            {{ form.physician }}
                            <div class="form-help-text">Select the physician mentioned in this feedback (optional)</div>
                            {% if form.physician.errors %}
                            <div class="text-danger small">{{ form.physician.errors }}</div>
                            {% endif %}
                        </div>

                        <!-- Encounter ID -->
                        <div class="mb-3">
                            <label for="{{ form.encounter_id.id_for_label }}" class="form-label">{% trans "Encounter ID" %}</label>
                            {{ form.encounter_id }}
                            <div class="form-help-text">Related encounter ID if applicable (optional)</div>
                            {% if form.encounter_id.errors %}
                            <div class="text-danger small">{{ form.encounter_id.errors }}</div>
                            {% endif %}
                        </div>
                    </div>
                </div>

                <!-- Form Actions -->
                <div class="d-flex justify-content-between align-items-center mb-4">
                    <a href="{% url 'feedback:feedback_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-1"></i> Cancel
                    </a>
                    <button type="submit" class="btn btn-primary">
                        <i class="bi bi-check-circle me-1"></i>
                        {% if is_create %}Create Feedback{% else %}Update Feedback{% endif %}
                    </button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    const anonymousCheckbox = document.getElementById('{{ form.is_anonymous.id_for_label }}');
    const patientField = document.getElementById('patientField');
    const anonymousFields = document.getElementById('anonymousFields');
    
    function toggleAnonymousFields() {
        if (anonymousCheckbox.checked) {
            patientField.style.display = 'none';
            anonymousFields.style.display = 'block';
        } else {
            patientField.style.display = 'block';
            anonymousFields.style.display = 'none';
        }
    }
    
    // Initial state
    toggleAnonymousFields();
    
    // Listen for changes
    anonymousCheckbox.addEventListener('change', toggleAnonymousFields);
    
    // Rating stars interaction
    const ratingLabels = document.querySelectorAll('.rating-input label');
    ratingLabels.forEach(label => {
        label.addEventListener('mouseenter', function() {
            const rating = this.previousElementSibling.value;
            highlightStars(rating);
        });
    });
    
    document.querySelector('.rating-input').addEventListener('mouseleave', function() {
        const checkedInput = document.querySelector('.rating-input input:checked');
        if (checkedInput) {
            highlightStars(checkedInput.value);
        } else {
            resetStars();
        }
    });
    
    function highlightStars(rating) {
        ratingLabels.forEach((label, index) => {
            if (5 - index <= rating) {
                label.style.color = '#ffc107';
            } else {
                label.style.color = '#ddd';
            }
        });
    }
    
    function resetStars() {
        ratingLabels.forEach(label => {
            label.style.color = '#ddd';
        });
    }
    
    // Form validation
    const form = document.getElementById('feedbackForm');
    form.addEventListener('submit', function(e) {
        const isAnonymous = anonymousCheckbox.checked;
        const patient = document.getElementById('{{ form.patient.id_for_label }}').value;
        const contactName = document.getElementById('{{ form.contact_name.id_for_label }}').value;
        
        if (!isAnonymous && !patient) {
            e.preventDefault();
            alert('Please select a patient or mark as anonymous.');
            return false;
        }
        
        if (isAnonymous && !contactName) {
            e.preventDefault();
            alert('Please enter a contact name for anonymous feedback.');
            return false;
        }
    });
});
</script>
{% endblock %}
