{% extends "base.html" %}
{% load static %}

{% block content %}
<div class="container my-5">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h1 class="h2 text-white"><i class="bi bi-patch-question me-2 text-primary"></i>Survey Dashboard</h1>
            <p class="text-muted mb-0">Create and manage reusable feedback forms and post-exam surveys.</p>
        </div>
        <div>
            <a href="{% url 'survey:survey_create' %}" class="btn btn-primary">
                <i class="bi bi-plus-lg me-1"></i>Create Survey
            </a>
        </div>
    </div>

    {% if messages %}
        {% for message in messages %}
            <div class="alert alert-{{ message.tags }} alert-dismissible fade show" role="alert">
                {{ message }}
                <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
            </div>
        {% endfor %}
    {% endif %}

    <div class="card bg-dark border-secondary text-white shadow">
        <div class="card-body p-0">
            <div class="table-responsive">
                <table class="table table-dark table-striped table-hover align-middle mb-0">
                    <thead>
                        <tr class="border-secondary">
                            <th class="ps-4 py-3">Survey Name</th>
                            <th class="py-3">Created By</th>
                            <th class="py-3 text-center">Questions</th>
                            <th class="py-3 text-center">Responses</th>
                            <th class="py-3 text-center">Status</th>
                            <th class="py-3 text-center">Type</th>
                            <th class="pe-4 py-3 text-end">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for survey in surveys %}
                            <tr class="border-secondary">
                                <td class="ps-4 py-3">
                                    <span class="fw-semibold text-white d-block">{{ survey.name }}</span>
                                    {% if survey.description %}
                                        <small class="text-muted d-block text-truncate" style="max-width: 250px;">{{ survey.description }}</small>
                                    {% endif %}
                                </td>
                                <td class="py-3">{{ survey.created_by.username|default:"System" }}</td>
                                <td class="py-3 text-center">
                                    <span class="badge bg-secondary">{{ survey.questions.count }}</span>
                                </td>
                                <td class="py-3 text-center">
                                    <span class="badge bg-info">{{ survey.responses.count }}</span>
                                </td>
                                <td class="py-3 text-center">
                                    {% if survey.active %}
                                        <span class="badge bg-success"><i class="bi bi-check-circle me-1"></i>Active</span>
                                    {% else %}
                                        <span class="badge bg-danger"><i class="bi bi-x-circle me-1"></i>Inactive</span>
                                    {% endif %}
                                </td>
                                <td class="py-3 text-center">
                                    {% if survey.anonymous %}
                                        <span class="badge bg-warning text-dark"><i class="bi bi-incognito me-1"></i>Anonymous</span>
                                    {% else %}
                                        <span class="badge bg-secondary"><i class="bi bi-person me-1"></i>Standard</span>
                                    {% endif %}
                                </td>
                                <td class="pe-4 py-3 text-end">
                                    <div class="btn-group btn-group-sm" role="group">
                                        <a href="{% url 'survey:survey_questions_edit' survey.pk %}" class="btn btn-outline-primary" title="Manage Questions">
                                            <i class="bi bi-list-task me-1"></i>Questions
                                        </a>
                                        <a href="{% url 'survey:survey_results' survey.pk %}" class="btn btn-outline-success" title="View Results">
                                            <i class="bi bi-bar-chart-fill me-1"></i>Analytics
                                        </a>
                                        <a href="{% url 'survey:survey_export_csv' survey.pk %}" class="btn btn-outline-info" title="Export CSV">
                                            <i class="bi bi-download"></i>
                                        </a>
                                        <a href="{% url 'survey:survey_update' survey.pk %}" class="btn btn-outline-secondary" title="Edit Metadata">
                                            <i class="bi bi-pencil"></i>
                                        </a>
                                        <a href="{% url 'survey:survey_delete' survey.pk %}" class="btn btn-outline-danger" title="Delete">
                                            <i class="bi bi-trash"></i>
                                        </a>
                                    </div>
                                </td>
                            </tr>
                        {% empty %}
                            <tr>
                                <td colspan="7" class="text-center py-5 text-muted">
                                    <i class="bi bi-patch-question d-block fs-1 mb-3"></i>
                                    No surveys created yet. Click "Create Survey" to get started.
                                </td>
                            </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</div>
{% endblock %}
