<!-- BEGIN [% component.name %] -->
    [% # PROCESS dumper.tt dump = all_results %]
    [% # PROCESS dumper.tt dump = raw_lab_test_data.haematology_data %]

  [%
    results = {};
    FOREACH section IN all_results.keys.sort;
      FOREACH entry IN all_results.$section.test;
        # PROCESS dumper.tt dump = entry.value.result;
        NEXT UNLESS entry.value.result;
        # PROCESS dumper.tt dump = entry.value.result;
        results.$section.result.${entry.key} = entry.value.result;
      END;
    END; # PROCESS dumper.tt dump = results;
	FOREACH summary IN result_summaries;
		section = summary.key;
		text = symbolise(summary.value.results_summary) | html_line_break; # escapeHTML already done
		results.$section.summary = text;
	END; # PROCESS dumper.tt dump = results;

    # format haematology data:
	haem_data = raw_lab_test_data.haematology_data;
	IF haem_data; # INCLUDE dumper.tt dump = haem_data;
        BLOCK haem_format; # INCLUDE dumper.tt dump = var
            DEFAULT factor = 10; # unless supplied
            value = haem_data.$var; # INCLUDE dumper.tt dump = value;
            IF value.match('\*'); # ie *000
                result = value;
            ELSIF float; # needs formatting:
                result = FILTER format('%.' _ float _ 'f'); value / factor; END;
            ELSE;
                result = value / factor;
            END;
            flag  = var _ '_f'; # eg wbc_f
            class = ( value.match('\*') || haem_data.$flag ) ? 'red' : 'normal';
            %]<span class="[% class %]">[% result %]</span>[%
		END;
        fbc = {}; # initialize for blood results, hb, wbc, etc if verified, or status if not:
		IF haem_data.status.match('verified');
            fbc.wbc = INCLUDE haem_format(var = 'wbc', float = 1);
            fbc.rbc = INCLUDE haem_format(var = 'rbc', float = 2, factor = 100);
            fbc.hb  = INCLUDE haem_format(var = 'hb',  factor = 1); # ie int
            fbc.plt = INCLUDE haem_format(var = 'plt', factor = 1); # ie int
            fbc.hct = INCLUDE haem_format(var = 'hct', float = 1);
            fbc.mcv = INCLUDE haem_format(var = 'mcv', float = 1);
		ELSE;
			USE url(app_url _ '/result/=/' _  data.id);
            fbc.status = "<a href=\"$url\">pending validation</a>";
		END;
        results.Haematology.result = fbc;
	END;
  %]

  [% IF results.size %]
    <p class="title">Lab test results:
      [% div_name = 'tests'; INCLUDE site/snippets/toggleview.tt %]
    </p>

    <div id="[% div_name %]" class="itemshown">
      <div class="indent">
      [% FOREACH section IN results.keys.sort %]
		<div style="margin-bottom: 1em">
            <span class="title">[% section | html %]</span>
            [% IF results.$section.result.size; %]
                <div class="test-summary">
                  [% FOREACH test IN results.$section.result.keys.sort;
                    # PROCESS dumper.tt dump = results.$section.result.$test %]
                    <i>[% test %]:</i>
                    [% results.$section.result.$test %]
                    [% UNLESS loop.last %]<span class="indent"></span>[% END %]
                  [% END %]
                </div>
            [% END %]
            [% IF results.$section.summary %]
                <div class="test-summary">
                    <u>Summary</u>: [% results.$section.summary %]
                </div>
            [% END %]
		</div>
      [% END %]
      </div>
    </div>
  [% ELSE %]
      <p><span class="title">Lab test results</span>: none</p>
  [% END %]
<!-- END [% component.name %] -->
