{% extends base_layout %} {% load i18n %} {% load static %} {% load math %} {% block title %}{{ complaint.reference_number }} - PX360{% endblock %} {% block extra_css %} {% endblock %} {% block content %}
{% if source_user %} {{ _("Back to My Complaints")}} {% else %} {% trans "Back to Complaints" %} {% endif %}

{{ complaint.title }}

{% if complaint.complaint_type == 'appreciation' %} {% trans "Appreciation" %} {% else %} {% trans "Complaint" %} {% endif %} {{ complaint.get_status_display }} {{ complaint.get_severity_display }}

{% trans "Ref #" %}: {{ complaint.reference_number }} | {% trans "Patient" %}: {{ complaint.patient.get_full_name }} ({% trans "MRN" %}: {{ complaint.patient.mrn }})

{% trans "Source" %}: {% if complaint.source %} {{ complaint.source.name_en }} {% if complaint.created_by %} {% if complaint.created_by.source_user_profile %} | {% trans "by" %} {{ complaint.created_by.get_full_name }} ({% trans "Source User" %}) {% else %} | {% trans "by" %} {{ complaint.created_by.get_full_name }} {% endif %} {% endif %} {% elif complaint.complaint_source_type == 'internal' %} {% trans "Internal" %} {% if complaint.created_by %} | {% trans "by" %} {{ complaint.created_by.get_full_name }} {% endif %} {% else %} {% trans "External (Patient)" %} {% if complaint.created_by %} | {% trans "by" %} {{ complaint.created_by.get_full_name }} {% endif %} {% endif %}

{% trans "Hospital" %}: {{ complaint.hospital.name_en }} {% if complaint.department %} | {% trans "Department" %}: {{ complaint.department.name_en }} {% endif %}

{% trans "SLA Deadline" %}

{{ complaint.due_at|date:"M d, Y H:i" }}

{% if complaint.is_overdue %}
{% trans "OVERDUE" %}
{% else %} {{ complaint.due_at|timeuntil }} {% trans "remaining" %} {% endif %}
{% trans "Complaint Details" %}
{% trans "Complaint Classification" %}
{% if can_edit %} {% endif %}
{% if complaint.domain %}
{% trans "Level 1 - Domain" %}:
{{ complaint.domain.name_en }} {% if complaint.domain.name_ar %} {{ complaint.domain.name_ar }} {% endif %}
{% else %}
{% trans "Level 1 - Domain" %}:
{% trans "Not classified" %}
{% endif %} {% if complaint.category %}
{% trans "Level 2 - Category" %}:
{{ complaint.category.name_en }} {% if complaint.category.name_ar %} {{ complaint.category.name_ar }} {% endif %}
{% else %}
{% trans "Level 2 - Category" %}:
{% trans "Not classified" %}
{% endif %} {% if complaint.subcategory_obj %}
{% trans "Level 3 - Subcategory" %}:
{{ complaint.subcategory_obj.name_en }} {% if complaint.subcategory_obj.name_ar %} {{ complaint.subcategory_obj.name_ar }} {% endif %}
{% elif complaint.subcategory %}
{% trans "Level 3 - Subcategory" %}:
{{ complaint.subcategory }}
{% else %}
{% trans "Level 3 - Subcategory" %}:
{% trans "Not classified" %}
{% endif %} {% if complaint.classification_obj %}
{% trans "Level 4 - Classification" %}:
{{ complaint.classification_obj.name_en }} {% if complaint.classification_obj.name_ar %} {{ complaint.classification_obj.name_ar }} {% endif %}
{% elif complaint.classification %}
{% trans "Level 4 - Classification" %}:
{{ complaint.classification }}
{% else %}
{% trans "Level 4 - Classification" %}:
{% trans "Not classified" %}
{% endif %}
{% if not complaint.patient %}
{% trans "Complainant Information" %}
{% trans "Name" %}:
{{ complaint.contact_name }}
{% trans "Mobile Number" %}:
{{ complaint.contact_phone }}
{% trans "Email" %}:
{% if complaint.contact_email %} {{ complaint.contact_email }} {% else %} {% trans "Not provided" %} {% endif %}
{% trans "Relation to Patient" %}:
{% if complaint.metadata.relation_to_patient %} {{ complaint.metadata.relation_to_patient }} {% else %} {% trans "Not specified" %} {% endif %}
{% trans "Patient Name" %}:
{% if complaint.metadata.patient_name %} {{ complaint.metadata.patient_name }} {% else %} {% trans "Not provided" %} {% endif %}
{% trans "National ID" %}:
{% if complaint.metadata.national_id %} {{ complaint.metadata.national_id }} {% else %} {% trans "Not provided" %} {% endif %}
{% trans "Incident Date" %}:
{% if complaint.metadata.incident_date %} {{ complaint.metadata.incident_date }} {% else %} {% trans "Not provided" %} {% endif %}
{% if complaint.metadata.location_name or complaint.metadata.main_section_name %}
{% trans "Location Information" %}
{% if complaint.metadata.location_name %}
{% trans "Location" %}:
{{ complaint.metadata.location_name }}
{% endif %} {% if complaint.metadata.main_section_name %}
{% trans "Main Section" %}:
{{ complaint.metadata.main_section_name }}
{% endif %} {% if complaint.metadata.subsection_name %}
{% trans "Subsection" %}:
{{ complaint.metadata.subsection_name }}
{% endif %}
{% endif %} {% if complaint.metadata.staff_name or complaint.metadata.expected_result %}
{% trans "Additional Information" %}
{% if complaint.metadata.staff_name %}
{% trans "Staff Name" %}:
{{ complaint.metadata.staff_name }}
{% endif %} {% if complaint.metadata.expected_result %}
{% trans "Expected Result" %}:
{{ complaint.metadata.expected_result }}
{% endif %}
{% endif %} {% endif %}
{% trans "Source" %}
{{ complaint.get_source_display }}
{% trans "Priority" %}
{{ complaint.get_priority_display }}
{% trans "Encounter ID" %}
{% if complaint.encounter_id %} {{ complaint.encounter_id }} {% else %} {% trans "N/A" %} {% endif %}
{% if complaint.department %}
{% trans "Department" %}
{{ complaint.department.name_en }} {% if complaint.department.name_ar %} ({{ complaint.department.name_ar }}) {% endif %}
{% endif %} {% if complaint.staff %}
{% trans "Staff Member" %}
{{ complaint.staff.first_name }} {{ complaint.staff.last_name }} {% if complaint.staff.first_name_ar or complaint.staff.last_name_ar %} ({{ complaint.staff.first_name_ar }} {{ complaint.staff.last_name_ar }}) {% endif %} {% if complaint.staff.job_title %}
{{ complaint.staff.job_title }}
{% endif %}
{% endif %}
{% trans "Description" %}

{{ complaint.description|linebreaks }}

{% if complaint.resolution %}
{% if complaint.status == 'partially_resolved' %} {% trans "Partial Resolution" %} {% else %} {% trans "Resolution" %} {% endif %}

{{ complaint.resolution|linebreaks }}

{% if complaint.status == 'partially_resolved' %} {% trans "Partially resolved by" %} {% else %} {% trans "Resolved by" %} {% endif %} {{ complaint.resolved_by.get_full_name }} {% trans "on" %} {{ complaint.resolved_at|date:"M d, Y H:i" }}
{% endif %}
{% trans "Created" %}
{{ complaint.created_at|date:"M d, Y H:i" }}
{% trans "Last Updated" %}
{{ complaint.updated_at|date:"M d, Y H:i" }}
{% trans "Activity Timeline" %}
{% if timeline %}
{% for update in timeline %}
{{ update.get_update_type_display }} {% if update.created_by %} by {{ update.created_by.get_full_name }} {% endif %}
{{ update.created_at|date:"M d, Y H:i" }}

{{ update.message }}

{% if update.old_status and update.new_status %}
{{ update.old_status }} {{ update.new_status }}
{% endif %}
{% endfor %}
{% else %}

{% trans "No timeline entries yet" %}

{% endif %}
{% trans "Attachments" %}
{% if attachments %}
{% for attachment in attachments %}
{{ attachment.filename }}
Uploaded by {{ attachment.uploaded_by.get_full_name }} on {{ attachment.created_at|date:"M d, Y H:i" }} ({{ attachment.file_size|filesizeformat }}) {% if attachment.description %}
{{ attachment.description }} {% endif %}
{% endfor %}
{% else %}

{% trans "No attachments" %}

{% endif %}
{% trans "Staff Explanations" %} {{ explanations|length }}
{% if explanations %} {% for exp in explanations %}
{% if exp.is_used %} {% if exp.metadata.is_escalation %} {% trans "Explanation from Manager (Escalated)" %} {% else %} {% trans "Explanation from Staff" %} {% endif %} {% else %} {% if exp.metadata.is_escalation %} {% trans "Pending Manager Explanation (Escalated)" %} {% else %} {% trans "Pending Staff Explanation" %} {% endif %} {% endif %}
{% if exp.is_used %} {% trans "Submitted" %} {% else %} {% trans "Pending" %} {% endif %}
{% if exp.staff %}

{% if exp.metadata.is_escalation %} {% trans "Manager:" %} {% else %} {% trans "Staff Member:" %} {% endif %} {{ exp.staff.get_full_name }} {% if exp.staff.job_title %} ({{ exp.staff.job_title }}) {% endif %} {% if exp.metadata.original_staff_name %}
{% trans "Regarding:" %} {{ exp.metadata.original_staff_name }} {% endif %}

{% endif %} {% if exp.is_used and exp.explanation %}

{{ exp.explanation|linebreaks }}

{% else %}

{% trans "No explanation submitted yet." %}

{% endif %}
{% if exp.responded_at %} {% trans "Responded:" %} {{ exp.responded_at|date:"M d, Y H:i" }} {% else %} {% trans "Requested:" %} {{ exp.email_sent_at|date:"M d, Y H:i" }} {% endif %} {% if exp.attachments.all %} | {{ exp.attachments.all.count }} {% trans "attachment(s)" %} {% endif %} {% if exp.attachments.all %}
{% trans "Attachments:" %}
{% for attachment in exp.attachments.all %} {{ attachment.filename }} {% endfor %}
{% endif %} {% if exp.request_message %}
{% trans "Request Message:" %}
{{ exp.request_message }}
{% endif %}
{% if exp.token and not exp.is_used %}
{% trans "Explanation ID:" %} {{ exp.id }} | {% trans "Token:" %} {{ exp.token|slice:":8" }}...
{% endif %}
{% endfor %} {% else %}

{% trans "No explanation has been submitted yet." %}

{% if can_edit %}
{% trans "Request Explanation" %}

{% trans "Send explanation link to the assigned staff member. Manager will be notified for awareness." %}

{% if complaint.staff %}
{% trans "Explanation link will be sent to:" %}
{{ complaint.staff.get_full_name }}
{% if complaint.staff.user %} {{ complaint.staff.user.email }} {% elif complaint.staff.email %} {{ complaint.staff.email }} {% else %} {% trans "No email configured" %} {% endif %}
{% if complaint.staff.report_to %}
{{ complaint.staff.report_to.get_full_name }} ({% trans "Manager - Notification Only" %})
{% if complaint.staff.report_to.user %} {{ complaint.staff.report_to.user.email }} {% elif complaint.staff.report_to.email %} {{ complaint.staff.report_to.email }} {% else %} {% trans "No email configured" %} {% endif %}
{% trans "Will receive link only if staff doesn't respond within SLA" %}
{% else %}
{% trans "No manager assigned to this staff member" %}
{% endif %}
{% else %}
{% trans "Please assign a staff member to this complaint before requesting an explanation." %}
{% endif %}
{% endif %}
{% endif %}
{% trans "Related PX Actions" %}
{% if px_actions %} {% for action in px_actions %}
{{ action.title }}

{{ action.description|truncatewords:20 }}

{{ action.get_status_display }} {{ action.get_priority_display }}
{% trans "View" %}
{% endfor %} {% else %}

{% trans "No PX actions created yet" %}

{% endif %}
{% trans "AI Analysis" %}
{% if complaint.metadata.ai_analysis.staff_matches %}
{% trans "AI Staff Matches" %}
{% if complaint.metadata.ai_analysis.needs_staff_review %} {% trans "Needs Review" %} {% endif %}
{% if complaint.metadata.ai_analysis.extracted_staff_names %}
{% trans "Extracted from Complaint" %}
{% for name in complaint.metadata.ai_analysis.extracted_staff_names %} {{ name }} {% endfor %}
{% if complaint.metadata.ai_analysis.primary_staff_name %} {% trans "Primary" %}: "{{ complaint.metadata.ai_analysis.primary_staff_name }}" {% endif %}
{% endif %}
{% for staff_match in complaint.metadata.ai_analysis.staff_matches|slice:":5" %}
{{ staff_match.name_en }} {% if staff_match.name_ar %} ({{ staff_match.name_ar }}) {% endif %} {% if complaint.staff and staff_match.id == complaint.staff.id|stringformat:"s" %} {% trans "Assigned" %} {% endif %}
{% if staff_match.job_title %}{{ staff_match.job_title }}{% endif %} {% if staff_match.specialization %}{{ staff_match.specialization }}{% endif %} {% if staff_match.department %}{{ staff_match.department }}{% endif %}
{{ staff_match.confidence|mul:100|floatformat:0 }}% {% trans "confidence" %}
{% if user.is_px_admin and not complaint.staff %} {% elif user.is_px_admin and complaint.staff and staff_match.id != complaint.staff.id|stringformat:"s" %} {% endif %}
{% endfor %}
{% if user.is_px_admin %}
{% endif %}
{% endif %} {% if complaint.short_description_ar or complaint.suggested_action_ar or complaint.emotion %}
{% trans "AI-Generated Insights" %} {% trans "AI Generated" %}
{% if complaint.emotion %}
{% trans "Emotion Analysis" %}
{{ complaint.get_emotion_display }}
{% trans "Confidence" %}: {{ complaint.emotion_confidence|mul:100|floatformat:0 }}%
{% trans "Intensity" %} {{ complaint.emotion_intensity|floatformat:2 }} / 1.0
{% if complaint.emotion == 'anger' %}
{% elif complaint.emotion == 'sadness' %}
{% elif complaint.emotion == 'confusion' %}
{% elif complaint.emotion == 'fear' %}
{% else %}
{% endif %}
{% endif %} {% if complaint.short_description_ar %}
{% trans "Summary" %}
{{ complaint.short_description_ar }}
{% endif %} {% if complaint.suggested_action_ar %}
{% trans "Suggested Action" %}
{{ complaint.suggested_action_ar }}
{% endif %}
{% else %}
{% trans "No AI analysis available for this complaint yet." %}
{% endif %}
{% trans "Resolution Management" %}
{% if complaint.status == 'resolved' or complaint.status == 'partially_resolved' or complaint.status == 'closed' %}
{% if complaint.status == 'partially_resolved' %} {% trans "Complaint Partially Resolved" %} {% else %} {% trans "Complaint Resolved" %} {% endif %}

{% trans "Resolution" %}:

{{ complaint.resolution|linebreaks }}

{% if complaint.resolution_category %}

{% trans "Resolution Category" %}: {{ complaint.get_resolution_category_display }}

{% endif %}
{% trans "Resolved by" %}: {{ complaint.resolved_by.get_full_name }} | {{ complaint.resolved_at|date:"M d, Y H:i" }}
{% if can_edit %}
{% trans "Re-open Complaint" %}

{% trans "This complaint has been resolved. You can re-open it if needed." %}

{% endif %} {% else %}
{% trans "Complaint Not Resolved" %}

{% trans "This complaint has not been resolved yet. Provide resolution details below to close this complaint." %}

{% if can_edit %}
{% trans "Resolve Complaint" %}
{% csrf_token %}
{% trans "Provide clear details about the resolution taken. This will be visible to the patient." %}
{% trans "The patient will receive an email with the resolution details." %}
{% else %}
{% trans "You do not have permission to resolve this complaint." %}
{% endif %} {% endif %}
{% trans "Resolution Guidelines" %}
  • {% trans "Be specific about the actions taken to resolve the complaint." %}
  • {% trans "Include any follow-up measures that will prevent similar issues." %}
  • {% trans "Maintain a professional and empathetic tone." %}
  • {% trans "Ensure the resolution aligns with organizational policies." %}
  • {% trans "Document any compensation or service recovery provided." %}
{% trans "PDF View" %}
{% trans "Download PDF" %}
{% trans "This will generate a professionally formatted PDF with all complaint details, including AI analysis, staff assignment, and resolution information." %}

{% trans "PDF Contents" %}
  • {% trans "Header:" %} Complaint title, ID, status, severity, patient info
  • {% trans "Basic Information:" %} Category, source, priority, encounter ID, dates
  • {% trans "Description:" %} Full complaint details
  • {% trans "Staff Assignment:" %} Assigned staff member (if any)
  • {% trans "AI Analysis:" %} Emotion analysis, summary, suggested action (if available)
  • {% trans "Resolution:" %} Resolution details (if resolved)
{% trans "Note" %}

{% trans "PDF generation requires WeasyPrint to be installed. If you see an error message, please contact your system administrator." %}

{% if user.is_px_admin %}
{% trans "Staff Assignment" %}
{% trans "Staff member that complaint is about" %} {% if complaint.staff %}
{{ complaint.staff.get_full_name }} {% if complaint.staff.job_title %}
{{ complaint.staff.job_title }} {% endif %}
{% else %} {% if complaint.metadata.ai_analysis.needs_staff_review %} {% trans "This complaint needs staff review" %} {% endif %} {% endif %}
{% endif %} {% if can_edit %}
{% trans "Quick Actions" %}
{% csrf_token %}
{% if complaint.complaint_type == 'appreciation' and not complaint.metadata.appreciation_id %}
{% elif complaint.metadata.appreciation_id %}
{% trans "Converted to Appreciation" %} {% trans "View Appreciation" %}
{% endif %}
{% endif %}
{% trans "Add Note" %}
{% csrf_token %}
{% trans "Assignment Info" %}
{% trans "Assigned To" %}
{% if complaint.assigned_to %}
{{ complaint.assigned_to.get_full_name }}
{% if complaint.assigned_to.is_px_admin %}{% trans "PX Admin" %}{% endif %} {% if complaint.assigned_to.is_hospital_admin %}{% trans "Hospital Admin" %}{% endif %}
Assigned {{ complaint.assigned_at|date:"M d, Y H:i" }}
{% else %}
{% if user.is_px_admin or user.is_hospital_admin %}

{% trans "No admin assigned to this complaint" %}

{% else %}

{% trans "No admin assigned to this complaint" %}

{% endif %}
{% endif %}
{% if complaint.resolved_by %}
{% trans "Resolved By" %}
{{ complaint.resolved_by.get_full_name }}
{{ complaint.resolved_at|date:"M d, Y H:i" }}
{% endif %} {% if complaint.closed_by %}
{% trans "Closed By" %}
{{ complaint.closed_by.get_full_name }}
{{ complaint.closed_at|date:"M d, Y H:i" }}
{% endif %}
{% if complaint.resolution_survey %}
{% trans "Resolution Survey" %}

{% trans "Status" %}: {{ complaint.resolution_survey.get_status_display }}

{% if complaint.resolution_survey.score %}

{% trans "Score" %}: {{ complaint.resolution_survey.score }}/100

{% endif %} {% trans "View Survey" %}
{% endif %}
{% endblock %}