{% extends "portal_base.html" %}
{% load static %}

{% block title %}{% if form.instance.pk %}Reply to Message{% else %}Compose Message{% endif %}{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="row">
        <div class="col-12">
            <div class="card">
                <div class="card-header">
                    <h5 class="mb-0">
                        {% if form.instance.pk %}
                            <i class="fas fa-reply"></i> Reply to Message
                        {% else %}
                            <i class="fas fa-envelope"></i> Compose Message
                        {% endif %}
                    </h5>
                </div>
                <div class="card-body">
                    {% if form.instance.parent_message %}
                        <div class="alert alert-info mb-4">
                            <strong>Replying to:</strong> {{ form.instance.parent_message.subject }}
                            <br>
                            <small class="text-muted">
                                From {{ form.instance.parent_message.sender.get_full_name|default:form.instance.parent_message.sender.username }}
                                on {{ form.instance.parent_message.created_at|date:"M d, Y H:i" }}
                            </small>
                            <div class="mt-2">
                                <strong>Original message:</strong>
                                <div class="border-start ps-3 mt-2">
                                    {{ form.instance.parent_message.content|linebreaks }}
                                </div>
                            </div>
                        </div>
                    {% endif %}

                    <form method="post" id="messageForm">
                        {% csrf_token %}

                        <div class="row">
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="{{ form.job.id_for_label }}" class="form-label">
                                        Related Job <span class="text-danger">*</span>
                                    </label>
                                    {{ form.job }}
                                    {% if form.job.errors %}
                                        <div class="text-danger small mt-1">
                                            {{ form.job.errors.0 }}
                                        </div>
                                    {% endif %}
                                    <div class="form-text">
                                        Select a job if this message is related to a specific position
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="{{ form.recipient.id_for_label }}" class="form-label">
                                        Recipient <span class="text-danger">*</span>
                                    </label>
                                    {{ form.recipient }}

                                    {% if form.recipient.errors %}
                                        <div class="text-danger small mt-1">
                                            {{ form.recipient.errors.0 }}
                                        </div>
                                    {% endif %}
                                    <div class="form-text">
                                        Select the user who will receive this message
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="mb-3">
                                    <label for="{{ form.message_type.id_for_label }}" class="form-label">
                                        Message Type <span class="text-danger">*</span>
                                    </label>
                                    {{ form.message_type }}
                                    {% if form.message_type.errors %}
                                        <div class="text-danger small mt-1">
                                            {{ form.message_type.errors.0 }}
                                        </div>
                                    {% endif %}
                                    <div class="form-text">
                                        Select the type of message you're sending
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="row">
                            <div class="col-md-6">
                                <div class="mb-3">
                                    <label for="{{ form.subject.id_for_label }}" class="form-label">
                                        Subject <span class="text-danger">*</span>
                                    </label>
                                    {{ form.subject }}
                                    {% if form.subject.errors %}
                                        <div class="text-danger small mt-1">
                                            {{ form.subject.errors.0 }}
                                        </div>
                                    {% endif %}
                                </div>
                            </div>
                        </div>

                        <div class="mb-3">
                            <label for="{{ form.content.id_for_label }}" class="form-label">
                                Message <span class="text-danger">*</span>
                            </label>
                            {{ form.content }}
                            {% if form.content.errors %}
                                <div class="text-danger small mt-1">
                                    {{ form.content.errors.0 }}
                                </div>
                            {% endif %}
                            <div class="form-text">
                                Write your message here. You can use line breaks and basic formatting.
                            </div>
                        </div>

                        <div class="d-flex justify-content-between">
                            <a href="{% url 'message_list' %}" class="btn btn-secondary">
                                <i class="fas fa-times"></i> Cancel
                            </a>
                            <button type="submit" class="btn btn-main-action">
                                <i class="fas fa-paper-plane"></i>
                                {% if form.instance.pk %}
                                    Send Reply
                                {% else %}
                                    Send Message
                                {% endif %}
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_css %}
<style>
#id_content {
    min-height: 200px;
    resize: vertical;
}

.form-select {
    {% if form.recipient.field.widget.attrs.disabled %}
    background-color: #f8f9fa;
    {% endif %}
}

.border-start {
    border-left: 3px solid #0d6efd;
}

.ps-3 {
    padding-left: 1rem;
}
</style>
{% endblock %}

{% block extra_js %}
<script>
document.addEventListener('DOMContentLoaded', function() {
    // Auto-resize textarea based on content
    const textarea = document.getElementById('id_content');
    if (textarea) {
        textarea.addEventListener('input', function() {
            this.style.height = 'auto';
            this.style.height = (this.scrollHeight) + 'px';
        });

        // Set initial height
        textarea.style.height = 'auto';
        textarea.style.height = (textarea.scrollHeight) + 'px';
    }

    // Character counter for subject
    const subjectField = document.getElementById('id_subject');
    const maxLength = 200;

    if (subjectField) {
        // Add character counter display
        const counter = document.createElement('small');
        counter.className = 'text-muted';
        counter.style.float = 'right';
        subjectField.parentNode.appendChild(counter);

        function updateCounter() {
            const remaining = maxLength - subjectField.value.length;
            counter.textContent = `${subjectField.value.length}/${maxLength} characters`;
            if (remaining < 20) {
                counter.className = 'text-warning';
            } else {
                counter.className = 'text-muted';
            }
        }

        subjectField.addEventListener('input', updateCounter);
        updateCounter();
    }

    // Form validation before submit
    const form = document.getElementById('messageForm');
    if (form) {
        form.addEventListener('submit', function(e) {
            const content = document.getElementById('id_content').value.trim();
            const subject = document.getElementById('id_subject').value.trim();
            const recipient = document.getElementById('id_recipient').value;

            if (!recipient) {
                e.preventDefault();
                alert('Please select a recipient.');
                return false;
            }

            if (!subject) {
                e.preventDefault();
                alert('Please enter a subject.');
                return false;
            }

            if (!content) {
                e.preventDefault();
                alert('Please enter a message.');
                return false;
            }
        });
    }
});
</script>
{% endblock %}
