<!-- BEGIN [% template.name %] -->
  [% META title = 'Worklist &raquo; Pre-screen' -%]

  [% # PROCESS dumper.tt dump = flow_screen_data %]
  [% # PROCESS dumper.tt dump = request_options_map %]
  [% # PROCESS dumper.tt dump = specimen_map %]
  [% # PROCESS dumper.tt dump = request_section_notes_map %]
  [% # PROCESS dumper.tt dump = unregistered_cases %]
  [% # PROCESS dumper.tt dump = unregistered_request_lab_tests %]
  [% # PROCESS dumper.tt dump = included_lab_tests %]
  [% # PROCESS dumper.tt dump = status_opts %]

  <h3 class="header">Pre-screen worklist</h3>

  [% # define some data structures:
    headers = [ 'LabNo' 'Sample' 'Surname' 'DoB' 'WBC' 'Lymph' 'Mixed' 'PCS'
	  'BLS' 'SCS' 'TLS' 'RIT' 'OUT' 'Date/Details'];

    lab_tests_map = {
      plasma_cell => 'PCS',
      b_lymphoid  => 'BLS',
      stem_cell   => 'SCS',
      t_lymphoid  => 'TLS',
      rituximab   => 'RIT',
      outreach    => 'OUT',
    };
    map_lab_tests = { # reverse of above
      PCS => 'plasma_cell',
      BLS => 'b_lymphoid',
      SCS => 'stem_cell',
      TLS => 't_lymphoid',
      RIT => 'rituximab',
      OUT => 'outreach',
    };

    col_names = [ 'PCS' 'BLS' 'SCS' 'TLS' 'RIT' 'OUT' ];
  %]

  [%
	BLOCK haematology; # param;
	  result = flow_screen_data.item(entry).haematology.item(param);
	  IF result.match('\*'); # ie *000 %]
		<span class="grey">[% result %]</span> [%
      ELSIF result; result / denominator | format('%.1f'); END;
	END
  %]

  [% lot_numbers = BLOCK # define lot numbers block for re-use %]
    <tr>
      <td colspan="3" class="borderless"></td>
      <td class="borderless">LOT No:</td>
      <td colspan="3" class="borderless"></td>
      [% FOREACH i IN [1 .. 6] %]<td></td>[% END %]
      <td colspan="3" class="borderless"></td>
    </tr>
  [% END %]

  [% IF flow_screen_data.size || unregistered_cases.size %]
  <form action="[% c.query.url(path_info=1) %]" method="post">
	<input type="hidden" name="function_name"
		value="[% c.query.param('function_name') %]" />
  <table class="indent">
    <tr class="borderless">
        <td class="bold" align="right">Restrict to cols:</td>
        <td>
          [% FOREACH col IN col_names; test_name = map_lab_tests.item(col) %]
            [% col %]<input type="checkbox" name="test_name" value="[% test_name %]"
              [% 'checked' IF included_lab_tests.item(test_name) %] />
            [% ' :: ' UNLESS loop.last %]
          [% END %]
        </td>
        <td rowspan="2" class="borderless">[% site.html.submit %]</td>
    </tr>
    <tr class="borderless">
        <td class="bold">Restrict to status:</td>
        <td>
          [% FOREACH opt IN status_opts;
            selected = ( opt.id == c.query.param('status_option_id') ) ? 1 : 0;
          %]
            <span class="[% selected ? 'highlight' : 'normal' %]">
                [% opt.description %]</span>
            <input type="radio" name="status_option_id" value="[% opt.id %]"
                [% 'checked' IF selected %]> ::
          [% END %]
            all (except analysed)<input type="radio" name="status_option_id" value=""
                [% 'checked' IF NOT c.query.param('status_option_id') # default %]>
        </td>
    </tr>
  </table>
  </form>

  <hr />

  <table class="indent">
    [% lot_numbers %]
    <tr>
      [% FOREACH item IN headers %]<th>[% item %]</th>[% END %]
    </tr>

    [% FOREACH entry IN flow_screen_data.keys.sort; # PROCESS dumper.tt dump = entry;
      import( flow_screen_data.item(entry) ); # PROCESS dumper.tt dump = lab_tests;
      # PROCESS dumper.tt dump = request_id;
      # PROCESS dumper.tt dump = lab_tests;
    %]
    <tr>
      <td>
        <a href="[% app_url %]/search/=/[% request_id %]">[% entry %]</a>
      </td>
      <td>
        [% specimen_map.item(request_id).sample_code.join(', ') %]
        [% INCLUDE site/snippets/doi.tt IF request_options_map.item(request_id).doi %]
      </td>
      <td>[% patient.last_name  | upper %]</td>
      <td>[% # empty DoB field %]</td>
      <td>[% INCLUDE haematology( param = 'wbc',   denominator = 10 ) %]</td>
      <td>[% INCLUDE haematology( param = 'lymph', denominator = 10 ) %]</td>
      <td>[% INCLUDE haematology( param = 'mixed', denominator = 10 ) %]</td>
      [% FOREACH col_name IN col_names; # BLS, TLC, PCS, etc %]
      <td align="center">
        [% FOREACH test IN lab_tests; # array of lab_test data from flow_screen_data
            abbreviated_test_name = lab_tests_map.item(test.name); # BLS, TLS, etc
            NEXT UNLESS abbreviated_test_name == col_name;
        %]
          [% IF test.status.match('new') %]x
          [% ELSE %]
			<span class="acronym" title="[% test.user.username | upper %]">
				<img src="/images/tick.gif" />
			</span>
          [% END %]
        [% END %]
      </td>
      [% END %]
      <td>
        [% request_section_notes_map.item(request_id) %]
      </td>
    </tr>
    [% END %]

    [% FOREACH entry IN unregistered_cases; # can have multiple specimens, or none:
        # PROCESS dumper.tt dump = entry;
        IF entry.pre_registration_specimen; # has had initial processing:
          FOREACH reg_specimen IN entry.pre_registration_specimen;
            PROCESS worklist/local/preregistration/specimen_row.tt;
          END;
        ELSE; # not yet had specimen(s) allocated:
          PROCESS worklist/local/preregistration/no_specimen_row.tt;
        END;
      END;
    %]

    [% lot_numbers %]
  </table>
  [% ELSE %]
	<p class="info indent">No outstanding pre-screen requests</p>
  [% END %]
<!-- END [% template.name %] -->
