[%
  # META title = '' # using my.meta_title to allow processing dynamic vars:
  status_query = c.query.param('status_query').replace('_', ' ') | ucfirst;
  my.meta_title = "Worklist &raquo; Request Status &raquo; ${status_query}"
-%]

<!-- BEGIN [% template.name %] -->
    [% # PROCESS dumper.tt dump = request_options_map %]
    [% # PROCESS dumper.tt dump = incomplete_lab_tests %]

    [%
	  status_query = c.query.param('status_query');
	  duration = c.query.param('duration') || c.cfg('settings').unreported_duration;
	  filter = c.query.param('filter'); # INCLUDE dumper.tt dump = filter;
	%]

  [% class = ( ! filter OR filter.match('all') ) ? 'normal' : 'grey' %]
  <h3 class="header">Request status - [% status_query.replace('_', ' ') %] cases
	[<span class="[% class %]">[% requests.size %]</span>]</h3>

  <div class="indent">
    [% IF status_query.match('unreported') # only customising unreported so far %]
      <form action="[% c.query.url(path_info=1) %]" method="get">
        <input type="hidden" name="status_query" value="[% status_query %]" />
        <p class="info">Cut-off (days):
		  <input type="textbox" size="2" name="duration" value="[% duration %]" />
          [% site.html.go_btn %]
        </p>
      </form>
    [% END %]

	[% IF requests.size AND c.cfg('settings').use_specimen_filter;
		INCLUDE worklist/snippets/specimen_filter.tt; END %]
	[% IF requests.size AND c.cfg('settings').use_location_filter;
		INCLUDE worklist/snippets/location_filter.tt; END %]

    <table>
      <tr>
        <td></td>
        <th>Lab No</th>
        <th>Name</th>
        <th>Sample</th>
        <th>Source</th>
        [% UNLESS status_query == 'unscreened' %]
            <th>Presentation</th>
          [% IF incomplete_lab_tests %]
            <th>Incomplete tests</th>
          [% END %]
        [% END %]
		<th>Registered</th>
      </tr>

      [% FOREACH data IN requests; # PROCESS dumper.tt dump = data.as_tree
		  patient = data.patient_case.patient;

		  IF filter;
			  specimens = specimen_map.item(data.id).sample_code; # array
				# INCLUDE dumper.tt dump = specimens;
			  tissue = '[DGLRTX](A|[BS][LP]|F|U)'; # ? BMAT
			  liquid = 'PB|[CE]F|CHI|([(BM)LX]A)$'; # PB,CE,EF,CHI,BMA,LA,XA
			IF filter.match('^cmp');
			  NEXT UNLESS specimens.grep('CMP').size;
			ELSIF filter.match('not_cmp');
			  NEXT IF specimens.grep('CMP').size;
			ELSIF filter.match('liquid$');
			  NEXT UNLESS specimens.grep(liquid).size
				OR specimens.grep('BMAT').size;
			ELSIF filter.match('liquid_only');
			  NEXT UNLESS specimens.grep(liquid).size;
			  NEXT IF specimens.grep(tissue).size;
			  NEXT IF specimens.grep('BMAT|CMP').size;
			ELSIF filter.match('tissue$');
			  NEXT UNLESS specimens.grep(tissue).size
				OR specimens.grep('BMAT').size;
			ELSIF filter.match('tissue_only');
			  NEXT UNLESS specimens.grep(tissue).size;
			  NEXT IF specimens.grep(liquid).size;
			ELSIF filter.match('with_incomplete_tests');
			  NEXT UNLESS incomplete_lab_tests.${data.id}.size;
			ELSIF filter.match('no_incomplete_tests');
			  NEXT IF incomplete_lab_tests.${data.id}.size;
			END;
		  END;
          IF c.query.param('location_filter');
            selected_location = c.query.param('location_filter');
            referral_source   = data.patient_case.referral_source.display_name;
            NEXT UNLESS selected_location.match('^all$')
                OR referral_source.match("^${selected_location}$");
          END;
	  %]
      <tr>
        <td>[% i = ( i || 0 ) + 1; i %]</td>
        <td>
          [% PROCESS site/lab_number.tt want_url = 1; # expects request object 'data' %]
        </td>
        [% class = request_options_map.item(data.id).urgent ? 'red' : 'normal' %]
        <td class="[% class %]">
          [% patient.last_name | upper %],
          [% format_firstname(patient) || patient.first_name | ucfirst %]
          [% patient.middle_name | ucfirst %]
        </td>
        <td>
		  [% specimen_map.item(data.id).sample_code.join(', ') %]
          [% INCLUDE site/snippets/doi.tt IF request_options_map.item(data.id).doi %]
		</td>
		<td>
		    [% SET location = data.patient_case.referral_source.display_name %]
            [% PROCESS site/display_name.tt %]
		</td>
        [% UNLESS status_query == 'unscreened' %]
          <td>
		    [% IF data.request_initial_screen;
			  data.request_initial_screen.screen.description;
			ELSE; site.html.grey_null; END; %]
		  </td>
          [% IF incomplete_lab_tests; tests = incomplete_lab_tests.${data.id} %]
            <td>
                [% IF tests; FOREACH t IN tests %]
                    <div class="no-wrap">
                        <span class="acronym" title="[% t.section %]">[% t.test %]</span>
                    </div>
                    [% END %]
                [% ELSE %]
                    <span class="grey">[none]</span>
                [% END %]
            </td>
          [% END %]
        [% END %]
        <td>
		  [%
			delta = duration_map.${data.id};
			class =
			  delta > 5 ? 'add' : # .red without bold text
			  delta > 2 ? 'orange' : 'green';
		  %]
		  <div class="no-wrap">[% data.created_at.strftime('%d.%b.%Y') %]
			<span class="printHide">[<span class="[% class %]">[% delta %]</span>]</span>
		  </div>
		</td>
      </tr>
      [% END %]
    </table>
  </div>

  <p>[% site.html.back_button %]</p>
<!-- END [% template.name %] -->
