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

{% block title %}Patient Journeys - 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;
    }
    .filter-panel {
        background: #f8f9fa;
        border: 1px solid #dee2e6;
        border-radius: 8px;
        padding: 20px;
        margin-bottom: 20px;
    }
    .filter-panel.collapsed {
        padding: 10px 20px;
    }
    .filter-panel.collapsed .filter-body {
        display: none;
    }
    .journey-row:hover {
        background: #f8f9fa;
        cursor: pointer;
    }
    .progress-bar-custom {
        height: 25px;
        border-radius: 12px;
        font-weight: 600;
    }
    .status-badge {
        padding: 4px 12px;
        border-radius: 12px;
        font-size: 0.85rem;
        font-weight: 500;
    }
    .status-active { background: #e3f2fd; color: #1976d2; }
    .status-completed { background: #e8f5e9; color: #388e3c; }
    .status-cancelled { background: #ffebee; color: #d32f2f; }
    .stat-card {
        border-left: 4px solid;
        transition: transform 0.2s;
    }
    .stat-card:hover {
        transform: translateY(-2px);
    }
</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-merge" class="me-2"></i>
                    Patient Journeys
                </h2>
                <p class="mb-0 opacity-75">{% trans "Monitor patient journey instances and stage completion" %}</p>
            </div>
        </div>
    </div>

    <!-- Statistics Cards -->
    <div class="row mb-4">
        <div class="col-md-4">
            <div class="card stat-card border-primary">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Total Journeys" %}</h6>
                            <h3 class="mb-0">{{ stats.total }}</h3>
                        </div>
                        <div class="text-primary">
                            <i data-lucide="list" style="width: 2rem; height: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card stat-card border-info">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Active" %}</h6>
                            <h3 class="mb-0">{{ stats.active }}</h3>
                        </div>
                        <div class="text-info">
                            <i data-lucide="hourglass" style="width: 2rem; height: 2rem;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="col-md-4">
            <div class="card stat-card border-success">
                <div class="card-body">
                    <div class="d-flex justify-content-between align-items-center">
                        <div>
                            <h6 class="text-muted mb-1">{% trans "Completed" %}</h6>
                            <h3 class="mb-0">{{ stats.completed }}</h3>
                        </div>
                        <div class="text-success">
                            <i data-lucide="check-circle" style="width: 2rem; height: 2px;"></i>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Filter Panel -->
    <div class="filter-panel" id="filterPanel">
        <div class="d-flex justify-content-between align-items-center mb-3">
            <h5 class="mb-0">
                <i data-lucide="filter" class="me-2" style="width: 18px; height: 18px;"></i>{% trans "Filters" %}
            </h5>
            <button class="btn btn-sm btn-outline-secondary" onclick="toggleFilters()">
                <i data-lucide="chevron-up" id="filterToggleIcon" style="width: 18px; height: 18px;"></i>
            </button>
        </div>
        
        <div class="filter-body">
            <form method="get" action="{% url 'journeys:instance_list' %}" id="filterForm">
                <div class="row g-3">
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Search" %}</label>
                        <input type="text" class="form-control" name="search" 
                               placeholder="{% trans 'Encounter ID, MRN, Patient name...' %}" 
                               value="{{ filters.search }}">
                    </div>
                    
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Journey Type" %}</label>
                        <select class="form-select" name="journey_type">
                            <option value="">{% trans "All Types" %}</option>
                            <option value="ems" {% if filters.journey_type == 'ems' %}selected{% endif %}>{% trans "EMS" %}</option>
                            <option value="inpatient" {% if filters.journey_type == 'inpatient' %}selected{% endif %}>{% trans "Inpatient" %}</option>
                            <option value="opd" {% if filters.journey_type == 'opd' %}selected{% endif %}>{% trans "OPD" %}</option>
                        </select>
                    </div>
                    
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Status" %}</label>
                        <select class="form-select" name="status">
                            <option value="">{% trans "All Statuses" %}</option>
                            <option value="active" {% if filters.status == 'active' %}selected{% endif %}>{% trans "Active" %}</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 class="col-md-4">
                        <label class="form-label">{% trans "Department" %}</label>
                        <select class="form-select" name="department" data-tomselect>
                            <option value="">{% trans "All Departments" %}</option>
                            {% for dept in departments %}
                            <option value="{{ dept.id }}" {% if filters.department == dept.id|stringformat:"s" %}selected{% endif %}>
                                {{ dept.get_localized_name }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div class="col-md-2">
                        <label class="form-label">{% trans "Date From" %}</label>
                        <input type="date" class="form-control" name="date_from" value="{{ filters.date_from }}">
                    </div>
                    <div class="col-md-2">
                        <label class="form-label">{% trans "Date To" %}</label>
                        <input type="date" class="form-control" name="date_to" value="{{ filters.date_to }}">
                    </div>
                </div>
                
                <div class="mt-3 d-flex gap-2">
                    <button type="submit" class="btn btn-primary">
                        <i data-lucide="search" class="me-1" style="width: 16px; height: 16px;"></i> {% trans "Apply Filters" %}
                    </button>
                    <a href="{% url 'journeys:instance_list' %}" class="btn btn-outline-secondary">
                        <i data-lucide="x-circle" class="me-1" style="width: 16px; height: 16px;"></i> {% trans "Clear" %}
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Journeys Table -->
    <div class="section-card">
        <div class="section-header">
            <div class="section-icon bg-primary bg-opacity-10 text-primary">
                <i data-lucide="route" style="width: 20px; height: 20px;"></i>
            </div>
            <h5 class="mb-0">{% trans "Patient Journeys" %}</h5>
        </div>
        <div class="table-responsive">
            <table class="table table-hover mb-0">
                <thead class="table-light">
                    <tr>
                        <th>{% trans "Encounter ID" %}</th>
                        <th>{% trans "Patient" %}</th>
                        <th>{% trans "Journey Type" %}</th>
                        <th>{% trans "Hospital" %}</th>
                        <th>{% trans "Progress" %}</th>
                        <th>{% trans "Status" %}</th>
                        <th>{% trans "Started" %}</th>
                        <th>{% trans "Actions" %}</th>
                    </tr>
                </thead>
                <tbody>
                    {% for journey in journeys %}
                    <tr class="journey-row" onclick="window.location='{% url 'journeys:instance_detail' journey.id %}'">
                        <td>
                            <strong>{{ journey.encounter_id }}</strong>
                        </td>
                        <td>
                            <strong>{{ journey.patient.get_full_name }}</strong><br>
                            <small class="text-muted">MRN: {{ journey.patient.mrn }}</small>
                        </td>
                        <td>
                            <span class="badge bg-primary">{{ journey.journey_template.get_journey_type_display }}</span>
                        </td>
                        <td>
                            <small>{{ journey.hospital.get_localized_name|truncatewords:3 }}</small>
                        </td>
                        <td style="width: 200px;">
                            {% with completion=journey.get_completion_percentage %}
                            <div class="progress progress-bar-custom">
                                <div class="progress-bar {% if completion == 100 %}bg-success{% elif completion > 50 %}bg-info{% else %}bg-warning{% endif %}" 
                                     style="width: {{ completion }}%">
                                    {{ completion }}%
                                </div>
                            </div>
                            {% endwith %}
                        </td>
                        <td>
                            <span class="status-badge status-{{ journey.status }}">
                                {{ journey.get_status_display }}
                            </span>
                        </td>
                        <td>
                            <small class="text-muted">{{ journey.started_at|date:"M d, Y H:i" }}</small>
                        </td>
                        <td onclick="event.stopPropagation();">
                            <a href="{% url 'journeys:instance_detail' journey.id %}" 
                               class="btn btn-sm btn-outline-primary">
                                <i data-lucide="eye" style="width: 16px; height: 16px;"></i>
                            </a>
                        </td>
                    </tr>
                    {% empty %}
                    <tr>
                        <td colspan="8" class="text-center py-5">
                            <i data-lucide="inbox" style="width: 3rem; height: 3rem; color: #ccc;"></i>
                            <p class="text-muted mt-3">{% trans "No journeys found" %}</p>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>

    <!-- Pagination -->
    {% if page_obj.has_other_pages %}
    <nav aria-label="Journeys 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 }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <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 }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                        {{ 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 }}{% for key, value in filters.items %}&{{ key }}={{ value }}{% endfor %}">
                    <i data-lucide="chevron-right" style="width: 16px; height: 16px;"></i>
                </a>
            </li>
            {% endif %}
        </ul>
    </nav>
    {% endif %}
</div>
{% endblock %}

{% block extra_js %}
<script>
function toggleFilters() {
    const panel = document.getElementById('filterPanel');
    const icon = document.getElementById('filterToggleIcon');
    panel.classList.toggle('collapsed');
    // Toggle between chevron-up and chevron-down
    const currentIcon = icon.getAttribute('data-lucide');
    if (currentIcon === 'chevron-up') {
        icon.setAttribute('data-lucide', 'chevron-down');
    } else {
        icon.setAttribute('data-lucide', 'chevron-up');
    }
    lucide.createIcons();
}
</script>
{% endblock %}
