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

{% block title %}{% if is_create %}{% trans "Add Task" %}{% else %}{% trans "Edit Task" %}{% 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;
}
</style>
{% endblock %}

{% block content %}
    <!-- Back Button -->
    <div class="mb-6">
        {% if focus_phase %}
        <a href="{% url 'projects:focus_phase_detail' pk=project.pk phase=focus_phase.phase %}" class="inline-flex items-center gap-2 text-slate hover:text-navy transition">
            <i data-lucide="arrow-left" class="w-4 h-4"></i>
            <span class="text-sm font-medium">{% trans "Back to Phase" %}</span>
        </a>
        {% elif pdca_phase %}
        <a href="{% url 'projects:pdca_phase_detail' pk=project.pk phase=pdca_phase.phase %}" class="inline-flex items-center gap-2 text-slate hover:text-navy transition">
            <i data-lucide="arrow-left" class="w-4 h-4"></i>
            <span class="text-sm font-medium">{% trans "Back to Phase" %}</span>
        </a>
        {% else %}
        <a href="{% url 'projects:project_detail' pk=project.pk %}" class="inline-flex items-center gap-2 text-slate hover:text-navy transition">
            <i data-lucide="arrow-left" class="w-4 h-4"></i>
            <span class="text-sm font-medium">{% trans "Back to Project" %}</span>
        </a>
        {% endif %}
    </div>

    <!-- Header -->
    <div class="page-header-gradient">
        <div class="flex justify-between items-start">
            <div>
                <h1 class="text-2xl font-bold flex items-center gap-3">
                    <i data-lucide="check-square" class="w-6 h-6"></i>
                    {% if is_create %}{% trans "Add Task" %}{% else %}{% trans "Edit Task" %}{% endif %}
                </h1>
                <p class="text-sm mt-1 opacity-90">
                    {% if is_create %}
                        {% trans "Create a new task for" %} <strong>{{ project.name }}</strong>
                        {% if focus_phase %} — {{ focus_phase.get_phase_display }} {% trans "phase" %}
                        {% elif pdca_phase %} — {{ pdca_phase.get_phase_display }} {% trans "phase" %}{% endif %}
                    {% else %}
                        {% trans "Update task details" %}
                    {% endif %}
                </p>
            </div>
        </div>
    </div>

<!-- Form Section -->
<div class="form-section max-w-2xl">
    <h2 class="text-sm font-bold text-navy mb-4">{% trans "Task Information" %}</h2>
    
    <form method="post" novalidate>
        {% csrf_token %}
        
        <!-- Title -->
        <div>
            <label for="{{ form.title.id_for_label }}" class="form-label">
                {% trans "Task Title" %} <span class="text-red-500">*</span>
            </label>
            {{ form.title }}
            {% if form.title.errors %}
            <p class="text-red-500 text-xs mt-1">{{ form.title.errors.0 }}</p>
            {% endif %}
        </div>

        <!-- Description -->
        <div class="mt-6">
            <label for="{{ form.description.id_for_label }}" class="form-label">
                {% trans "Description" %}
            </label>
            {{ form.description }}
            {% if form.description.errors %}
            <p class="text-red-500 text-xs mt-1">{{ form.description.errors.0 }}</p>
            {% endif %}
        </div>

        <div class="grid grid-cols-2 gap-6 mt-6">
            <!-- Status -->
            <div>
                <label for="{{ form.status.id_for_label }}" class="form-label">
                    {% trans "Status" %} <span class="text-red-500">*</span>
                </label>
                {{ form.status }}
                {% if form.status.errors %}
                <p class="text-red-500 text-xs mt-1">{{ form.status.errors.0 }}</p>
                {% endif %}
            </div>
            
            <!-- Assigned To -->
            <div>
                <label for="{{ form.assigned_to.id_for_label }}" class="form-label">
                    {% trans "Assigned To" %}
                </label>
                {{ form.assigned_to }}
                {% if form.assigned_to.errors %}
                <p class="text-red-500 text-xs mt-1">{{ form.assigned_to.errors.0 }}</p>
                {% endif %}
            </div>
        </div>

        <!-- PDCA Phase -->
        {% if form.pdca_phase.field.widget.__class__.__name__ != 'HiddenInput' %}
        <div class="mt-6">
            <label for="{{ form.pdca_phase.id_for_label }}" class="form-label">
                {% trans "PDCA Phase" %}
            </label>
            {{ form.pdca_phase }}
            {% if form.pdca_phase.errors %}
            <p class="text-red-500 text-xs mt-1">{{ form.pdca_phase.errors.0 }}</p>
            {% endif %}
        </div>
        {% else %}
            {{ form.pdca_phase }}
        {% endif %}

        <!-- FOCUS Phase -->
        {% if form.focus_phase.field.widget.__class__.__name__ != 'HiddenInput' %}
        <div class="mt-6">
            <label for="{{ form.focus_phase.id_for_label }}" class="form-label">
                {% trans "FOCUS Phase" %}
            </label>
            {{ form.focus_phase }}
            {% if form.focus_phase.errors %}
            <p class="text-red-500 text-xs mt-1">{{ form.focus_phase.errors.0 }}</p>
            {% endif %}
        </div>
        {% else %}
            {{ form.focus_phase }}
        {% endif %}

        <div class="mt-6">
            <label for="{{ form.due_date.id_for_label }}" class="form-label">
                {% trans "Due Date" %}
            </label>
            {{ form.due_date }}
            {% if form.due_date.errors %}
            <p class="text-red-500 text-xs mt-1">{{ form.due_date.errors.0 }}</p>
            {% endif %}
        </div>

        <!-- Submit Buttons -->
        <div class="flex items-center gap-3 mt-8 pt-6 border-t">
            <button type="submit" class="hh-btn hh-btn-primary">
                <i data-lucide="save" class="w-4 h-4"></i>
                {% if is_create %}{% trans "Add Task" %}{% else %}{% trans "Save Changes" %}{% endif %}
            </button>
            {% if focus_phase %}
            <a href="{% url 'projects:focus_phase_detail' pk=project.pk phase=focus_phase.phase %}" class="hh-btn hh-btn-secondary">
                <i data-lucide="x" class="w-4 h-4"></i>
                {% trans "Cancel" %}
            </a>
            {% elif pdca_phase %}
            <a href="{% url 'projects:pdca_phase_detail' pk=project.pk phase=pdca_phase.phase %}" class="hh-btn hh-btn-secondary">
                <i data-lucide="x" class="w-4 h-4"></i>
                {% trans "Cancel" %}
            </a>
            {% else %}
            <a href="{% url 'projects:project_detail' pk=project.pk %}" class="hh-btn hh-btn-secondary">
                <i data-lucide="x" class="w-4 h-4"></i>
                {% trans "Cancel" %}
            </a>
            {% endif %}
        </div>
    </form>
</div>
{% endblock %}
