<!-- BEGIN report/test_summary.tt -->
    [% # PROCESS dumper.tt dump = all_results %]
    [% # PROCESS dumper.tt dump = raw_lab_test_data.haematology_data %]
    
  [%
    results = {};
    haem_data = raw_lab_test_data.haematology_data;
    
    FOREACH section IN all_results.keys.sort;    
      FOREACH entry IN all_results.item(section).test;
        # PROCESS dumper.tt dump = entry.value.result;
      NEXT UNLESS entry.value.result;
        # PROCESS dumper.tt dump = entry.value.result;
      results.$section.${entry.key} = entry.value.result;
      END;
    END; # PROCESS dumper.tt dump = results
  %]

  [% IF results.size || haem_data.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 %]
        <p class="results">
          <span class="title">[% section | html %]</span>:
          [% FOREACH test IN results.item(section).keys.sort;
            # PROCESS dumper.tt dump = results.item(section).item(test) %]
            <i>[% test %]:</i>
            [% results.item(section).item(test) %]
            [% UNLESS loop.last %]<span style="margin-left: 2em"></span>[% END %]            
          [% END %]
        </p>
      [% END %]

      [% IF haem_data;
            BLOCK calculate;
                DEFAULT factor = 10; # unless supplied
                result = haem_data.item(var);
                flag = var _ '_f'; # eg wbc_f
                class = result.match('\*') || haem_data.item(flag)
                    ? 'red' : 'normal';
            %]
            [% var %]: <span class="[% class %]">
                [%  IF result.match('\*'); result; # ie *000
                    ELSE; 
                        IF float; # needs formatting:
                            FILTER format('%.' _ float _ 'f'); result / factor; END;
                        ELSE;
                            result / factor;
                        END;
                    END;
                %]
            </span>
            [% END %]
        <p class="results"><span class="title">Haematology</span>:
            [% IF haem_data.status == 'verified' %]
                [% INCLUDE calculate(var = 'wbc',  float = 1) %]|
                [% INCLUDE calculate(var = 'rbc',  float = 2, factor = 100) %]|
                [% INCLUDE calculate(var = 'hb',   float = 1)  %]|
                [% INCLUDE calculate(var = 'plt',  float = 0, factor = 1) %]|
                [% INCLUDE calculate(var = 'hct',  float = 1) %]|
                [% INCLUDE calculate(var = 'mcv',  float = 1) %]
            [% ELSE %]
                [ <a href="[% app_url %]/result/=/[% data.id %]">
                    awaiting validation </a> ]
            [% END %]
        </p>
      [% END %]
      </div>      
    </div>
  [% END %]
<!-- END report/test_summary.tt -->
