{% extends "base.html" %} {% block title %}Inspection #{{ inspection.id }} — Results{% endblock %} {% block extra_css %} {% endblock %} {% block content %}
{# Action bar #}
Back to Inspections
Export PDF {% if current_user.role not in ['customer'] %} Re-inspect {% endif %} {% if current_user.role in ['admin','director'] %} {% if not inspection.follow_up_required %} {% else %}
{% endif %}
{% endif %}
{# ── Follow-up required alert ── #} {% if inspection.follow_up_required %}
Follow-up Inspection Required {% if inspection.follow_up_note %}
{{ inspection.follow_up_note }}{% endif %}
{% endif %} {# ── Parent/child inspection links ── #} {% if inspection.parent %}
This is a re-inspection of Inspection #{{ inspection.parent.id }} ({{ inspection.parent.inspection_date.strftime('%Y-%m-%d') }}, score: {{ inspection.parent.overall_score|round(1) if inspection.parent.overall_score else 'N/A' }}%).
{% endif %} {% set followups = inspection.follow_ups.all() %} {% if followups %}
Follow-up inspection(s): {% for fu in followups %} #{{ fu.id }} ({{ fu.inspection_date.strftime('%Y-%m-%d') }}, score: {{ fu.overall_score|round(1) if fu.overall_score else 'N/A' }}%) {% if not loop.last %}, {% endif %} {% endfor %}
{% endif %} {# ── Score comparison card (re-inspections only) ── #} {% if comparison %}
Score Comparison vs. Inspection #{{ comparison.parent_id }} {{ comparison.parent_date.strftime('%Y-%m-%d') }} {# Overall delta badge #} {% if comparison.score_delta is not none %} {% if comparison.score_delta > 0 %} +{{ comparison.score_delta }}% {% elif comparison.score_delta < 0 %} {{ comparison.score_delta }}% {% else %} No change {% endif %} {% endif %} {# Score pills #} {{ comparison.parent_score|round(1) if comparison.parent_score else '—' }}% → {{ comparison.current_score|round(1) if comparison.current_score else '—' }}%
{% endif %} {# Header #}

{{ inspection.template.name }}

{{ inspection.facility.name }}{% if inspection.area %} · {{ inspection.area.name }}{% endif %}  ·  Inspector: {{ inspection.inspector.display_name }}
{% if inspection.scheduled_inspection_id %} Scheduled{% if inspection.scheduled_inspection %} · {{ inspection.scheduled_inspection.frequency_label }}{% endif %} {% endif %} {{ inspection.status|replace('_',' ')|title }} {% if inspection.overall_score is not none %}
{{ inspection.overall_score }}%
{% endif %}
{# Meta row #}
Start Date {{ inspection.inspection_date.strftime('%B %d, %Y %H:%M') }}
{% if inspection.completed_at %}
Completed {{ inspection.completed_at.strftime('%B %d, %Y %H:%M') }}
{% endif %}
Template {{ inspection.template.name }}
Frequency {{ inspection.template.frequency|title }}
{# ── Submission GPS (admin / director only) ──────────────────────────── #} {% if current_user.role in ['admin', 'director'] and inspection.submit_latitude and inspection.submit_longitude %} {% set _lat = inspection.submit_latitude | float %} {% set _lng = inspection.submit_longitude | float %}
Submission Location
{{ '%.6f' | format(_lat) }}, {{ '%.6f' | format(_lng) }} Open in Maps
{% endif %} {# ── Build the filtered field set ────────────────────────────────────── Strategy: 1. Find all grid rows that contain at least one rated rating field. 2. Collect every field whose grid row overlaps a rated row. 3. Also collect section fields that immediately precede a rated group. 4. Re-number rows sequentially (1-based) so there are no gaps. #} {% if form_fields %} {# Pass 1: find all rated rows AND which label IDs have a rated field after them #} {% set ns = namespace(rated_rows=[], visible_label_ids=[]) %} {# 1a: collect rows that have any answered/filled field (not just ratings) #} {% set _skip_types = ['label', 'section', 'button_submit', 'button_print', 'button_email'] %} {% for field in form_fields %} {% if field.type not in _skip_types %} {% set fval = form_data.get(field.id | string, '') %} {# A row is "visible" if: rating has score>0, OR any other field has a non-empty value #} {% if field.type == 'rating' %} {% set score = fval | int %} {% if score > 0 %} {% for r in range(field.row, field.row + field.rowSpan) %} {% if r not in ns.rated_rows %}{% set ns.rated_rows = ns.rated_rows + [r] %}{% endif %} {% endfor %} {% endif %} {% elif fval and fval != '' and fval != [] %} {% for r in range(field.row, field.row + field.rowSpan) %} {% if r not in ns.rated_rows %}{% set ns.rated_rows = ns.rated_rows + [r] %}{% endif %} {% endfor %} {% endif %} {% endif %} {% endfor %} {# 1b: walk fields in order; accumulate label IDs. When we hit any answered field (rating > 0, pass_fail answered, text filled, etc.), mark ALL accumulated label IDs as visible and clear the buffer. #} {% set lbuf = namespace(ids=[]) %} {% for field in form_fields %} {% if field.type == 'label' %} {% set lbuf.ids = lbuf.ids + [field.id] %} {% elif field.type not in _skip_types %} {% set fval = form_data.get(field.id | string, '') %} {% set answered = namespace(v=false) %} {% if field.type == 'rating' %} {% if fval | int > 0 %}{% set answered.v = true %}{% endif %} {% elif fval and fval != '' and fval != [] %} {% set answered.v = true %} {% endif %} {% if answered.v %} {% for lid in lbuf.ids %} {% if lid not in ns.visible_label_ids %} {% set ns.visible_label_ids = ns.visible_label_ids + [lid] %} {% endif %} {% endfor %} {% set lbuf.ids = [] %} {% endif %} {% endif %} {% endfor %} {# Pass 2: render — labels only if in visible_label_ids, data fields only if row overlaps rated_rows, sections always buffered. #} {% set remap = namespace(out_row=1, last_orig_row=-1, pending_sec=none) %}
{% for field in form_fields %} {% set ftype = field.type %} {# Track pending section #} {% if ftype == 'section' %} {% set remap.pending_sec = field %} {% elif ftype in ('button_submit', 'button_print', 'button_email') %} {# skip — action buttons not shown in read-only view #} {% elif ftype == 'label' %} {# Only show labels that are ancestors of rated fields #} {% if field.id in ns.visible_label_ids %} {# Advance row only when original row actually changes #} {% if field.row != remap.last_orig_row %} {% if remap.last_orig_row != -1 %}{% set remap.out_row = remap.out_row + 1 %}{% endif %} {# Flush pending section before the first field on this new row #} {% if remap.pending_sec is not none %}
{{ remap.pending_sec.label }}
{% set remap.out_row = remap.out_row + 1 %} {% set remap.pending_sec = none %} {% endif %} {% set remap.last_orig_row = field.row %} {% endif %} {% set fs_map = {'small':'0.72rem','normal':'0.82rem','large':'0.96rem','x-large':'1.1rem'} %}
{{ field.text_content or '' }}
{% endif %} {% else %} {# Check if this field overlaps any rated row #} {% set vis = namespace(show=false) %} {% for r in range(field.row, field.row + field.rowSpan) %} {% if r in ns.rated_rows %}{% set vis.show = true %}{% endif %} {% endfor %} {% if vis.show %} {# Advance row only when original row actually changes. Section flush is also guarded here so a label already placed on this row at the same out_row cannot be displaced. #} {% if field.row != remap.last_orig_row %} {% if remap.last_orig_row != -1 %}{% set remap.out_row = remap.out_row + 1 %}{% endif %} {% if remap.pending_sec is not none %}
{{ remap.pending_sec.label }}
{% set remap.out_row = remap.out_row + 1 %} {% set remap.pending_sec = none %} {% endif %} {% set remap.last_orig_row = field.row %} {% endif %} {# Render the field at its original col/colSpan but remapped row #}
{% set fid = field.id | string %} {% set val = form_data.get(fid, '') %} {% if ftype == 'rating' %} {{ field.label }}
{% set score = val | int %} {% if score > 0 %} {% for i in range(1,6) %}{{ '★' if i <= score else '☆' }}{% endfor %} {{ score }}/5 {% else %} Not rated {% endif %}
{% elif ftype == 'image' %} {{ field.label }}
{% if val %} {% else %} No photo {% endif %}
{% elif ftype == 'signature' %} {{ field.label }}
{% if val and val.startswith('data:') %} {% else %} No signature {% endif %}
{% elif ftype == 'pass_fail' %} {{ field.label }}
{% if val and val.lower() in ('pass','yes','ok','good','acceptable','compliant') %} {{ val }} {% elif val %} {{ val }} {% else %} Not answered {% endif %}
{% elif ftype == 'checkbox' %} {{ field.label }}
{% if val == 'yes' %} Yes {% else %} No{% endif %}
{% elif ftype == 'checkbox_group' %} {{ field.label }}
{% if val and val is iterable and val is not string %} {% for item in val %}{{ item }}{% endfor %} {% else %}None{% endif %}
{% elif ftype == 'table' %} {{ field.label }}
{% if val and val is iterable and val is not string %} {% for hdr in (field.col_headers or ['Col']) %}{% endfor %}{% for row in val %}{% for hdr in (field.col_headers or ['Col']) %}{% endfor %}{% endfor %}
{{ hdr }}
{{ row.get(hdr,'') }}
{% else %}No data{% endif %}
{% else %} {# text, number, date, email, radio, select, textarea #} {{ field.label }}
{{ val or '—' }}
{% endif %}
{# /fg-cell #} {% endif %} {% endif %} {% endfor %}
{# /form-grid #} {% if not ns.rated_rows %}

No rated fields in this inspection.

{% endif %} {% else %}

No form fields found for this template.

{% endif %} {# Issues #} {% if issues %}
Issues Logged ({{ issues|length }})
{% for issue in issues %} {% endfor %}
SeverityAreaDescriptionStatus
{{ issue.severity|title }} {{ issue.area.name }} {{ issue.description[:80] }}{% if issue.description|length > 80 %}…{% endif %} {{ issue.status|replace('_',' ')|title }} View
{% endif %}
{# ── Print-only sign-off block (hidden on screen, visible @media print) ── #} {# Media lightbox #}
Photo
{% endblock %} {% block extra_js %} {# ── Flag follow-up modal ── #} {% endblock %}