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

{% block title %}{% trans "QI Projects" %} - 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; }
    .filter-btn.active { background-color: #005696; color: white; }
    .filter-btn:not(.active) { background-color: transparent; border: 1px solid #e2e8f0; color: #64748b; }
    .filter-btn:not(.active):hover { background-color: #f8fafc; }

    /* Page Header Gradient */
    .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);
    }

    /* Section Cards */
    .section-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
        overflow: hidden;
        transition: all 0.3s ease;
    }
    .section-card:hover {
        border-color: #005696;
        box-shadow: 0 10px 25px -5px rgba(0, 86, 150, 0.15);
    }

    .section-header {
        padding: 1rem 1.5rem;
        border-bottom: 2px solid #e2e8f0;
        background: linear-gradient(to right, #f8fafc, #f1f5f9);
        display: flex;
        align-items: center;
        gap: 0.75rem;
    }

    .section-icon {
        width: 40px;
        height: 40px;
        border-radius: 0.75rem;
        display: flex;
        align-items: center;
        justify-content: center;
    }
</style>
{% endblock %}

{% block content %}
<!-- Page Header Gradient -->
<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="folder-kanban" class="w-7 h-7"></i>
                {% trans "Quality Improvement Projects" %}
            </h1>
            <p class="text-sm opacity-90 mt-1">{% trans "Track and manage quality improvement initiatives" %}</p>
        </div>
        <div class="flex items-center gap-3">
            <div class="relative group">
                <i data-lucide="search" class="w-4 h-4 absolute left-3 top-1/2 -translate-y-1/2 text-white/70 group-focus-within:text-white"></i>
                <input type="text" id="searchInput" placeholder="{% trans 'Search projects...' %}"
                       class="pl-10 pr-4 py-2.5 bg-white/20 border-transparent border focus:border-white/50 focus:bg-white/30 rounded-xl text-sm outline-none w-64 transition-all placeholder-white/70 text-white"
                       value="{{ filters.search|default:'' }}">
            </div>
            {% if user.is_px_admin or user.is_hospital_admin or user.is_department_manager %}
            <a href="{% url 'projects:template_list' %}" class="bg-white/20 text-white border-2 border-white/30 px-5 py-2.5 rounded-xl text-sm font-bold hover:bg-white hover:text-[#005696] flex items-center gap-2 transition">
                <i data-lucide="copy" class="w-4 h-4"></i> {% trans "Templates" %}
            </a>
            <a href="{% url 'projects:project_create' %}" class="bg-white text-[#005696] px-5 py-2.5 rounded-xl text-sm font-bold shadow-lg hover:bg-white/90 flex items-center gap-2 transition">
                <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Create Project" %}
            </a>
            {% endif %}
        </div>
    </div>
</div>

<!-- Statistics Cards -->
<div class="grid grid-cols-4 gap-6 mb-6">
    <div class="section-card p-4 flex items-center gap-4">
        <div class="section-icon bg-blue/10">
            <i data-lucide="layers" class="text-blue w-5 h-5"></i>
        </div>
        <div>
            <p class="text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Total Projects" %}</p>
            <p class="text-xl font-black text-navy leading-tight">{{ stats.total }}</p>
        </div>
    </div>
    <div class="section-card p-4 flex items-center gap-4">
        <div class="section-icon bg-cyan-50">
            <i data-lucide="play-circle" class="text-cyan-600 w-5 h-5"></i>
        </div>
        <div>
            <p class="text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Active" %}</p>
            <p class="text-xl font-black text-navy leading-tight">{{ stats.active }}</p>
        </div>
    </div>
    <div class="section-card p-4 flex items-center gap-4">
        <div class="section-icon bg-green-50">
            <i data-lucide="check-circle" class="text-green-600 w-5 h-5"></i>
        </div>
        <div>
            <p class="text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Completed" %}</p>
            <p class="text-xl font-black text-navy leading-tight">{{ stats.completed }}</p>
        </div>
    </div>
    <div class="section-card p-4 flex items-center gap-4">
        <div class="section-icon bg-yellow-50">
            <i data-lucide="clock" class="text-yellow-600 w-5 h-5"></i>
        </div>
        <div>
            <p class="text-[10px] font-bold text-slate uppercase tracking-wider">{% trans "Pending" %}</p>
            <p class="text-xl font-black text-navy leading-tight">{{ stats.pending }}</p>
        </div>
    </div>
</div>

<!-- Filters and Projects Section Card -->
<div class="section-card mb-6">
    <!-- Filter Tabs -->
    <div class="section-header border-b-2 border-slate-200 flex items-center justify-between">
        <div class="flex items-center gap-2">
            <div class="section-icon bg-[#005696]">
                <i data-lucide="filter" class="w-5 h-5 text-white"></i>
            </div>
            <h3 class="font-bold text-navy">{% trans "Filters" %}</h3>
        </div>
        <div class="flex items-center gap-3">
            <a href="?" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if not filters.status %}active{% endif %}">
                {% trans "All Projects" %}
            </a>
            <a href="?status=active" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if filters.status == 'active' %}active{% endif %}">
                {% trans "Active" %}
            </a>
            <a href="?status=pending" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if filters.status == 'pending' %}active{% endif %}">
                {% trans "Pending" %}
            </a>
            <a href="?status=completed" class="filter-btn px-4 py-1.5 rounded-full text-xs font-semibold transition {% if filters.status == 'completed' %}active{% endif %}">
                {% trans "Completed" %}
            </a>
            <div class="h-4 w-[1px] bg-slate-200 mx-2"></div>
        </div>
    </div>
    <p class="px-6 py-2 text-[10px] font-bold text-slate uppercase bg-slate-50 border-b-2 border-slate-200">
        {% trans "Showing:" %} <span class="text-navy">{{ page_obj.start_index|default:0 }}-{{ page_obj.end_index|default:0 }} {% trans "of" %} {{ page_obj.paginator.count|default:0 }}</span>
    </p>

    <!-- Projects Table -->
    <div class="overflow-hidden">
        <table class="w-full text-left border-collapse">
            <thead class="bg-slate-50 border-b uppercase text-[10px] font-bold text-slate tracking-wider">
                <tr>
                    <th class="px-6 py-4">{% trans "Project" %}</th>
                    <th class="px-6 py-4">{% trans "Hospital" %}</th>
                    <th class="px-6 py-4">{% trans "Project Lead" %}</th>
                    <th class="px-6 py-4">{% trans "Status" %}</th>
                    <th class="px-6 py-4">{% trans "Tasks" %}</th>
                    <th class="px-6 py-4">{% trans "Timeline" %}</th>
                    <th class="px-6 py-4 text-right">{% trans "Actions" %}</th>
                </tr>
            </thead>
            <tbody class="text-sm divide-y divide-slate-100">
                {% for project in projects %}
                <tr class="hover:bg-light/30 transition-colors cursor-pointer group" onclick="window.location.href='{% url 'projects:project_detail' pk=project.pk %}'">
                    <td class="px-6 py-4">
                        <a href="{% url 'projects:project_detail' pk=project.pk %}" class="font-bold text-navy hover:text-blue" onclick="event.stopPropagation()">
                            {{ project.name }}
                        </a>
                        {% if project.description %}
                        <p class="text-xs text-slate mt-0.5 truncate max-w-xs">{{ project.description|truncatechars:60 }}</p>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        <span class="bg-slate-100 px-2 py-0.5 rounded text-[10px] font-bold text-slate-600">{{ project.hospital.name }}</span>
                    </td>
                    <td class="px-6 py-4 text-slate-600">
                        {% if project.project_lead %}
                        {{ project.project_lead.get_full_name }}
                        {% else %}
                        <span class="text-muted">-</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        {% if project.status == 'active' %}
                        <span class="status-active px-2.5 py-1 rounded-full text-[10px] font-bold uppercase">{{ project.get_status_display }}</span>
                        {% elif project.status == 'pending' %}
                        <span class="status-pending px-2.5 py-1 rounded-full text-[10px] font-bold uppercase">{{ project.get_status_display }}</span>
                        {% elif project.status == 'completed' %}
                        <span class="status-completed px-2.5 py-1 rounded-full text-[10px] font-bold uppercase">{{ project.get_status_display }}</span>
                        {% elif project.status == 'cancelled' %}
                        <span class="status-cancelled px-2.5 py-1 rounded-full text-[10px] font-bold uppercase">{{ project.get_status_display }}</span>
                        {% else %}
                        <span class="bg-slate-100 px-2.5 py-1 rounded-full text-[10px] font-bold uppercase text-slate-600">{{ project.get_status_display }}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4">
                        {% with total=project.tasks.count completed=project.tasks.completed.count %}
                        <div class="flex items-center gap-2">
                            <div class="flex-1 h-1.5 bg-slate-200 rounded-full overflow-hidden max-w-[80px]">
                                {% if total > 0 %}
                                <div class="h-full bg-green-500 rounded-full" style="width: {{ completed|div:total|mul:100|floatformat:0 }}%"></div>
                                {% endif %}
                            </div>
                            <span class="text-xs text-slate">
                                <span class="text-green-600 font-semibold">{{ completed }}</span>/{{ total }}
                                {% if total > 0 %}
                                <span class="text-muted">({{ completed|div:total|mul:100|floatformat:0 }}%)</span>
                                {% endif %}
                            </span>
                        </div>
                        {% endwith %}
                    </td>
                    <td class="px-6 py-4 text-slate-600">
                        {% if project.start_date or project.target_completion_date %}
                        <small>
                            {% if project.start_date %}{{ project.start_date|date:"M d" }}{% else %}-{% endif %}
                            →
                            {% if project.target_completion_date %}{{ project.target_completion_date|date:"M d, Y" }}{% else %}-{% endif %}
                        </small>
                        {% else %}
                        <span class="text-muted">-</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4 text-right">
                        <div class="flex justify-end gap-2 opacity-0 group-hover:opacity-100 transition-opacity">
                            <a href="{% url 'projects:project_detail' pk=project.pk %}" class="p-1.5 text-navy hover:bg-white rounded border border-transparent hover:border-slate-200" title="{% trans 'View' %}" onclick="event.stopPropagation()">
                                <i data-lucide="eye" class="w-4 h-4"></i>
                            </a>
                            {% if user.is_px_admin or user.is_hospital_admin or user.is_department_manager %}
                            <a href="{% url 'projects:project_edit' pk=project.pk %}" class="p-1.5 text-blue hover:bg-white rounded border border-transparent hover:border-slate-200" title="{% trans 'Edit' %}" onclick="event.stopPropagation()">
                                <i data-lucide="pencil" class="w-4 h-4"></i>
                            </a>
                            {% endif %}
                        </div>
                    </td>
                </tr>
                {% empty %}
                <tr>
                    <td colspan="7" class="px-6 py-12 text-center">
                        <i data-lucide="kanban" class="w-12 h-12 mx-auto text-slate-300 mb-3"></i>
                        <p class="text-slate mb-3">{% trans "No projects found" %}</p>
                        {% if user.is_px_admin or user.is_hospital_admin or user.is_department_manager %}
                        <a href="{% url 'projects:project_create' %}" class="bg-[#005696] text-white px-5 py-2.5 rounded-xl text-sm font-bold shadow-lg shadow-[#005696]/20 hover:bg-[#0069a8] inline-flex items-center gap-2 transition">
                            <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Create First Project" %}
                        </a>
                        {% endif %}
                    </td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
    
    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <div class="bg-slate-50 px-8 py-4 flex items-center justify-between border-t">
        <div class="flex items-center gap-4">
            <span class="text-xs text-slate font-medium">
                {% trans "Showing" %} <span class="font-bold text-navy">{{ page_obj.start_index }}-{{ page_obj.end_index }}</span> {% trans "of" %} <span class="font-bold text-navy">{{ page_obj.paginator.count }}</span> {% trans "entries" %}
            </span>
        </div>
        <div class="flex gap-2">
            {% if page_obj.has_previous %}
            <a href="?page={{ page_obj.previous_page_number }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}" 
               class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
                <i data-lucide="chevron-left" class="w-4 h-4 text-slate"></i>
            </a>
            {% else %}
            <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
                <i data-lucide="chevron-left" class="w-4 h-4"></i>
            </span>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <span class="w-8 h-8 flex items-center justify-center rounded-lg bg-[#005696] text-white text-xs font-bold">{{ num }}</span>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <a href="?page={{ num }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}" 
                   class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                    {{ num }}
                </a>
                {% elif num == 1 or num == page_obj.paginator.num_pages %}
                <a href="?page={{ num }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}" 
                   class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 text-xs font-bold text-slate transition">
                    {{ num }}
                </a>
                {% elif num == page_obj.number|add:'-3' or num == page_obj.number|add:'3' %}
                <span class="w-8 h-8 flex items-center justify-center text-xs text-slate">...</span>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <a href="?page={{ page_obj.next_page_number }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.status %}&status={{ filters.status }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}" 
               class="w-8 h-8 flex items-center justify-center rounded-lg border bg-white hover:bg-slate-50 transition">
                <i data-lucide="chevron-right" class="w-4 h-4 text-slate"></i>
            </a>
            {% else %}
            <span class="w-8 h-8 flex items-center justify-center rounded-lg border bg-slate-100 text-slate-300 cursor-not-allowed">
                <i data-lucide="chevron-right" class="w-4 h-4"></i>
            </span>
            {% endif %}
        </div>
    </div>
    {% endif %}
</div>

<script>
// Search functionality
document.getElementById('searchInput')?.addEventListener('keypress', function(e) {
    if (e.key === 'Enter') {
        const value = this.value;
        let url = '?';
        if (value) url += 'search=' + encodeURIComponent(value);
        {% if filters.status %}url += '&status={{ filters.status }}';{% endif %}
        window.location.href = url;
    }
});
</script>
{% endblock %}
