HH/templates/surveys/comment_list.html
2026-02-22 08:35:53 +03:00

447 lines
26 KiB
HTML

{% extends "layouts/base.html" %}
{% load i18n %}
{% block title %}{% trans "Survey Comments" %} - PX360{% endblock %}
{% block content %}
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8 py-8">
<!-- Page Header -->
<div class="mb-8">
<h2 class="text-3xl font-bold text-gray-800 mb-2 flex items-center gap-2">
<i data-lucide="message-square-quote" class="w-8 h-8 text-navy"></i>
{% trans "Survey Comments" %}
</h2>
<p class="text-gray-400">
{% trans "View all patient comments with AI-powered sentiment analysis" %}
</p>
</div>
<!-- Statistics Cards -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6 mb-8">
<div class="bg-white p-6 rounded-[2rem] shadow-sm border border-gray-50">
<div class="flex justify-between items-start mb-4">
<div class="bg-blue-100 p-3 rounded-full">
<i data-lucide="message-square-quote" class="text-blue-500 w-6 h-6"></i>
</div>
<span class="text-blue-500 text-sm font-bold bg-blue-50 px-2 py-1 rounded-lg">
{% trans "Total" %}
</span>
</div>
<div class="text-4xl font-bold mb-1">{{ stats.total }}</div>
<div class="text-gray-400 text-sm font-medium">{% trans "Comments" %}</div>
</div>
<div class="bg-white p-6 rounded-[2rem] shadow-sm border border-gray-50">
<div class="flex justify-between items-start mb-4">
<div class="bg-green-100 p-3 rounded-full">
<i data-lucide="smile" class="text-green-500 w-6 h-6"></i>
</div>
<span class="text-green-500 text-sm font-bold bg-green-50 px-2 py-1 rounded-lg">
{% trans "Positive" %}
</span>
</div>
<div class="text-4xl font-bold mb-1 text-green-600">{{ stats.positive }}</div>
<div class="text-gray-400 text-sm font-medium">{% trans "Sentiment" %}</div>
</div>
<div class="bg-white p-6 rounded-[2rem] shadow-sm border border-gray-50">
<div class="flex justify-between items-start mb-4">
<div class="bg-red-100 p-3 rounded-full">
<i data-lucide="frown" class="text-red-500 w-6 h-6"></i>
</div>
<span class="text-red-500 text-sm font-bold bg-red-50 px-2 py-1 rounded-lg">
{% trans "Negative" %}
</span>
</div>
<div class="text-4xl font-bold mb-1 text-red-600">{{ stats.negative }}</div>
<div class="text-gray-400 text-sm font-medium">{% trans "Sentiment" %}</div>
</div>
<div class="bg-white p-6 rounded-[2rem] shadow-sm border border-gray-50">
<div class="flex justify-between items-start mb-4">
<div class="bg-light p-3 rounded-full">
<i data-lucide="bot" class="text-navy w-6 h-6"></i>
</div>
<span class="text-navy text-sm font-bold bg-light px-2 py-1 rounded-lg">
{% trans "AI" %}
</span>
</div>
<div class="text-4xl font-bold mb-1 text-navy">{{ stats.analyzed }}</div>
<div class="text-gray-400 text-sm font-medium">{% trans "Analyzed" %}</div>
</div>
</div>
<!-- Charts -->
<div class="grid grid-cols-1 lg:grid-cols-2 gap-6 mb-8">
<div class="bg-white rounded-2xl shadow-sm border border-gray-50 p-6">
<h3 class="font-bold text-gray-800 mb-4 flex items-center gap-2">
<i data-lucide="pie-chart" class="w-5 h-5 text-navy"></i>
{% trans "Patient Type Distribution" %}
</h3>
<div id="patientTypeDistributionChart" style="min-height: 300px;"></div>
</div>
<div class="bg-white rounded-2xl shadow-sm border border-gray-50 p-6">
<h3 class="font-bold text-gray-800 mb-4 flex items-center gap-2">
<i data-lucide="bar-chart-3" class="w-5 h-5 text-navy"></i>
{% trans "Sentiment by Patient Type" %}
</h3>
<div id="sentimentByPatientTypeChart" style="min-height: 300px;"></div>
</div>
</div>
<!-- Filters -->
<div class="bg-white rounded-2xl shadow-sm border border-gray-50 p-6 mb-6">
<form method="get" class="space-y-4">
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 xl:grid-cols-7 gap-4">
<!-- Search -->
<div class="xl:col-span-2">
<label for="search" class="block text-sm font-bold text-gray-700 mb-2">{% trans "Search" %}</label>
<div class="relative">
<i data-lucide="search" class="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400"></i>
<input type="text" class="w-full pl-10 pr-4 py-2.5 border-2 border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-navy focus:border-transparent transition"
id="search" name="search"
placeholder="{% trans 'MRN, name, or comment...' %}"
value="{{ filters.search }}">
</div>
</div>
<!-- Sentiment -->
<div>
<label for="sentiment" class="block text-sm font-bold text-gray-700 mb-2">{% trans "Sentiment" %}</label>
<select class="w-full px-4 py-2.5 border-2 border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-navy focus:border-transparent transition"
id="sentiment" name="sentiment">
<option value="">{% trans "All" %}</option>
<option value="positive" {% if filters.sentiment == 'positive' %}selected{% endif %}>{% trans "Positive" %}</option>
<option value="negative" {% if filters.sentiment == 'negative' %}selected{% endif %}>{% trans "Negative" %}</option>
<option value="neutral" {% if filters.sentiment == 'neutral' %}selected{% endif %}>{% trans "Neutral" %}</option>
</select>
</div>
<!-- Survey Type -->
<div>
<label for="survey_type" class="block text-sm font-bold text-gray-700 mb-2">{% trans "Survey Type" %}</label>
<select class="w-full px-4 py-2.5 border-2 border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-navy focus:border-transparent transition"
id="survey_type" name="survey_type">
<option value="">{% trans "All" %}</option>
<option value="stage" {% if filters.survey_type == 'stage' %}selected{% endif %}>{% trans "Journey Stage" %}</option>
<option value="complaint_resolution" {% if filters.survey_type == 'complaint_resolution' %}selected{% endif %}>{% trans "Complaint Resolution" %}</option>
<option value="general" {% if filters.survey_type == 'general' %}selected{% endif %}>{% trans "General" %}</option>
<option value="nps" {% if filters.survey_type == 'nps' %}selected{% endif %}>{% trans "NPS" %}</option>
</select>
</div>
<!-- Hospital -->
<div>
<label for="hospital" class="block text-sm font-bold text-gray-700 mb-2">{% trans "Hospital" %}</label>
<select class="w-full px-4 py-2.5 border-2 border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-navy focus:border-transparent transition"
id="hospital" name="hospital">
<option value="">{% trans "All" %}</option>
{% for hospital in hospitals %}
<option value="{{ hospital.id }}"
{% if filters.hospital == hospital.id|stringformat:"s" %}selected{% endif %}>
{{ hospital.name }}
</option>
{% endfor %}
</select>
</div>
<!-- Patient Type -->
<div>
<label for="patient_type" class="block text-sm font-bold text-gray-700 mb-2">{% trans "Patient Type" %}</label>
<select class="w-full px-4 py-2.5 border-2 border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-navy focus:border-transparent transition"
id="patient_type" name="patient_type">
<option value="">{% trans "All" %}</option>
<option value="outpatient" {% if filters.patient_type == 'outpatient' %}selected{% endif %}>{% trans "Outpatient" %}</option>
<option value="inpatient" {% if filters.patient_type == 'inpatient' %}selected{% endif %}>{% trans "Inpatient" %}</option>
<option value="emergency" {% if filters.patient_type == 'emergency' %}selected{% endif %}>{% trans "Emergency" %}</option>
</select>
</div>
<!-- Date Range -->
<div class="xl:col-span-1">
<label class="block text-sm font-bold text-gray-700 mb-2">{% trans "Date Range" %}</label>
<div class="flex gap-2">
<input type="date" class="flex-1 px-3 py-2.5 border-2 border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-navy focus:border-transparent transition text-sm"
id="date_from" name="date_from" value="{{ filters.date_from }}">
<input type="date" class="flex-1 px-3 py-2.5 border-2 border-gray-200 rounded-xl text-gray-800 focus:ring-2 focus:ring-navy focus:border-transparent transition text-sm"
id="date_to" name="date_to" value="{{ filters.date_to }}">
</div>
</div>
</div>
<div class="flex justify-between items-center pt-4 border-t border-gray-100">
<button type="submit" class="bg-navy text-white px-6 py-2.5 rounded-xl font-semibold hover:bg-navy transition flex items-center gap-2">
<i data-lucide="search" class="w-4 h-4"></i> {% trans "Search" %}
</button>
<a href="{% url 'surveys:survey_comments_list' %}" class="text-gray-500 hover:text-navy font-medium flex items-center gap-1">
<i data-lucide="x-circle" class="w-4 h-4"></i> {% trans "Clear Filters" %}
</a>
</div>
</form>
</div>
<!-- Comments List -->
<div class="bg-white rounded-2xl shadow-sm border border-gray-50 overflow-hidden">
{% if page_obj.object_list %}
<div class="overflow-x-auto">
<table class="w-full">
<thead class="bg-gray-50">
<tr>
<th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Patient" %}</th>
<th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Type" %}</th>
<th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Comment" %}</th>
<th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Sentiment" %}</th>
<th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Survey" %}</th>
<th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Date" %}</th>
<th class="px-6 py-4 text-left text-xs font-bold text-gray-500 uppercase tracking-wider">{% trans "Actions" %}</th>
</tr>
</thead>
<tbody class="divide-y divide-gray-100">
{% for survey in page_obj.object_list %}
<tr class="hover:bg-gray-50 transition">
<td class="px-6 py-4">
<div class="font-semibold text-gray-800">{{ survey.patient.get_full_name }}</div>
<div class="text-sm text-gray-400">{{ survey.patient.mrn }}</div>
{% if survey.survey_template.hospital %}
<div class="text-xs text-gray-400 mt-1">{{ survey.survey_template.hospital.name }}</div>
{% endif %}
</td>
<td class="px-6 py-4">
{% if survey.patient_type and survey.patient_type.code %}
<span class="inline-flex items-center gap-1 px-3 py-1 rounded-full text-xs font-bold {{ survey.patient_type.bg_class }} {{ survey.patient_type.text_class }}">
<i data-lucide="{{ survey.patient_type.icon }}" class="w-3 h-3"></i>
{{ survey.patient_type.label }}
</span>
{% else %}
<span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-600">N/A</span>
{% endif %}
</td>
<td class="px-6 py-4">
<div class="max-w-md overflow-hidden text-ellipsis whitespace-nowrap text-gray-700" title="{{ survey.comment }}">
{{ survey.comment }}
</div>
{% if survey.comment_analyzed %}
{% if survey.comment_analysis.emotion %}
<div class="text-xs text-gray-500 mt-1 flex items-center gap-1">
<i data-lucide="heart-pulse" class="w-3 h-3"></i> {{ survey.comment_analysis.emotion|title }}
</div>
{% endif %}
{% else %}
<div class="text-xs text-amber-500 mt-1 flex items-center gap-1">
<i data-lucide="hourglass" class="w-3 h-3"></i> {% trans "Analyzing..." %}
</div>
{% endif %}
</td>
<td class="px-6 py-4">
{% if survey.comment_analyzed and survey.comment_analysis.sentiment %}
<span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-bold {% if survey.comment_analysis.sentiment == 'positive' %}bg-green-100 text-green-700{% elif survey.comment_analysis.sentiment == 'negative' %}bg-red-100 text-red-700{% else %}bg-gray-100 text-gray-700{% endif %}">
{{ survey.comment_analysis.sentiment|title }}
</span>
{% else %}
<span class="inline-flex items-center px-3 py-1 rounded-full text-xs font-medium bg-gray-100 text-gray-600">{% trans "Pending" %}</span>
{% endif %}
</td>
<td class="px-6 py-4">
<div class="font-semibold text-gray-800 text-sm">{{ survey.survey_template.name }}</div>
<div class="text-xs text-gray-400">{{ survey.survey_template.get_survey_type_display }}</div>
{% if survey.total_score %}
<div class="text-xs {% if survey.is_negative %}text-red-600{% else %}text-green-600{% endif %} mt-1">
<strong>Score: {{ survey.total_score|floatformat:1 }}/5.0</strong>
</div>
{% endif %}
</td>
<td class="px-6 py-4">
<div class="text-sm text-gray-800">{{ survey.completed_at|date:"M d, Y" }}</div>
<div class="text-xs text-gray-400">{{ survey.completed_at|time:"H:i" }}</div>
</td>
<td class="px-6 py-4">
<a href="{% url 'surveys:instance_detail' survey.id %}"
class="inline-flex items-center gap-1 px-3 py-2 text-navy bg-light rounded-lg hover:bg-light transition font-medium text-sm">
<i data-lucide="eye" class="w-4 h-4"></i> {% trans "View" %}
</a>
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<!-- Pagination -->
{% if page_obj.has_other_pages %}
<div class="px-6 py-4 border-t border-gray-100">
<div class="flex items-center justify-between">
<div class="text-sm text-gray-500">
{% trans "Showing" %} <span class="font-medium text-gray-900">{{ page_obj.start_index }}</span>
{% trans "to" %} <span class="font-medium text-gray-900">{{ page_obj.end_index }}</span>
{% trans "of" %} <span class="font-medium text-gray-900">{{ page_obj.paginator.count }}</span>
{% trans "results" %}
</div>
<nav class="flex gap-2">
{% if page_obj.has_previous %}
<a href="?page=1{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.sentiment %}&sentiment={{ filters.sentiment }}{% endif %}{% if filters.survey_type %}&survey_type={{ filters.survey_type }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}{% if filters.date_from %}&date_from={{ filters.date_from }}{% endif %}{% if filters.date_to %}&date_to={{ filters.date_to }}{% endif %}"
class="px-3 py-2 text-gray-500 hover:text-navy hover:bg-light rounded-lg transition">
<i data-lucide="chevrons-left" class="w-5 h-5"></i>
</a>
<a href="?page={{ page_obj.previous_page_number }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.sentiment %}&sentiment={{ filters.sentiment }}{% endif %}{% if filters.survey_type %}&survey_type={{ filters.survey_type }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}{% if filters.date_from %}&date_from={{ filters.date_from }}{% endif %}{% if filters.date_to %}&date_to={{ filters.date_to }}{% endif %}"
class="px-3 py-2 text-gray-500 hover:text-navy hover:bg-light rounded-lg transition">
<i data-lucide="chevron-left" class="w-5 h-5"></i>
</a>
{% endif %}
<div class="px-4 py-2 bg-light text-navy rounded-lg font-medium">
<span class="text-gray-500">{% trans "Page" %}</span> {{ page_obj.number }} <span class="text-gray-500">{% trans "of" %}</span> {{ page_obj.paginator.num_pages }}
</div>
{% if page_obj.has_next %}
<a href="?page={{ page_obj.next_page_number }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.sentiment %}&sentiment={{ filters.sentiment }}{% endif %}{% if filters.survey_type %}&survey_type={{ filters.survey_type }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}{% if filters.date_from %}&date_from={{ filters.date_from }}{% endif %}{% if filters.date_to %}&date_to={{ filters.date_to }}{% endif %}"
class="px-3 py-2 text-gray-500 hover:text-navy hover:bg-light rounded-lg transition">
<i data-lucide="chevron-right" class="w-5 h-5"></i>
</a>
<a href="?page={{ page_obj.paginator.num_pages }}{% if filters.search %}&search={{ filters.search }}{% endif %}{% if filters.sentiment %}&sentiment={{ filters.sentiment }}{% endif %}{% if filters.survey_type %}&survey_type={{ filters.survey_type }}{% endif %}{% if filters.hospital %}&hospital={{ filters.hospital }}{% endif %}{% if filters.date_from %}&date_from={{ filters.date_from }}{% endif %}{% if filters.date_to %}&date_to={{ filters.date_to }}{% endif %}"
class="px-3 py-2 text-gray-500 hover:text-navy hover:bg-light rounded-lg transition">
<i data-lucide="chevrons-right" class="w-5 h-5"></i>
</a>
{% endif %}
</nav>
</div>
</div>
{% endif %}
{% else %}
<div class="text-center py-16">
<div class="inline-flex items-center justify-center w-20 h-20 bg-gray-100 rounded-full mb-4">
<i data-lucide="message-square-quote" class="w-10 h-10 text-gray-400"></i>
</div>
<h3 class="text-xl font-bold text-gray-800 mb-2">{% trans "No Comments Found" %}</h3>
<p class="text-gray-500 mb-4">{% trans "No survey comments match your current filters." %}</p>
{% if filters.search or filters.sentiment or filters.survey_type or filters.hospital or filters.date_from or filters.date_to %}
<a href="{% url 'surveys:survey_comments_list' %}" class="inline-flex items-center gap-2 text-navy hover:text-navy font-medium">
<i data-lucide="x-circle" class="w-4 h-4"></i> {% trans "Clear Filters" %}
</a>
{% endif %}
</div>
{% endif %}
</div>
</div>
<!-- Chart Scripts -->
<script>
document.addEventListener('DOMContentLoaded', function() {
lucide.createIcons();
// Patient Type Distribution Chart (Donut Chart)
const patientTypeDistributionData = {{ patient_type_distribution_json|safe }};
if (patientTypeDistributionData && patientTypeDistributionData.length > 0) {
const patientTypeLabels = patientTypeDistributionData.map(item => item.label);
const patientTypeCounts = patientTypeDistributionData.map(item => item.count);
const patientTypeColors = ['#3b82f6', '#f59e0b', '#ef4444', '#6b7280'];
const patientTypeChartOptions = {
series: patientTypeCounts,
labels: patientTypeLabels,
colors: patientTypeColors,
chart: {
type: 'donut',
height: 300,
fontFamily: 'Inter, sans-serif'
},
plotOptions: {
pie: {
donut: {
size: '65%',
labels: {
show: true,
total: {
show: true,
showAlways: true,
label: 'Total',
formatter: function (w) {
return w.globals.seriesTotals.reduce((a, b) => a + b, 0)
}
}
}
}
}
},
dataLabels: {
enabled: true,
formatter: function(val, opts) {
return patientTypeDistributionData[opts.seriesIndex].percentage + '%'
}
},
tooltip: {
y: {
formatter: function(value, { series, seriesIndex, dataPointIndex, w }) {
return value + ' comments (' + patientTypeDistributionData[seriesIndex].percentage + '%)'
}
}
},
legend: {
position: 'bottom',
fontSize: '13px',
fontFamily: 'Inter, sans-serif'
}
};
const patientTypeChart = new ApexCharts(document.querySelector("#patientTypeDistributionChart"), patientTypeChartOptions);
patientTypeChart.render();
}
// Sentiment by Patient Type Chart (Stacked Bar Chart)
const sentimentByPatientTypeData = {{ sentiment_by_patient_type_json|safe }};
if (sentimentByPatientTypeData && sentimentByPatientTypeData.types && sentimentByPatientTypeData.types.length > 0) {
const sentimentChartOptions = {
series: [
{ name: 'Positive', data: sentimentByPatientTypeData.positive },
{ name: 'Negative', data: sentimentByPatientTypeData.negative },
{ name: 'Neutral', data: sentimentByPatientTypeData.neutral }
],
chart: {
type: 'bar',
height: 300,
stacked: true,
fontFamily: 'Inter, sans-serif',
toolbar: { show: false }
},
plotOptions: {
bar: {
horizontal: false,
borderRadius: 4,
dataLabels: { total: { enabled: true, style: { fontSize: '13px', fontWeight: 900 } } }
}
},
xaxis: {
categories: sentimentByPatientTypeData.types,
labels: { style: { fontSize: '12px', fontFamily: 'Inter, sans-serif' } }
},
yaxis: {
title: { text: 'Number of Comments' },
labels: { style: { fontSize: '12px', fontFamily: 'Inter, sans-serif' } }
},
legend: {
position: 'bottom',
fontSize: '13px',
fontFamily: 'Inter, sans-serif'
},
fill: { opacity: 1 },
colors: ['#10b981', '#ef4444', '#6b7280'],
dataLabels: { enabled: false },
tooltip: {
y: {
formatter: function (value, { series, seriesIndex, dataPointIndex, w }) {
return value + ' comments'
}
}
}
};
const sentimentChart = new ApexCharts(document.querySelector("#sentimentByPatientTypeChart"), sentimentChartOptions);
sentimentChart.render();
}
});
</script>
{% endblock %}