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

{% block title %}{{ 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; }
    
    /* Modal styles */
    .modal-backdrop {
        background-color: rgba(0, 0, 0, 0.5);
        backdrop-filter: blur(4px);
    }
    
    /* Table styles */
    .task-table {
        width: 100%;
        border-collapse: separate;
        border-spacing: 0;
    }
    
    .task-table th {
        text-align: left;
        padding: 0.5rem 0.75rem;
        font-size: 0.65rem;
        font-weight: 700;
        text-transform: uppercase;
        letter-spacing: 0.05em;
        color: #64748b;
        background-color: #f8fafc;
        border-bottom: 1px solid #e2e8f0;
    }
    
    .task-table td {
        padding: 0.625rem 0.75rem;
        border-bottom: 1px solid #f1f5f9;
        vertical-align: middle;
    }
    
    .task-table tr:hover td {
        background-color: #f8fafc;
    }
    
    .task-table tr:last-child td {
        border-bottom: none;
    }
    
    /* Scrollbar styling */
    .task-scroll::-webkit-scrollbar {
        width: 4px;
    }
    .task-scroll::-webkit-scrollbar-track {
        background: transparent;
    }
    .task-scroll::-webkit-scrollbar-thumb {
        background-color: #cbd5e1;
        border-radius: 20px;
    }
    
    /* Phase header */
    .phase-header-compact {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        padding: 0.5rem 0.75rem;
        background-color: #f8fafc;
        border-bottom: 2px solid #e2e8f0;
    }
</style>
{% endblock %}

{% block content %}
<!-- Back Button -->
<div class="mb-4">
    <a href="{% url 'projects:project_list' %}" 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 Projects" %}</span>
    </a>
</div>

<!-- Header -->
<header class="mb-6">
    <div class="flex flex-col lg:flex-row justify-between items-start gap-4">
        <div class="flex-grow">
            <h1 class="text-2xl font-bold text-navy">{{ project.name }}</h1>
            {% if project.description %}
            <p class="text-sm text-slate mt-1 max-w-2xl">{{ project.description|linebreaksbr }}</p>
            {% endif %}
        </div>
        <div class="flex items-center gap-3 flex-wrap">
            {% if can_edit %}
            <a href="{% url 'projects:project_edit' pk=project.pk %}" 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" %}
            </a>
            {% endif %}
            {% if user.is_px_admin or user.is_hospital_admin %}
            <a href="{% url 'projects:project_export_excel' pk=project.pk %}" class="bg-white text-green-600 border-2 border-green-200 px-4 py-2 rounded-xl text-sm font-bold hover:bg-green-600 hover:text-white flex items-center gap-2 transition">
                <i data-lucide="file-spreadsheet" class="w-4 h-4"></i> {% trans "Export Excel" %}
            </a>
            <a href="{% url 'projects:project_save_as_template' pk=project.pk %}" class="bg-white text-purple-600 border-2 border-purple-200 px-4 py-2 rounded-xl text-sm font-bold hover:bg-purple-600 hover:text-white flex items-center gap-2 transition">
                <i data-lucide="copy" class="w-4 h-4"></i> {% trans "Save as Template" %}
            </a>
            <a href="{% url 'projects:project_delete' pk=project.pk %}" class="bg-white text-red-600 border-2 border-red-200 px-4 py-2 rounded-xl text-sm font-bold hover:bg-red-600 hover:text-white flex items-center gap-2 transition">
                <i data-lucide="trash-2" class="w-4 h-4"></i> {% trans "Delete" %}
            </a>
            {% endif %}
        </div>
    </div>
</header>

<div class="space-y-6">
    
    <!-- PDCA Cycle 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="refresh-cw" class="w-4 h-4"></i>
                {% trans "PDCA Cycle" %}
            </h2>
            <span class="text-xs text-slate">{% trans "Plan - Do - Check - Act" %}</span>
        </div>
        <div class="p-0">
            {% for phase_key, phase_label in pdca_phase_choices %}
            {% with phase_obj=pdca_phases|key:phase_key tasks=pdca_tasks|key:phase_key %}
            <div class="border-b last:border-b-0">
                <!-- Phase Header -->
                <div class="phase-header-compact">
                    <div class="w-8 h-8 rounded-lg flex items-center justify-center text-sm
                        {% if phase_obj %}
                            {% if phase_obj.status == 'completed' %}bg-green-100 text-green-700
                            {% elif phase_obj.status == 'active' %}bg-blue-100 text-blue-700
                            {% else %}bg-slate-100 text-slate-600{% endif %}
                        {% else %}bg-slate-100 text-slate-400{% endif %}">
                        {% if phase_key == 'plan' %}<i data-lucide="lightbulb" class="w-4 h-4"></i>
                        {% elif phase_key == 'do' %}<i data-lucide="play" class="w-4 h-4"></i>
                        {% elif phase_key == 'check' %}<i data-lucide="search" class="w-4 h-4"></i>
                        {% elif phase_key == 'act' %}<i data-lucide="rocket" class="w-4 h-4"></i>
                        {% endif %}
                    </div>
                    <div class="flex-grow">
                        <h3 class="font-bold text-sm text-navy">{{ phase_label }}</h3>
                        {% if phase_obj and phase_obj.owner %}
                        <p class="text-[10px] text-slate-500">{{ phase_obj.owner.get_full_name }}</p>
                        {% endif %}
                    </div>
                    {% if phase_obj %}
                    <span class="inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-bold uppercase
                        {% if phase_obj.status == 'completed' %}bg-green-100 text-green-700
                        {% elif phase_obj.status == 'active' %}bg-blue-100 text-blue-700
                        {% elif phase_obj.status == 'pending' %}bg-yellow-100 text-yellow-700
                        {% else %}bg-slate-100 text-slate-600{% endif %}">
                        {{ phase_obj.get_status_display }}
                    </span>
                    <span class="text-[10px] text-slate-500 font-medium ml-2">{{ tasks.count }} {% trans "tasks" %}</span>
                    {% if can_edit %}
                    <button hx-get="{% url 'projects:htmx_phase_edit_form' project_pk=project.pk phase_type='pdca' phase=phase_key %}"
                            hx-target="#phaseModalContent"
                            onclick="openPhaseModal('{{ phase_label }}')"
                            class="p-1.5 text-slate-400 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition ml-2"
                            title="{% trans 'Edit Phase' %}">
                        <i data-lucide="pencil" class="w-3.5 h-3.5"></i>
                    </button>
                    {% endif %}
                    {% else %}
                    <span class="text-[10px] text-slate-400">{% trans "Not set up" %}</span>
                    {% endif %}
                </div>
                
                <!-- Tasks Table -->
                {% if tasks %}
                <div class="overflow-x-auto">
                    <table class="task-table">
                        <thead>
                            <tr>
                                <th class="w-10"></th>
                                <th class="w-10"></th>
                                <th>{% trans "Task" %}</th>
                                <th class="w-32">{% trans "Assigned To" %}</th>
                                <th class="w-28">{% trans "Due Date" %}</th>
                                <th class="w-24">{% trans "Status" %}</th>
                                {% if can_edit %}
                                <th class="w-20">{% trans "Actions" %}</th>
                                {% endif %}
                            </tr>
                        </thead>
                        <tbody>
                            {% for task in tasks %}
                            {% include "projects/partials/task_row.html" with task=task project=project can_edit=can_edit today=today %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <div class="px-6 py-4 text-center text-slate-400 text-sm">
                    {% trans "No tasks in this phase" %}
                </div>
                {% endif %}
                
                {% if can_edit %}
                <div class="px-6 py-3 bg-slate-50 border-t border-slate-100">
                    <button hx-get="{% url 'projects:htmx_task_create' project_pk=project.pk phase_type='pdca' phase=phase_key %}"
                            hx-target="#taskModalContent"
                            onclick="openTaskModal('{% trans "Add Task" %}')"
                            class="text-sm font-semibold text-blue hover:text-blue-700 flex items-center gap-2 transition">
                        <i data-lucide="plus" class="w-4 h-4"></i>
                        {% trans "Add Task" %}
                    </button>
                </div>
                {% endif %}
            </div>
            {% endwith %}
            {% endfor %}
        </div>
    </div>

    {% if project.focus_enabled %}
    <!-- FOCUS Methodology 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="target" class="w-4 h-4"></i>
                {% trans "FOCUS Methodology" %}
            </h2>
            <span class="text-[10px] text-slate font-medium">{% trans "Find, Organize, Clarify, Understand, Select" %}</span>
        </div>
        <div class="p-0">
            {% for phase_key, phase_label in focus_phase_choices %}
            {% with phase_obj=focus_phases|key:phase_key tasks=focus_tasks|key:phase_key %}
            <div class="border-b last:border-b-0">
                <!-- Phase Header -->
                <div class="phase-header-compact">
                    <div class="w-8 h-8 rounded-lg flex items-center justify-center text-sm
                        {% if phase_obj %}
                            {% if phase_obj.status == 'completed' %}bg-teal-100 text-teal-700
                            {% elif phase_obj.status == 'active' %}bg-cyan-100 text-cyan-700
                            {% else %}bg-slate-100 text-slate-600{% endif %}
                        {% else %}bg-slate-100 text-slate-400{% endif %}">
                        {% if phase_key == 'find' %}<i data-lucide="search" class="w-4 h-4"></i>
                        {% elif phase_key == 'organize' %}<i data-lucide="folder" class="w-4 h-4"></i>
                        {% elif phase_key == 'clarify' %}<i data-lucide="eye" class="w-4 h-4"></i>
                        {% elif phase_key == 'understand' %}<i data-lucide="brain" class="w-4 h-4"></i>
                        {% elif phase_key == 'select' %}<i data-lucide="check-circle" class="w-4 h-4"></i>
                        {% endif %}
                    </div>
                    <div class="flex-grow">
                        <h3 class="font-bold text-sm text-navy">{{ phase_label }}</h3>
                        {% if phase_obj and phase_obj.owner %}
                        <p class="text-[10px] text-slate-500">{{ phase_obj.owner.get_full_name }}</p>
                        {% endif %}
                    </div>
                    {% if phase_obj %}
                    <span class="inline-flex items-center px-2 py-0.5 rounded-full text-[10px] font-bold uppercase
                        {% if phase_obj.status == 'completed' %}bg-teal-100 text-teal-700
                        {% elif phase_obj.status == 'active' %}bg-cyan-100 text-cyan-700
                        {% elif phase_obj.status == 'pending' %}bg-yellow-100 text-yellow-700
                        {% else %}bg-slate-100 text-slate-600{% endif %}">
                        {{ phase_obj.get_status_display }}
                    </span>
                    <span class="text-[10px] text-slate-500 font-medium ml-2">{{ tasks.count }} {% trans "tasks" %}</span>
                    {% if can_edit %}
                    <button hx-get="{% url 'projects:htmx_phase_edit_form' project_pk=project.pk phase_type='focus' phase=phase_key %}"
                            hx-target="#phaseModalContent"
                            onclick="openPhaseModal('{{ phase_label }}')"
                            class="p-1.5 text-slate-400 hover:text-blue-600 hover:bg-blue-50 rounded-lg transition ml-2"
                            title="{% trans 'Edit Phase' %}">
                        <i data-lucide="pencil" class="w-3.5 h-3.5"></i>
                    </button>
                    {% endif %}
                    {% else %}
                    <span class="text-[10px] text-slate-400">{% trans "Not set up" %}</span>
                    {% endif %}
                </div>
                
                <!-- Tasks Table -->
                {% if tasks %}
                <div class="overflow-x-auto">
                    <table class="task-table">
                        <thead>
                            <tr>
                                <th class="w-10"></th>
                                <th class="w-10"></th>
                                <th>{% trans "Task" %}</th>
                                <th class="w-32">{% trans "Assigned To" %}</th>
                                <th class="w-28">{% trans "Due Date" %}</th>
                                <th class="w-24">{% trans "Status" %}</th>
                                {% if can_edit %}
                                <th class="w-20">{% trans "Actions" %}</th>
                                {% endif %}
                            </tr>
                        </thead>
                        <tbody>
                            {% for task in tasks %}
                            {% include "projects/partials/task_row.html" with task=task project=project can_edit=can_edit today=today %}
                            {% endfor %}
                        </tbody>
                    </table>
                </div>
                {% else %}
                <div class="px-6 py-4 text-center text-slate-400 text-sm">
                    {% trans "No tasks in this phase" %}
                </div>
                {% endif %}
                
                {% if can_edit %}
                <div class="px-6 py-3 bg-slate-50 border-t border-slate-100">
                    <button hx-get="{% url 'projects:htmx_task_create' project_pk=project.pk phase_type='focus' phase=phase_key %}"
                            hx-target="#taskModalContent"
                            onclick="openTaskModal('{% trans "Add Task" %}')"
                            class="text-sm font-semibold text-blue hover:text-blue-700 flex items-center gap-2 transition">
                        <i data-lucide="plus" class="w-4 h-4"></i>
                        {% trans "Add Task" %}
                    </button>
                </div>
                {% endif %}
            </div>
            {% endwith %}
            {% endfor %}
        </div>
    </div>
    {% endif %}

    <!-- Outcome Section -->
    {% if project.outcome_description %}
    <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="trophy" class="w-4 h-4"></i>
                {% trans "Outcomes" %}
            </h2>
        </div>
        <div class="px-6 py-4">
            <p class="text-sm text-slate">{{ project.outcome_description|linebreaksbr }}</p>
        </div>
    </div>
    {% endif %}
</div>

<!-- Task Modal -->
<div id="taskModal" class="fixed inset-0 modal-backdrop z-50 hidden flex items-center justify-center p-4">
    <div class="bg-white rounded-2xl shadow-2xl max-w-2xl w-full max-h-[90vh] overflow-hidden">
        <div class="px-6 py-4 border-b border-slate-200 flex justify-between items-center">
            <h3 id="taskModalTitle" class="text-lg font-bold text-navy">{% trans "Add Task" %}</h3>
            <button type="button" onclick="closeTaskModal()" class="text-slate-400 hover:text-slate-600 transition">
                <i data-lucide="x" class="w-5 h-5"></i>
            </button>
        </div>
        <div id="taskModalContent" class="p-6 overflow-y-auto" style="max-height: calc(90vh - 80px);">
            <!-- Form loaded via HTMX -->
        </div>
    </div>
</div>

<!-- Phase Modal -->
<div id="phaseModal" class="fixed inset-0 modal-backdrop z-50 hidden flex items-center justify-center p-4">
    <div class="bg-white rounded-2xl shadow-2xl max-w-2xl w-full max-h-[90vh] overflow-hidden">
        <div class="px-6 py-4 border-b border-slate-200 flex justify-between items-center">
            <h3 id="phaseModalTitle" class="text-lg font-bold text-navy">{% trans "Edit Phase" %}</h3>
            <button type="button" onclick="closePhaseModal()" class="text-slate-400 hover:text-slate-600 transition">
                <i data-lucide="x" class="w-5 h-5"></i>
            </button>
        </div>
        <div id="phaseModalContent" class="p-6 overflow-y-auto" style="max-height: calc(90vh - 80px);">
            <!-- Form loaded via HTMX -->
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="{% static 'js/project-board.js' %}"></script>
<script>
    // Initialize Lucide icons
    document.addEventListener('DOMContentLoaded', function() {
        if (typeof lucide !== 'undefined') lucide.createIcons();
    });
    
    // Re-initialize icons after HTMX swaps
    document.body.addEventListener('htmx:afterSwap', function(evt) {
        if (typeof lucide !== 'undefined') lucide.createIcons();
    });
    
    // Modal functions
    function openTaskModal(title) {
        document.getElementById('taskModalTitle').textContent = title;
        document.getElementById('taskModal').classList.remove('hidden');
        document.body.style.overflow = 'hidden';
    }
    
    function closeTaskModal() {
        document.getElementById('taskModal').classList.add('hidden');
        document.getElementById('taskModalContent').innerHTML = '';
        document.body.style.overflow = '';
    }
    
    function openPhaseModal(title) {
        document.getElementById('phaseModalTitle').textContent = title;
        document.getElementById('phaseModal').classList.remove('hidden');
        document.body.style.overflow = 'hidden';
    }
    
    function closePhaseModal() {
        document.getElementById('phaseModal').classList.add('hidden');
        document.getElementById('phaseModalContent').innerHTML = '';
        document.body.style.overflow = '';
    }
    
    // Close modals on backdrop click
    document.getElementById('taskModal').addEventListener('click', function(e) {
        if (e.target === this) closeTaskModal();
    });
    document.getElementById('phaseModal').addEventListener('click', function(e) {
        if (e.target === this) closePhaseModal();
    });
    
    // Close modals on Escape key
    document.addEventListener('keydown', function(e) {
        if (e.key === 'Escape') {
            closeTaskModal();
            closePhaseModal();
        }
    });
    
    // Refresh phase column after task operations
    function refreshPhaseColumn(phaseType, phaseKey) {
        const columnId = `phase-column-${phaseType}-${phaseKey}`;
        const column = document.getElementById(columnId);
        if (column) {
            const url = phaseType === 'pdca' 
                ? `{% url 'projects:htmx_pdca_column' project_pk=project.pk phase='__phase__' %}`.replace('__phase__', phaseKey)
                : `{% url 'projects:htmx_focus_column' project_pk=project.pk phase='__phase__' %}`.replace('__phase__', phaseKey);
            
            htmx.ajax('GET', url, {
                target: column,
                swap: 'outerHTML'
            });
        }
    }
    
    // Handle task deletion to refresh column
    document.body.addEventListener('taskDeleted', function(evt) {
        const detail = evt.detail;
        if (detail) {
            refreshPhaseColumn(detail.phase_type, detail.phase);
        }
    });
</script>
{% endblock %}
