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

{% block title %}{% if action %}{% trans "Edit Action Plan" %}{% else %}{% trans "Create Action Plan" %}{% endif %} - PX360{% endblock %}

{% block content %}
<div class="max-w-3xl mx-auto">
    <div class="mb-8">
        <h1 class="text-3xl font-bold text-navy mb-2">
            {% if action %}{% trans "Edit Action Plan" %}{% else %}{% trans "Create Action Plan" %}{% endif %}
        </h1>
        <p class="text-slate">
            {% if action %}{% trans "Update action plan details" %}{% else %}{% trans "Create a new improvement action plan" %}{% endif %}
        </p>
    </div>

    <div class="bg-white rounded-2xl shadow-sm border border-slate-100 overflow-hidden">
        <form method="post">
            {% csrf_token %}
            
            <div class="p-6 space-y-6">
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="title" class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Title" %} <span class="text-navy">*</span></label>
                        <input type="text" class="w-full px-4 py-3 border border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-navy focus:border-transparent transition" id="title" name="title" value="{{ action.title|default:'' }}" required placeholder="{% trans 'Enter action plan title' %}">
                    </div>
                    
                    <div>
                        <label for="priority" class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Priority" %} <span class="text-navy">*</span></label>
                        <select class="w-full px-4 py-3 border border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-navy focus:border-transparent transition" id="priority" name="priority" required>
                            <option value="low" {% if action.priority == 'low' or not action %}selected{% endif %}>{% trans "Low" %}</option>
                            <option value="medium" {% if action.priority == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                            <option value="high" {% if action.priority == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                        </select>
                    </div>
                </div>
                
                <div>
                    <label for="description" class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Description" %} <span class="text-navy">*</span></label>
                    <textarea class="w-full px-4 py-3 border border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-navy focus:border-transparent transition" id="description" name="description" rows="4" required placeholder="{% trans 'Describe the action plan...' %}">{{ action.description|default:'' }}</textarea>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="assigned_to" class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Assigned To" %}</label>
                        <select class="w-full px-4 py-3 border border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-navy focus:border-transparent transition" id="assigned_to" name="assigned_to">
                            <option value="">{% trans "Select User" %}</option>
                            {% for user in users %}
                            <option value="{{ user.id }}" {% if action.assigned_to_id == user.id %}selected{% endif %}>{{ user.get_full_name|default:user.username }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div>
                        <label for="due_date" class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Due Date" %}</label>
                        <input type="date" class="w-full px-4 py-3 border border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-navy focus:border-transparent transition" id="due_date" name="due_date" value="{{ action.due_date|date:'Y-m-d'|default:'' }}">
                    </div>
                </div>
                
                <div class="grid grid-cols-1 md:grid-cols-2 gap-6">
                    <div>
                        <label for="related_complaint" class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Related Complaint" %}</label>
                        <select class="w-full px-4 py-3 border border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-navy focus:border-transparent transition" id="related_complaint" name="related_complaint">
                            <option value="">{% trans "Select Complaint" %}</option>
                            {% for complaint in complaints %}
                            <option value="{{ complaint.id }}" {% if action.related_complaint_id == complaint.id %}selected{% endif %}>{{ complaint.ticket_number }} - {{ complaint.category.name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div>
                        <label for="related_survey" class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Related Survey" %}</label>
                        <select class="w-full px-4 py-3 border border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-navy focus:border-transparent transition" id="related_survey" name="related_survey">
                            <option value="">{% trans "Select Survey" %}</option>
                            {% for survey in surveys %}
                            <option value="{{ survey.id }}" {% if action.related_survey_id == survey.id %}selected{% endif %}>{{ survey.patient_name }}</option>
                            {% endfor %}
                        </select>
                    </div>
                </div>
                
                <div>
                    <label for="status" class="block text-sm font-semibold text-slate-700 mb-2">{% trans "Status" %}</label>
                    <select class="w-full px-4 py-3 border border-slate-200 rounded-xl text-navy focus:ring-2 focus:ring-navy focus:border-transparent transition" id="status" name="status">
                        <option value="pending" {% if action.status == 'pending' or not action %}selected{% endif %}>{% trans "Pending" %}</option>
                        <option value="in_progress" {% if action.status == 'in_progress' %}selected{% endif %}>{% trans "In Progress" %}</option>
                        <option value="completed" {% if action.status == 'completed' %}selected{% endif %}>{% trans "Completed" %}</option>
                        <option value="cancelled" {% if action.status == 'cancelled' %}selected{% endif %}>{% trans "Cancelled" %}</option>
                    </select>
                </div>
            </div>
            
            <div class="px-6 py-4 bg-slate-50 border-t border-slate-100 flex justify-end gap-3">
                <a href="{% url 'actions:action_list' %}" class="px-6 py-3 bg-slate-200 text-slate-700 rounded-xl font-semibold hover:bg-slate-300 transition">
                    {% trans "Cancel" %}
                </a>
                <button type="submit" class="px-6 py-3 bg-gradient-to-r from-navy to-blue text-white rounded-xl font-semibold hover:opacity-90 transition">
                    {% trans "Save" %}
                </button>
            </div>
        </form>
    </div>
</div>
{% endblock %}