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

{% block title %}{% trans "Comment Action Plans" %} - 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);
    }
    .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;
    }
    .stat-card {
        background: white;
        border-radius: 1rem;
        border: 2px solid #e2e8f0;
        box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
        transition: all 0.3s ease;
    }
    .stat-card:hover {
        transform: translateY(-2px);
        box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);
        border-color: #005696;
    }
</style>
{% endblock %}

{% block content %}
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-2xl font-bold mb-1">{% trans "Steps 3-5 — Comment Action Plans" %}</h1>
            <p class="text-blue-100 text-sm">{% trans "Track action plans derived from patient comments" %}</p>
        </div>
        <a href="{% url 'feedback:export_action_plans' %}" class="inline-flex items-center px-4 py-2.5 bg-white text-navy font-medium rounded-xl hover:bg-blue-50 transition">
            <i data-lucide="download" class="w-4 h-4 me-2"></i>{% trans "Export Action Plans" %}
        </a>
    </div>
</div>

<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon bg-slate-500/10">
            <i data-lucide="sliders-horizontal" class="w-5 h-5 text-slate-600"></i>
        </div>
        <h5 class="text-lg font-semibold text-gray-800">{% trans "Filters" %}</h5>
    </div>
    <div class="p-6">
        <form method="get" class="flex flex-wrap gap-4 items-end">
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Status" %}</label>
                <select name="status" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    <option value="completed" {% if selected_status == 'completed' %}selected{% endif %}>{% trans "Completed" %}</option>
                    <option value="on_process" {% if selected_status == 'on_process' %}selected{% endif %}>{% trans "On Process" %}</option>
                    <option value="pending" {% if selected_status == 'pending' %}selected{% endif %}>{% trans "Pending" %}</option>
                </select>
            </div>
            <div class="flex items-center gap-2">
                <label class="text-xs font-bold text-slate uppercase">{% trans "Year" %}</label>
                <select name="year" class="px-3 py-1.5 bg-white border border-slate-200 rounded-lg text-xs">
                    <option value="">{% trans "All" %}</option>
                    {% for y in years_range %}
                    <option value="{{ y }}" {% if selected_year == y %}selected{% endif %}>{{ y }}</option>
                    {% endfor %}
                </select>
            </div>
            <button type="submit" class="px-4 py-1.5 bg-navy text-white rounded-lg text-xs font-bold">
                <i data-lucide="filter" class="w-3 h-3 inline-block me-1"></i>{% trans "Filter" %}
            </button>
        </form>
    </div>
</div>

{% regroup action_plans by department_label as dept_groups %}
{% for dept, plans in dept_groups %}
<div class="section-card mb-4">
    <div class="section-header">
        <div class="section-icon bg-blue-500/10">
            <i data-lucide="building-2" class="w-5 h-5 text-blue-600"></i>
        </div>
        <h5 class="text-lg font-semibold text-gray-800">{{ dept|default:_("Unassigned") }}</h5>
    </div>
    <div class="p-0">
        <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 "#" %}</th>
                    <th class="px-6 py-4">{% trans "Recommendation" %}</th>
                    <th class="px-6 py-4">{% trans "Frequency" %}</th>
                    <th class="px-6 py-4">{% trans "Status" %}</th>
                    <th class="px-6 py-4">{% trans "Timeframe" %}</th>
                    <th class="px-6 py-4">{% trans "Evidence" %}</th>
                </tr>
            </thead>
            <tbody class="text-sm divide-y divide-slate-100">
                {% for plan in plans %}
                <tr class="hover:bg-slate-50/50 transition-colors">
                    <td class="px-6 py-4 font-semibold text-navy">{{ plan.problem_number|default:"—" }}</td>
                    <td class="px-6 py-4 text-slate-700">{{ plan.recommendation|truncatewords:20 }}</td>
                    <td class="px-6 py-4">
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-blue-100 text-blue-700">{{ plan.frequency }}</span>
                    </td>
                    <td class="px-6 py-4">
                        {% if plan.status == 'completed' %}
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-green-100 text-green-700">{% trans "Completed" %}</span>
                        {% elif plan.status == 'on_process' %}
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-yellow-100 text-yellow-700">{% trans "On Process" %}</span>
                        {% else %}
                        <span class="px-2.5 py-1 rounded-full text-[10px] font-bold uppercase bg-slate-100 text-slate-600">{% trans "Pending" %}</span>
                        {% endif %}
                    </td>
                    <td class="px-6 py-4 text-slate-600">{{ plan.timeframe|default:"—" }}</td>
                    <td class="px-6 py-4 text-slate-600">{{ plan.evidences|truncatewords:10|default:"—" }}</td>
                </tr>
                {% endfor %}
            </tbody>
        </table>
    </div>
</div>
{% empty %}
<div class="section-card">
    <div class="px-6 py-16 text-center">
        <i data-lucide="clipboard-list" class="w-12 h-12 mx-auto text-slate-300 mb-3"></i>
        <p class="text-slate">{% trans "No action plans found." %}</p>
    </div>
</div>
{% endfor %}
{% endblock %}
