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

{% block title %}Patient Journeys - PX360{% endblock %}

{% block extra_css %}
<style>
    .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="d-flex justify-content-between align-items-center mb-4">
        <div>
            <h2 class="mb-1">
                <i class="bi bi-diagram-3 text-primary me-2"></i>
                Patient Journeys
            </h2>
            <p class="text-muted mb-0">Monitor patient journey instances and stage completion</p>
        </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 class="bi bi-list-ul" style="font-size: 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 class="bi bi-hourglass-split" style="font-size: 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 class="bi bi-check-circle" style="font-size: 2rem;"></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 class="bi bi-funnel me-2"></i>Filters
            </h5>
            <button class="btn btn-sm btn-outline-secondary" onclick="toggleFilters()">
                <i class="bi bi-chevron-up" id="filterToggleIcon"></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="">All Types</option>
                            <option value="ems" {% if filters.journey_type == 'ems' %}selected{% endif %}>EMS</option>
                            <option value="inpatient" {% if filters.journey_type == 'inpatient' %}selected{% endif %}>Inpatient</option>
                            <option value="opd" {% if filters.journey_type == 'opd' %}selected{% endif %}>OPD</option>
                        </select>
                    </div>
                    
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Status" %}</label>
                        <select class="form-select" name="status">
                            <option value="">All Statuses</option>
                            <option value="active" {% if filters.status == 'active' %}selected{% endif %}>Active</option>
                            <option value="completed" {% if filters.status == 'completed' %}selected{% endif %}>Completed</option>
                            <option value="cancelled" {% if filters.status == 'cancelled' %}selected{% endif %}>Cancelled</option>
                        </select>
                    </div>
                    
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Hospital" %}</label>
                        <select class="form-select" name="hospital">
                            <option value="">All Hospitals</option>
                            {% for hospital in hospitals %}
                            <option value="{{ hospital.id }}" {% if filters.hospital == hospital.id|stringformat:"s" %}selected{% endif %}>
                                {{ hospital.name_en }}
                            </option>
                            {% endfor %}
                        </select>
                    </div>
                    
                    <div class="col-md-4">
                        <label class="form-label">{% trans "Department" %}</label>
                        <select class="form-select" name="department">
                            <option value="">All Departments</option>
                            {% for dept in departments %}
                            <option value="{{ dept.id }}" {% if filters.department == dept.id|stringformat:"s" %}selected{% endif %}>
                                {{ dept.name_en }}
                            </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 class="bi bi-search me-1"></i> Apply Filters
                    </button>
                    <a href="{% url 'journeys:instance_list' %}" class="btn btn-outline-secondary">
                        <i class="bi bi-x-circle me-1"></i> Clear
                    </a>
                </div>
            </form>
        </div>
    </div>

    <!-- Journeys Table -->
    <div class="card">
        <div class="card-body p-0">
            <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.name_en|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 class="bi bi-eye"></i>
                                </a>
                            </td>
                        </tr>
                        {% empty %}
                        <tr>
                            <td colspan="8" class="text-center py-5">
                                <i class="bi bi-inbox" style="font-size: 3rem; color: #ccc;"></i>
                                <p class="text-muted mt-3">No journeys found</p>
                            </td>
                        </tr>
                        {% endfor %}
                    </tbody>
                </table>
            </div>
        </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 class="bi bi-chevron-left"></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 class="bi bi-chevron-right"></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');
    icon.classList.toggle('bi-chevron-up');
    icon.classList.toggle('bi-chevron-down');
}
</script>
{% endblock %}
