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

{% block title %}{{ phase_label }} - {{ project.name }} - PX360{% endblock %}

{% block extra_css %}
<style>
    .status-active { background-color: #e0f2fe; color: #075985; }
    .status-pending { background-color: #fef9c3; color: #854d0e; }
    .status-completed { background-color: #dcfce7; color: #166534; }
    .status-cancelled { background-color: #fee2e2; color: #991b1b; }
    .task-status-pending { background-color: #f1f5f9; color: #475569; }
    .task-status-in_progress { background-color: #e0f2fe; color: #075985; }
    .task-status-completed { background-color: #dcfce7; color: #166534; }
    .task-status-blocked { background-color: #fee2e2; color: #991b1b; }
</style>
{% endblock %}

{% block content %}
<!-- Breadcrumb -->
<div class="mb-6">
    <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>
</div>

<!-- Header -->
<header class="mb-6">
    <div class="flex justify-between items-start">
        <div>
            <div class="flex items-center gap-3 mb-2">
                <div class="w-12 h-12 rounded-xl flex items-center justify-center
                    {% if pdca_phase.status == 'completed' %}bg-green-200 text-green-700
                    {% elif pdca_phase.status == 'active' %}bg-blue-200 text-blue-700
                    {% else %}bg-slate-200 text-slate-500{% endif %}">
                    {% if phase_key == 'plan' %}<i data-lucide="lightbulb" class="w-6 h-6"></i>
                    {% elif phase_key == 'do' %}<i data-lucide="play" class="w-6 h-6"></i>
                    {% elif phase_key == 'check' %}<i data-lucide="search" class="w-6 h-6"></i>
                    {% elif phase_key == 'act' %}<i data-lucide="rocket" class="w-6 h-6"></i>
                    {% endif %}
                </div>
                <div>
                    <h1 class="text-2xl font-bold text-navy">{{ pdca_phase.title|default:phase_label }}</h1>
                    <p class="text-sm text-slate">{{ project.name }}</p>
                </div>
            </div>
        </div>
        <div class="flex items-center gap-3">
            {% if can_edit %}
            <a href="{% url 'projects:pdca_phase_edit' pk=project.pk phase=phase_key %}" class="bg-white text-blue border-2 border-blue/30 px-4 py-2 rounded-xl text-sm font-bold hover:bg-blue hover:text-white flex items-center gap-2 transition">
                <i data-lucide="pencil" class="w-4 h-4"></i> {% trans "Edit Phase" %}
            </a>
            <a href="{% url 'projects:phase_task_create' project_pk=project.pk phase=phase_key %}" class="bg-navy text-white px-4 py-2 rounded-xl text-sm font-bold hover:bg-blue flex items-center gap-2 transition">
                <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Add Task" %}
            </a>
            {% endif %}
        </div>
    </div>
</header>

<div class="grid grid-cols-3 gap-6">
    <!-- Main Content -->
    <div class="col-span-2 space-y-6">
        <!-- Phase Info -->
        <div class="bg-white rounded-2xl border shadow-sm overflow-hidden">
            <div class="px-6 py-4 border-b">
                <h2 class="text-sm font-bold text-navy flex items-center gap-2">
                    <i data-lucide="info" class="w-4 h-4"></i>
                    {% trans "Phase Information" %}
                </h2>
            </div>
            <div class="p-6 space-y-4">
                <div class="grid grid-cols-3 gap-4">
                    <div>
                        <p class="text-[10px] font-bold text-slate uppercase tracking-wider mb-1">{% trans "Status" %}</p>
                        <span class="status-{{ pdca_phase.status }} px-2.5 py-1 rounded-full text-[10px] font-bold uppercase">
                            {{ pdca_phase.get_status_display }}
                        </span>
                    </div>
                    <div>
                        <p class="text-[10px] font-bold text-slate uppercase tracking-wider mb-1">{% trans "Owner" %}</p>
                        <p class="text-sm text-navy font-medium">{{ pdca_phase.owner.get_full_name|default:"—" }}</p>
                    </div>
                    <div>
                        <p class="text-[10px] font-bold text-slate uppercase tracking-wider mb-1">{% trans "Dates" %}</p>
                        <p class="text-sm text-navy font-medium">
                            {% if pdca_phase.start_date %}{{ pdca_phase.start_date|date:"M d, Y" }}{% else %}—{% endif %}
                            <span class="text-slate"> → </span>
                            {% if pdca_phase.due_date %}{{ pdca_phase.due_date|date:"M d, Y" }}{% else %}—{% endif %}
                        </p>
                    </div>
                </div>

                {% if pdca_phase.description %}
                <div>
                    <p class="text-[10px] font-bold text-slate uppercase tracking-wider mb-1">{% trans "Description" %}</p>
                    <p class="text-sm text-slate">{{ pdca_phase.description|linebreaksbr }}</p>
                </div>
                {% endif %}

                {% if pdca_phase.findings %}
                <div>
                    <p class="text-[10px] font-bold text-slate uppercase tracking-wider mb-1">{% trans "Findings" %}</p>
                    <p class="text-sm text-slate">{{ pdca_phase.findings|linebreaksbr }}</p>
                </div>
                {% endif %}
            </div>
        </div>

        <!-- Tasks Section -->
        <div class="bg-white rounded-2xl border shadow-sm overflow-hidden">
            <div class="px-6 py-4 border-b flex justify-between items-center">
                <h2 class="text-sm font-bold text-navy flex items-center gap-2">
                    <i data-lucide="check-square" class="w-4 h-4"></i>
                    {% trans "Phase Tasks" %}
                    <span class="ml-2 bg-slate-200 text-slate px-2 py-0.5 rounded-full text-[10px]">{{ tasks.count }}</span>
                </h2>
                {% if can_edit %}
                <a href="{% url 'projects:phase_task_create' project_pk=project.pk phase=phase_key %}" class="bg-navy text-white px-3 py-1.5 rounded-lg text-xs font-bold flex items-center gap-1 hover:bg-blue transition">
                    <i data-lucide="plus" class="w-3 h-3"></i> {% trans "Add Task" %}
                </a>
                {% endif %}
            </div>
            
            {% if tasks %}
            <div class="divide-y divide-slate-100">
                {% for task in tasks %}
                <div class="px-6 py-4 hover:bg-slate-50 transition-colors">
                    <div class="flex justify-between items-start">
                        <div class="flex-grow">
                            <div class="flex items-center gap-3 mb-1">
                                <form method="post" action="{% url 'projects:phase_task_toggle_status' project_pk=project.pk phase=phase_key task_pk=task.pk %}" class="inline">
                                    {% csrf_token %}
                                    <button type="submit" class="p-0 bg-transparent border-none cursor-pointer">
                                        {% if task.status == 'completed' %}
                                        <i data-lucide="check-square" class="w-5 h-5 text-green-600"></i>
                                        {% else %}
                                        <i data-lucide="square" class="w-5 h-5 text-slate-300"></i>
                                        {% endif %}
                                    </button>
                                </form>
                                <h3 class="font-semibold text-navy {% if task.status == 'completed' %}line-through text-slate{% endif %}">
                                    {{ task.title }}
                                </h3>
                            </div>
                            {% if task.description %}
                            <p class="text-xs text-slate ml-8 mb-2">{{ task.description }}</p>
                            {% endif %}
                            <div class="flex items-center gap-4 ml-8 text-xs text-slate">
                                {% if task.assigned_to %}
                                <span class="flex items-center gap-1">
                                    <i data-lucide="user" class="w-3 h-3"></i>
                                    {{ task.assigned_to.get_full_name }}
                                </span>
                                {% endif %}
                                {% if task.due_date %}
                                <span class="flex items-center gap-1">
                                    <i data-lucide="calendar" class="w-3 h-3"></i>
                                    {% if task.due_date < today and task.status != 'completed' %}
                                    <span class="text-red-600 font-semibold">{{ task.due_date|date:"M d, Y" }}</span>
                                    {% else %}
                                    {{ task.due_date|date:"M d, Y" }}
                                    {% endif %}
                                </span>
                                {% endif %}
                                <span class="task-status-{{ task.status }} px-2 py-0.5 rounded-full text-[10px] font-bold uppercase">
                                    {{ task.get_status_display }}
                                </span>
                            </div>
                        </div>
                        {% if can_edit %}
                        <div class="flex items-center gap-1 ml-4">
                            <a href="{% url 'projects:phase_task_edit' project_pk=project.pk phase=phase_key task_pk=task.pk %}" 
                               class="p-2 text-blue hover:bg-blue/10 rounded-lg transition" title="{% trans 'Edit' %}">
                                <i data-lucide="pencil" class="w-4 h-4"></i>
                            </a>
                            <a href="{% url 'projects:phase_task_delete' project_pk=project.pk phase=phase_key task_pk=task.pk %}" 
                               class="p-2 text-red-600 hover:bg-red-50 rounded-lg transition" title="{% trans 'Delete' %}">
                                <i data-lucide="trash-2" class="w-4 h-4"></i>
                            </a>
                        </div>
                        {% endif %}
                    </div>
                </div>
                {% endfor %}
            </div>
            {% else %}
            <div class="px-6 py-12 text-center">
                <i data-lucide="check-square" class="w-12 h-12 mx-auto text-slate-300 mb-3"></i>
                <p class="text-slate mb-3">{% trans "No tasks in this phase yet" %}</p>
                {% if can_edit %}
                <a href="{% url 'projects:phase_task_create' project_pk=project.pk phase=phase_key %}" class="bg-navy text-white px-4 py-2 rounded-xl text-sm font-bold inline-flex items-center gap-2 hover:bg-blue transition">
                    <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Add First Task" %}
                </a>
                {% endif %}
            </div>
            {% endif %}
        </div>
    </div>

    <!-- Sidebar: Phase Navigation -->
    <div class="col-span-1">
        <div class="bg-white rounded-2xl border shadow-sm overflow-hidden mb-6">
            <div class="px-6 py-4 border-b">
                <h2 class="text-sm font-bold text-navy flex items-center gap-2">
                    <i data-lucide="refresh-cw" class="w-4 h-4"></i>
                    {% trans "PDCA Cycle" %}
                </h2>
            </div>
            <div class="p-2">
                {% for ph_key, ph_label in pdca_phase_choices %}
                <a href="{% url 'projects:pdca_phase_detail' pk=project.pk phase=ph_key %}"
                   class="flex items-center gap-3 px-4 py-3 rounded-xl transition
                   {% if ph_key == phase_key %}bg-navy text-white{% else %}text-slate hover:bg-slate-50{% endif %}">
                    <div class="w-8 h-8 rounded-lg flex items-center justify-center text-sm
                        {% if ph_key == phase_key %}bg-white/20{% else %}
                            {% if ph_key == 'plan' %}bg-yellow-100 text-yellow-700
                            {% elif ph_key == 'do' %}bg-blue-100 text-blue-700
                            {% elif ph_key == 'check' %}bg-purple-100 text-purple-700
                            {% elif ph_key == 'act' %}bg-green-100 text-green-700{% endif %}
                        {% endif %}">
                        {% if ph_key == 'plan' %}<i data-lucide="lightbulb" class="w-4 h-4"></i>
                        {% elif ph_key == 'do' %}<i data-lucide="play" class="w-4 h-4"></i>
                        {% elif ph_key == 'check' %}<i data-lucide="search" class="w-4 h-4"></i>
                        {% elif ph_key == 'act' %}<i data-lucide="rocket" class="w-4 h-4"></i>
                        {% endif %}
                    </div>
                    <span class="text-sm font-medium">{{ ph_label }}</span>
                    {% if ph_key != phase_key %}
                    <span class="ml-auto text-[10px] bg-slate-200 text-slate px-1.5 py-0.5 rounded-full">
                        {{ phase_task_counts|key:ph_key|default:0 }}
                    </span>
                    {% endif %}
                </a>
                {% endfor %}
            </div>
        </div>
    </div>
</div>
{% endblock %}
