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

{% block title %}{{ template.name }} - PX360{% endblock %}

{% block content %}
<div class="container-fluid">
    <div class="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-route text-primary me-2"></i>
                {{ template.name }}
            </h2>
            <p class="text-muted mb-0">{{ template.name_ar }}</p>
        </div>
        <div>
            <a href="{% url 'journeys:template_list' %}" class="btn btn-outline-secondary me-2">
                <i class="bi bi-arrow-left me-1"></i> {% trans "Back to Templates" %}
            </a>
            <a href="{% url 'journeys:template_edit' template.pk %}" class="btn btn-primary me-2">
                <i class="bi bi-pencil me-1"></i> {% trans "Edit" %}
            </a>
            <button type="button" class="btn btn-danger" data-bs-toggle="modal" data-bs-target="#deleteModal">
                <i class="bi bi-trash me-1"></i> {% trans "Delete" %}
            </button>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card bg-primary text-white">
                <div class="card-body">
                    <h6 class="card-title">{% trans "Total Journeys" %}</h6>
                    <h3 class="mb-0">{{ stats.total_instances }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card bg-warning text-white">
                <div class="card-body">
                    <h6 class="card-title">{% trans "Active" %}</h6>
                    <h3 class="mb-0">{{ stats.active_instances }}</h3>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card bg-success text-white">
                <div class="card-body">
                    <h6 class="card-title">{% trans "Completed" %}</h6>
                    <h3 class="mb-0">{{ stats.completed_instances }}</h3>
                </div>
            </div>
        </div>
    </div>

    <!-- Template Details -->
    <div class="row">
        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">{% trans "Template Details" %}</h5>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <th width="30%">{% trans "Hospital:" %}</th>
                            <td>{{ template.hospital.get_localized_name }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Status:" %}</th>
                            <td>
                                {% if template.is_active %}
                                <span class="badge bg-success">{% trans "Active" %}</span>
                                {% else %}
                                <span class="badge bg-secondary">{% trans "Inactive" %}</span>
                                {% endif %}
                            </td>
                        </tr>
                        <tr>
                            <th>{% trans "Description:" %}</th>
                            <td>{{ template.description|default:"-" }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Created By:" %}</th>
                            <td>{{ template.created_by.get_full_name|default:"System" }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Created At:" %}</th>
                            <td>{{ template.created_at|date:"Y-m-d H:i" }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>

        <div class="col-md-6">
            <div class="card mb-4">
                <div class="card-header">
                    <h5 class="mb-0">{% trans "Statistics" %}</h5>
                </div>
                <div class="card-body">
                    <table class="table table-borderless">
                        <tr>
                            <th width="30%">{% trans "Total Stages:" %}</th>
                            <td>{{ stages.count }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Total Journeys:" %}</th>
                            <td>{{ stats.total_instances }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Active Journeys:" %}</th>
                            <td>{{ stats.active_instances }}</td>
                        </tr>
                        <tr>
                            <th>{% trans "Completed Journeys:" %}</th>
                            <td>{{ stats.completed_instances }}</td>
                        </tr>
                    </table>
                </div>
            </div>
        </div>
    </div>

    <!-- Stages -->
    <div class="card">
        <div class="card-header">
            <h5 class="mb-0">{% trans "Journey Stages" %} ({{ stages.count }})</h5>
        </div>
        <div class="card-body">
            {% if stages %}
            <div class="table-responsive">
                <table class="table table-hover">
                    <thead>
                        <tr>
                            <th width="10%">{% trans "Order" %}</th>
                            <th width="25%">{% trans "Stage Name (EN)" %}</th>
                            <th width="25%">{% trans "Stage Name (AR)" %}</th>
                            <th width="20%">{% trans "Survey Template" %}</th>
                            <th width="10%">{% trans "Active" %}</th>
                            <th width="10%">{% trans "Actions" %}</th>
                        </tr>
                    </thead>
                    <tbody>
                        {% for stage in stages %}
                        <tr>
                            <td><span class="badge bg-secondary">{{ stage.order }}</span></td>
                            <td>{{ stage.name }}</td>
                            <td>{{ stage.name_ar|default:"-" }}</td>
                            <td>
                                {% if stage.survey_template %}
                                <small>
                                    <i class="bi bi-file-earmark-text text-primary me-1"></i>
                                    {{ stage.survey_template.name }}
                                </small>
                                {% else %}
                                <span class="text-muted">{% trans "No survey assigned" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                {% if stage.is_active %}
                                <span class="badge bg-success">{% trans "Yes" %}</span>
                                {% else %}
                                <span class="badge bg-secondary">{% trans "No" %}</span>
                                {% endif %}
                            </td>
                            <td>
                                <span class="text-muted small" title="{% trans 'Edit template to change survey assignment' %}">
                                    <i class="bi bi-pencil"></i> {% trans "Edit" %}
                                </span>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
            {% else %}
            <div class="text-center py-4">
                <i class="bi bi-inbox" style="font-size: 2rem; color: #ccc;"></i>
                <p class="text-muted mt-2">{% trans "No stages defined yet" %}</p>
            </div>
            {% endif %}
        </div>
    </div>
</div>

<!-- Delete Confirmation Modal -->
<div class="modal fade" id="deleteModal" tabindex="-1" aria-labelledby="deleteModalLabel" 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">{% 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 survey assignments. This action 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 Template" %}</button>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}
