{# ── 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 a rated field (score>0), mark ALL accumulated label IDs as visible
and clear the buffer. Never clear the buffer for non-rating fields — labels
can sit several rows above the ratings they introduce. #}
{% set lbuf = namespace(ids=[]) %}
{% for field in form_fields %}
{% if field.type == 'label' %}
{% set lbuf.ids = lbuf.ids + [field.id] %}
{% elif field.type == 'rating' %}
{% set score = form_data.get(field.id | string, '0') | int %}
{% if score > 0 %}
{% 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 %}
{% if remap.last_orig_row != -1 %}
{% set remap.out_row = remap.out_row + 1 %}
{% endif %}
{% set remap.last_orig_row = field.row %}
{% 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 %}
{# Flush pending section first #}
{% if remap.pending_sec is not none %}
{# Section: 1 row tall, spanning all 12 cols #}
{{ remap.pending_sec.label }}
{% set remap.out_row = remap.out_row + 1 %}
{% set remap.pending_sec = none %}
{% set remap.last_orig_row = -1 %}
{% endif %}
{# Advance out_row when the original row changes #}
{% if field.row != remap.last_orig_row and remap.last_orig_row != -1 %}
{% set remap.out_row = remap.out_row + 1 %}
{% endif %}
{% set remap.last_orig_row = field.row %}
{# 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 == 'checkbox' %}
{{ field.label }}
{% if val == 'yes' %} Yes
{% else %} No{% endif %}