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

{% block title %}{{ template.name }} - {% trans "Survey Template" %} - 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;
}
</style>
{% endblock %}

{% block content %}
<!-- Back Button -->
<div class="mb-6">
    <a href="{% url 'surveys:template_list' %}" class="inline-flex items-center gap-2 px-4 py-2 border border-slate-200 rounded-xl text-slate hover:text-navy hover:bg-light transition text-sm font-semibold">
        <i data-lucide="arrow-left" class="w-4 h-4"></i> {% trans "Back to Templates" %}
    </a>
</div>

<!-- Breadcrumb & Header -->
<header class="mb-6">
    <div class="flex items-center gap-2 text-sm text-slate mb-2">
        <a href="{% url 'surveys:template_list' %}">{% trans "Templates" %}</a>
        <i data-lucide="chevron-right" class="w-4 h-4"></i>
        <span class="font-bold text-navy">{{ template.name }}</span>
        {% if template.is_active %}
        <span class="ml-2 px-2 py-0.5 rounded-full text-[10px] font-bold uppercase tracking-wider bg-green-100 text-green-700">{% trans "Active" %}</span>
        {% else %}
        <span class="ml-2 px-2 py-0.5 rounded-full text-[10px] font-bold uppercase tracking-wider bg-slate-100 text-slate-600">{% trans "Inactive" %}</span>
        {% endif %}
    </div>
    <div class="flex items-center justify-between">
        <h1 class="text-2xl font-bold text-navy">{{ template.name }}</h1>
        <div class="flex items-center gap-3">
            <a href="{% url 'surveys:template_edit' template.pk %}" class="px-4 py-2 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition flex items-center gap-2 text-sm">
                <i data-lucide="pencil" class="w-4 h-4"></i> {% trans "Edit Template" %}
            </a>
        </div>
    </div>
</header>

<!-- Year Filter -->
<div class="flex items-center justify-between mb-4">
    <div class="flex items-center gap-3">
        <label for="year-filter" class="text-sm font-semibold text-slate">{% trans "Year" %}:</label>
        <form method="get" class="inline-flex items-center gap-2">
            <select id="year-filter" name="year" onchange="this.form.submit()" class="form-select rounded-lg border-slate-200 text-sm py-1.5 px-3">
                {% for year in available_years %}
                <option value="{{ year }}" {% if year == selected_year %}selected{% endif %}>{{ year }}</option>
                {% endfor %}
            </select>
        </form>
    </div>
    <span class="text-xs text-slate font-medium">{% trans "Showing data for" %} {{ selected_year }}</span>
</div>

<!-- Stats Banner -->
{% if stats %}
<div class="bg-gradient-to-br from-navy to-blue rounded-2xl p-6 mb-6 text-white shadow-lg">
    <div class="grid grid-cols-3 gap-4">
        <div class="text-center">
            <div class="text-white/80 text-xs font-semibold mb-1">{% trans "Total Sent" %}</div>
            <div class="text-2xl font-bold">{{ stats.total_instances|default:0 }}</div>
        </div>
        <div class="text-center">
            <div class="text-white/80 text-xs font-semibold mb-1">{% trans "Completed" %}</div>
            <div class="text-2xl font-bold">{{ stats.completed_instances|default:0 }}</div>
        </div>
        <div class="text-center">
            <div class="text-white/80 text-xs font-semibold mb-1">{% trans "Positive" %}</div>
            <div class="text-2xl font-bold text-green-300">{{ stats.positive_instances|default:0 }}</div>
        </div>
        <div class="text-center">
            <div class="text-white/80 text-xs font-semibold mb-1">{% trans "Negative" %}</div>
            <div class="text-2xl font-bold text-red-300">{{ stats.negative_instances|default:0 }}</div>
        </div>
        <div class="text-center">
            <div class="text-white/80 text-xs font-semibold mb-1">{% trans "Positive Rate" %}</div>
            <div class="text-2xl font-bold">{{ stats.positive_rate|floatformat:0|default:"0" }}%</div>
        </div>
        <div class="text-center">
            <div class="text-white/80 text-xs font-semibold mb-1">{% trans "Completion Rate" %}</div>
            <div class="text-2xl font-bold">{{ stats.completion_rate|floatformat:0|default:"0" }}%</div>
        </div>
        <div class="text-center">
            <div class="text-white/80 text-xs font-semibold mb-1">{% trans "Avg Score" %}</div>
            <div class="text-2xl font-bold">{{ stats.avg_score|floatformat:1|default:"-" }}</div>
        </div>
    </div>
</div>
{% endif %}

<!-- Main Content Grid -->
<div class="grid grid-cols-12 gap-8">
    <!-- Left Column -->
    <div class="col-span-8 space-y-6">

        <!-- Questions Card -->
        <div class="section-card">
            <div class="section-header">
                <div class="section-icon bg-blue-100">
                    <i data-lucide="list-ordered" class="w-5 h-5 text-blue-600"></i>
                </div>
                <h3 class="font-bold text-navy">{% trans "Questions" %}</h3>
                <div class="ml-auto text-xs text-slate font-medium">
                    {{ questions|length }} {% trans "question" %}{{ questions|length|pluralize:"s" }}
                </div>
            </div>

            {% if questions %}
            <div class="overflow-x-auto">
                <table class="w-full">
                    <thead class="bg-slate-50 border-b border-slate-100">
                        <tr class="text-xs font-bold text-slate uppercase tracking-wider">
                            <th class="px-6 py-4 text-left">{% trans "Order" %}</th>
                            <th class="px-6 py-4 text-left">{% trans "Question (English)" %}</th>
                            <th class="px-6 py-4 text-left">{% trans "Question (Arabic)" %}</th>
                            <th class="px-6 py-4 text-left">{% trans "Type" %}</th>
                            <th class="px-6 py-4 text-left">{% trans "Required" %}</th>
                            <th class="px-6 py-4 text-left">{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody class="divide-y divide-slate-100">
                        {% for question in questions %}
                        <tr class="hover:bg-light/30 transition">
                            <td class="px-6 py-4">
                                <span class="font-semibold text-slate">{{ question.order }}</span>
                            </td>
                            <td class="px-6 py-4">
                                <span class="font-semibold text-navy">{{ question.text|truncatewords:10 }}</span>
                            </td>
                            <td class="px-6 py-4">
                                <span class="text-sm text-slate" dir="rtl">{{ question.text_ar|truncatewords:10 }}</span>
                            </td>
                            <td class="px-6 py-4">
                                <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-blue-100 text-blue-700">{{ question.get_question_type_display }}</span>
                            </td>
                            <td class="px-6 py-4">
                                {% if question.is_required %}
                                <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-green-100 text-green-700">{% trans "Yes" %}</span>
                                {% else %}
                                <span class="px-2.5 py-1 rounded-lg text-xs font-bold bg-slate-100 text-slate-600">{% trans "No" %}</span>
                                {% endif %}
                            </td>
                            <td class="px-6 py-4">
                                <a href="{% url 'surveys:template_edit' template.pk %}#question-{{ question.pk }}" class="px-3 py-1.5 text-xs font-semibold rounded-lg bg-slate-100 text-slate-600 hover:bg-slate-200 transition">
                                    {% trans "Edit" %}
                                </a>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-12">
                <div class="inline-flex items-center justify-center w-16 h-16 bg-slate-100 rounded-full mb-3">
                    <i data-lucide="help-circle" class="w-8 h-8 text-slate-400"></i>
                </div>
                <h3 class="text-lg font-bold text-navy mb-1">{% trans "No Questions Yet" %}</h3>
                <p class="text-slate text-sm mb-4">{% trans "Add questions to this template." %}</p>
                <a href="{% url 'surveys:template_edit' template.pk %}" class="px-4 py-2 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition inline-flex items-center gap-2 text-sm">
                    <i data-lucide="plus" class="w-4 h-4"></i> {% trans "Add Questions" %}
                </a>
            </div>
            {% endif %}
        </div>
    </div>

    <!-- Right Column (Sidebar) -->
    <div class="col-span-4 space-y-6">

        <!-- Template Information -->
        <section class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
            <h3 class="font-bold text-navy mb-4 text-sm flex items-center gap-2">
                <i data-lucide="info" class="w-4 h-4 text-blue"></i>
                {% trans "Template Information" %}
            </h3>
            <ul class="space-y-3 text-sm">
                <li class="flex justify-between border-b border-slate-100 pb-2">
                    <span class="text-slate">{% trans "Name" %}</span>
                    <span class="font-bold text-navy">{{ template.name }}</span>
                </li>
                {% if template.name_ar %}
                <li class="flex justify-between border-b border-slate-100 pb-2">
                    <span class="text-slate">{% trans "Name (Arabic)" %}</span>
                    <span class="font-bold text-navy" dir="rtl">{{ template.name_ar }}</span>
                </li>
                {% endif %}
                <li class="flex justify-between border-b border-slate-100 pb-2">
                    <span class="text-slate">{% trans "Survey Type" %}</span>
                    <span class="px-2 py-0.5 rounded-lg text-xs font-bold bg-light text-navy">{{ template.get_survey_type_display }}</span>
                </li>
                <li class="flex justify-between border-b border-slate-100 pb-2">
                    <span class="text-slate">{% trans "Scoring" %}</span>
                    <span class="px-2 py-0.5 rounded-lg text-xs font-bold bg-purple-100 text-purple-700">{{ template.get_scoring_method_display }}</span>
                </li>
                {% if template.negative_threshold %}
                <li class="flex justify-between border-b border-slate-100 pb-2">
                    <span class="text-slate">{% trans "Negative Threshold" %}</span>
                    <span class="font-bold text-slate-800">{{ template.negative_threshold }}</span>
                </li>
                {% endif %}
                {% if template.hospital %}
                <li class="flex justify-between border-b border-slate-100 pb-2">
                    <span class="text-slate">{% trans "Hospital" %}</span>
                    <span class="font-bold text-slate-800">{{ template.hospital.name }}</span>
                </li>
                {% endif %}
                <li class="flex justify-between">
                    <span class="text-slate">{% trans "Status" %}</span>
                    {% if template.is_active %}
                    <span class="px-2 py-0.5 rounded-lg text-xs font-bold bg-green-100 text-green-600">{% trans "Active" %}</span>
                    {% else %}
                    <span class="px-2 py-0.5 rounded-lg text-xs font-bold bg-slate-100 text-slate-600">{% trans "Inactive" %}</span>
                    {% endif %}
                </li>
            </ul>
        </section>

        <!-- Quick Actions -->
        <section class="bg-white rounded-2xl p-6 shadow-sm border border-slate-100">
            <h3 class="font-bold text-navy mb-4 text-sm flex items-center gap-2">
                <i data-lucide="zap" class="w-4 h-4 text-blue"></i>
                {% trans "Quick Actions" %}
            </h3>
            <div class="space-y-2">
                <a href="{% url 'surveys:template_edit' template.pk %}" class="w-full px-4 py-3 bg-navy text-white rounded-xl font-semibold hover:bg-blue transition flex items-center justify-center gap-2 text-sm">
                    <i data-lucide="pencil" class="w-4 h-4"></i>{% trans "Edit Template" %}
                </a>
                <a href="{% url 'surveys:template_delete' template.pk %}" class="w-full px-4 py-3 bg-white border border-red-200 text-red-600 rounded-xl font-semibold hover:bg-red-50 transition flex items-center justify-center gap-2 text-sm">
                    <i data-lucide="trash-2" class="w-4 h-4"></i>{% trans "Delete Template" %}
                </a>
            </div>
        </section>
    </div>
</div>
{% endblock %}
