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

{% block title %}{% trans "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;
    }

    .section-icon-blue {
        background: linear-gradient(135deg, #005696, #007bbd);
        color: white;
    }

    .section-icon-orange {
        background: linear-gradient(135deg, #f97316, #fb923c);
        color: white;
    }

    .section-icon-gray {
        background: linear-gradient(135deg, #64748b, #94a3b8);
        color: white;
    }
</style>
{% endblock %}

{% block content %}
<!-- Page Header -->
<div class="page-header-gradient">
    <div class="flex justify-between items-center">
        <div>
            <h1 class="text-3xl font-bold mb-2 flex items-center gap-3">
                <i data-lucide="check-circle-2" class="w-8 h-8"></i>
                {% trans "Action Plans" %}
            </h1>
            <p class="opacity-90">{% trans "Manage improvement action plans" %}</p>
        </div>
        <a href="{% url 'actions:action_create' %}" class="bg-white text-[#005696] px-6 py-3 rounded-xl font-bold flex items-center gap-2 shadow-lg hover:bg-gray-50 transition">
            <i data-lucide="plus" class="w-5 h-5"></i> {% trans "Create Action Plan" %}
        </a>
    </div>
</div>

<!-- Filters Section -->
<div class="section-card mb-6">
    <div class="section-header">
        <div class="section-icon section-icon-blue">
            <i data-lucide="filter" class="w-5 h-5"></i>
        </div>
        <h3 class="font-bold text-gray-800">{% trans "Filters" %}</h3>
    </div>
    <div class="p-6">
        <form method="get" class="grid grid-cols-1 md:grid-cols-4 gap-4">
            <div class="md:col-span-2">
                <label class="block text-sm font-semibold text-gray-700 mb-2">{% trans "Search" %}</label>
                <input type="text" class="w-full px-4 py-3 border border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-[#005696] focus:border-transparent transition" name="search" placeholder="{% trans 'Search action plans...' %}" value="{{ filters.search }}">
            </div>
            <div>
                <label class="block text-sm font-semibold text-gray-700 mb-2">{% trans "Status" %}</label>
                <select class="w-full px-4 py-3 border border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-[#005696] focus:border-transparent transition" name="status">
                    <option value="">{% trans "All Status" %}</option>
                    <option value="pending" {% if filters.status == 'pending' %}selected{% endif %}>{% trans "Pending" %}</option>
                    <option value="in_progress" {% if filters.status == 'in_progress' %}selected{% endif %}>{% trans "In Progress" %}</option>
                    <option value="completed" {% if filters.status == 'completed' %}selected{% endif %}>{% trans "Completed" %}</option>
                    <option value="cancelled" {% if filters.status == 'cancelled' %}selected{% endif %}>{% trans "Cancelled" %}</option>
                </select>
            </div>
            <div>
                <label class="block text-sm font-semibold text-gray-700 mb-2">{% trans "Priority" %}</label>
                <select class="w-full px-4 py-3 border border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-[#005696] focus:border-transparent transition" name="priority">
                    <option value="">{% trans "All Priorities" %}</option>
                    <option value="high" {% if filters.priority == 'high' %}selected{% endif %}>{% trans "High" %}</option>
                    <option value="medium" {% if filters.priority == 'medium' %}selected{% endif %}>{% trans "Medium" %}</option>
                    <option value="low" {% if filters.priority == 'low' %}selected{% endif %}>{% trans "Low" %}</option>
                </select>
            </div>
            <div class="md:col-span-2 flex items-end">
                <div class="flex gap-2 w-full">
                    <button type="submit" class="flex-1 bg-[#005696] text-white px-4 py-3 rounded-xl font-semibold hover:bg-[#0069a8] transition flex items-center justify-center gap-2">
                        <i data-lucide="search" class="w-4 h-4"></i> {% trans "Filter" %}
                    </button>
                    <a href="{% url 'actions:action_list' %}" class="bg-gray-100 text-gray-600 px-4 py-3 rounded-xl font-semibold hover:bg-gray-200 transition flex items-center justify-center gap-2">
                        <i data-lucide="x-circle" class="w-4 h-4"></i>
                    </a>
                </div>
            </div>
        </form>
    </div>
</div>

<!-- Action Plans Section -->
<div class="section-card">
    <div class="section-header">
        <div class="section-icon section-icon-orange">
            <i data-lucide="check-square" class="w-5 h-5"></i>
        </div>
        <h3 class="font-bold text-gray-800">{% trans "Action Plans" %}</h3>
        <span class="ml-auto bg-gray-100 text-gray-600 px-3 py-1 rounded-full text-sm font-semibold">
            {{ page_obj.paginator.count|default:actions|length }} {% trans "Total" %}
        </span>
    </div>
    
    <div class="p-6">
        <!-- Action Plans Grid -->
        <div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
            {% for action in actions %}
            <div class="bg-white rounded-2xl shadow-sm border-2 border-gray-100 overflow-hidden hover:shadow-md hover:border-[#005696]/30 transition-all duration-300">
                <div class="p-6">
                    <div class="flex justify-between items-start mb-4">
                        <div class="flex gap-2">
                            {% if action.priority == 'high' %}
                            <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-red-100 text-red-600">{% trans "High" %}</span>
                            {% elif action.priority == 'medium' %}
                            <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-orange-100 text-orange-600">{% trans "Medium" %}</span>
                            {% else %}
                            <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-600">{% trans "Low" %}</span>
                            {% endif %}
                            
                            {% if action.status == 'completed' %}
                            <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-600">{% trans "Completed" %}</span>
                            {% elif action.status == 'in_progress' %}
                            <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-blue-100 text-blue-600">{% trans "In Progress" %}</span>
                            {% elif action.status == 'cancelled' %}
                            <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-gray-100 text-gray-600">{% trans "Cancelled" %}</span>
                            {% else %}
                            <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-yellow-100 text-yellow-600">{% trans "Pending" %}</span>
                            {% endif %}
                        </div>
                        <span class="text-xs text-gray-400">{{ action.created_at|date:"M d, Y" }}</span>
                    </div>
                    
                    <h3 class="font-bold text-gray-800 mb-2">{{ action.title }}</h3>
                    <p class="text-gray-500 text-sm mb-4 line-clamp-2">{{ action.description|truncatewords:20 }}</p>
                    
                    <div class="space-y-2 text-sm">
                        {% if action.due_date %}
                        <div class="flex items-center gap-2 text-gray-600">
                            <i data-lucide="calendar" class="w-4 h-4 text-[#005696]"></i>
                            <span>{% trans "Due:" %} {{ action.due_date|date:"M d, Y" }}</span>
                        </div>
                        {% endif %}
                        
                        {% if action.assigned_to %}
                        <div class="flex items-center gap-2 text-gray-600">
                            <i data-lucide="user" class="w-4 h-4 text-[#005696]"></i>
                            <span>{{ action.assigned_to.get_full_name|default:action.assigned_to.username }}</span>
                        </div>
                        {% endif %}
                    </div>
                </div>
                
                <div class="px-6 py-4 bg-gray-50 border-t border-gray-100 flex justify-between items-center">
                    <a href="{% url 'actions:action_detail' action.id %}" class="text-[#005696] text-sm font-bold hover:underline flex items-center gap-1">
                        {% trans "View Details" %} <i data-lucide="arrow-right" class="w-4 h-4"></i>
                    </a>
                </div>
            </div>
            {% empty %}
            <div class="col-span-full">
                <div class="bg-gray-50 rounded-2xl border-2 border-dashed border-gray-200 p-12 text-center">
                    <div class="section-icon section-icon-gray mx-auto mb-4" style="width: 64px; height: 64px;">
                        <i data-lucide="check-square" class="w-8 h-8"></i>
                    </div>
                    <h3 class="text-xl font-bold text-gray-800 mb-2">{% trans "No Action Plans Found" %}</h3>
                    <p class="text-gray-500 mb-6">{% trans "Get started by creating your first action plan" %}</p>
                    <a href="{% url 'actions:action_create' %}" class="inline-flex items-center gap-2 bg-[#005696] text-white px-6 py-3 rounded-xl font-bold shadow-lg hover:bg-[#0069a8] transition">
                        <i data-lucide="plus" class="w-5 h-5"></i> {% trans "Create Action Plan" %}
                    </a>
                </div>
            </div>
            {% endfor %}
        </div>
        
        <!-- Pagination -->
        {% if page_obj.has_other_pages %}
        <div class="mt-6 flex justify-center">
            <div class="flex gap-2">
                {% if page_obj.has_previous %}
                <a href="?page={{ page_obj.previous_page_number }}&{{ filters|urlencode }}" class="px-4 py-2 border border-gray-200 rounded-xl text-sm font-semibold hover:bg-gray-50 transition">
                    <i data-lucide="chevron-left" class="w-4 h-4 inline"></i> {% trans "Previous" %}
                </a>
                {% endif %}
                
                <span class="px-4 py-2 bg-[#005696] text-white rounded-xl text-sm font-bold">
                    {% blocktrans with current=page_obj.number total=page_obj.paginator.num_pages %}Page {{ current }} of {{ total }}{% endblocktrans %}
                </span>
                
                {% if page_obj.has_next %}
                <a href="?page={{ page_obj.next_page_number }}&{{ filters|urlencode }}" class="px-4 py-2 border border-gray-200 rounded-xl text-sm font-semibold hover:bg-gray-50 transition">
                    {% trans "Next" %} <i data-lucide="chevron-right" class="w-4 h-4 inline"></i>
                </a>
                {% endif %}
            </div>
        </div>
        {% endif %}
    </div>
</div>
{% endblock %}
