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

{% block title %}Journey Templates - 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 %}
<div class="container-fluid">
    <!-- Page Header -->
    <div class="page-header-gradient">
        <div class="d-flex justify-content-between align-items-center">
            <div>
                <h2 class="mb-1">
                    <i data-lucide="git-branch" class="me-2"></i>
                    Journey Templates
                </h2>
                <p class="mb-0 opacity-75">{% trans "Manage journey templates and stages" %}</p>
            </div>
            <a href="{% url 'journeys:template_create' %}" class="btn btn-light">
                <i data-lucide="plus-circle" class="me-1" style="width: 18px; height: 18px;"></i> {% trans "Create Journey Template" %}
            </a>
        </div>
    </div>

    <!-- Templates Table -->
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-primary bg-opacity-10 text-primary">
                <i data-lucide="layers" style="width: 20px; height: 20px;"></i>
            </div>
            <h5 class="mb-0">{% trans "Journey Templates" %}</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>{% trans "Name" %}</th>
                        <th>{% trans "Journey Type" %}</th>
                        <th>{% trans "Hospital" %}</th>
                        <th>{% trans "Stages" %}</th>
                        <th>{% trans "Status" %}</th>
                        <th>{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody>
                    {% for template in templates %}
                    <tr>
                        <td><strong>{{ template.name }}</strong></td>
                        <td><span class="badge bg-primary">{{ template.get_journey_type_display }}</span></td>
                        <td><small>{{ template.hospital.get_localized_name }}</small></td>
                        <td><span class="badge bg-info">{{ template.stages.count }} {% trans "stages" %}</span></td>
                        <td>
                            {% if template.is_active %}
                            <span class="badge bg-success">{% trans "Active" %}</span>
                            {% else %}
                            <span class="badge bg-secondary">{% trans "Inactive" %}</span>
                            {% endif %}
                        </td>
                        <td>
                            <div class="dropdown">
                                <button class="btn btn-sm btn-light dropdown-toggle" type="button" data-bs-toggle="dropdown">
                                    <i data-lucide="more-vertical" style="width: 16px; height: 16px;"></i> {% trans "Actions" %}
                                </button>
                                <ul class="dropdown-menu">
                                    <li>
                                        <a class="dropdown-item" href="{% url 'journeys:template_detail' template.pk %}">
                                            <i data-lucide="eye" class="me-2" style="width: 16px; height: 16px;"></i>{% trans "View" %}
                                        </a>
                                    </li>
                                    <li>
                                        <a class="dropdown-item" href="{% url 'journeys:template_edit' template.pk %}">
                                            <i data-lucide="pencil" class="me-2" style="width: 16px; height: 16px;"></i>{% trans "Edit" %}
                                        </a>
                                    </li>
                                    <li>
                                        <a class="dropdown-item" href="{% url 'journeys:instance_list' %}?journey_type={{ template.journey_type }}">
                                            <i data-lucide="list-checks" class="me-2" style="width: 16px; height: 16px;"></i>{% trans "View Instances" %}
                                        </a>
                                    </li>
                                    <li><hr class="dropdown-divider"></li>
                                    <li>
                                        <button type="button" class="dropdown-item text-danger" data-bs-toggle="modal" data-bs-target="#deleteModal-{{ template.pk }}">
                                            <i data-lucide="trash-2" class="me-2" style="width: 16px; height: 16px;"></i>{% trans "Delete" %}
                                        </button>
                                    </li>
                                </ul>
                            </div>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="6" class="text-center py-5">
                            <i data-lucide="inbox" style="font-size: 3rem; color: #ccc;"></i>
                            <p class="text-muted mt-3">{% trans "No templates found" %}</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    {% if page_obj.has_other_pages %}
    <nav aria-label="Templates pagination" class="mt-4">
        <ul class="pagination justify-content-center">
            {% if page_obj.has_previous %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.previous_page_number }}">
                    <i data-lucide="chevron-left" style="width: 16px; height: 16px;"></i>
                </a>
            </li>
            {% endif %}
            
            {% for num in page_obj.paginator.page_range %}
                {% if page_obj.number == num %}
                <li class="page-item active"><span class="page-link">{{ num }}</span></li>
                {% elif num > page_obj.number|add:'-3' and num < page_obj.number|add:'3' %}
                <li class="page-item">
                    <a class="page-link" href="?page={{ num }}">{{ num }}</a>
                </li>
                {% endif %}
            {% endfor %}
            
            {% if page_obj.has_next %}
            <li class="page-item">
                <a class="page-link" href="?page={{ page_obj.next_page_number }}">
                    <i data-lucide="chevron-right" style="width: 16px; height: 16px;"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>

<!-- Delete Confirmation Modals -->
{% for template in templates %}
<div class="modal fade" id="deleteModal-{{ template.pk }}" tabindex="-1" aria-labelledby="deleteModalLabel-{{ template.pk }}" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header bg-danger text-white">
                <h5 class="modal-title" id="deleteModalLabel-{{ template.pk }}">{% trans "Confirm Delete" %}</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <div class="modal-body">
                {% blocktrans %}Are you sure you want to delete the journey template "<strong>{{ template.name }}</strong>"?{% endblocktrans %}
                <p class="text-muted">{% trans "This will also delete all associated stages and cannot be undone." %}</p>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">{% trans "Cancel" %}</button>
                <form method="post" action="{% url 'journeys:template_delete' template.pk %}">
                    {% csrf_token %}
                    <button type="submit" class="btn btn-danger">{% trans "Delete" %}</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endfor %}
{% endblock %}
