[% META title = 'PCR/Sequencing Worksheet' %]
<!-- BEGIN [% template.name %] -->

  [% # PROCESS dumper.tt dump = extraction_status %]
  [% # PROCESS dumper.tt dump = specimen_map %]
  [% # PROCESS dumper.tt dump = t.as_tree FOREACH t IN lab_tests %]

  <h2>[% worklist_function %] Worksheet Select</h2>

  <form method="get" action="[% app_url %]/local_worklist" name="pcr_worksheet"
		id="pcr_worksheet">
    <input type="hidden" name="function_name"
      value="[% c.query.param('function_name') %]" />
    <input type="hidden" name="worklist_function" value="[% worklist_function %]" />

  <div class="indent">
    [% IF NOT test_names.size %]
	  <p class="bold">Select one or more tests:</p>
		<div id="multi">
		  <div class="indent">
			<select multiple size="[% IF lab_tests.size > 8; 8; END %]"
				name="lab_test_id">
			[% FOREACH test IN lab_tests;
			  NEXT IF test.test_name.match('extraction'); # causes errs & don't need
			%]
			  <option value="[% test.id %]">[% test.field_label | html %]</option>
			[% END %]
			</select>
		  </div>
		</div>
		<p>[% site.html.reset %] [% site.html.submit %]</p>
	[% ELSE %]
	  <input type="hidden" name="print_worksheet" value="1" />
	  [% FOREACH id IN lab_test_ids %]
		<input type="hidden" name="lab_test_id"	value="[% id %]" />
	  [% END %]

      <p class="bold">Selected test(s):</p>
		<ul>[% FOREACH t IN test_names %]<li>[% t %]</li>[% END %]</ul>

	  [% IF requests.size %]
	  <table>
        <tr>
            <td colspan="8" align="right" class="borderless">
                <input type="button" name="CheckAll" value="Select all"
                  onClick="checkAll(document.pcr_worksheet.request_specimen)">
                <input type="button" name="UnCheckAll" value="Clear all"
                  onClick="uncheckAll(document.pcr_worksheet.request_specimen)">
            </td>
        </tr>
		<tr>
			<td class="borderless"></td>
			<th>HMDS ref</th>
			<th>Name</th>
			<th>Date</th>
			<th>Sample</th>
			<th>Screened</th>
            [% IF extraction_status.size # only relevant for PCR, not sequencing %]
                <th>Status</th>
            [% END %]
			<th>Select</th>
		</tr>
		[%
		  i = 0; # row counter
		  seen = {}; # to enable unique entries

		  FOREACH entry IN requests; # PROCESS dumper.tt dump = entry.as_tree;
			data = entry.request; # for lab_number.tt
			patient = data.patient_case.patient;
			extraction = extraction_status.item(data.id); # PROCESS dumper.tt dump = extraction;

			# only want unique requests, so skip if already seen:
 	        NEXT IF seen.${data.id};
			seen.${data.id} = 1; # for unique entries

            # want to split specimens into new line for each:
            specimens = specimen_map.item(data.id).sample_code;
            FOREACH specimen IN specimens;
		%]
		<tr>
			<td align="right">[% i = i + 1; i %]</td>
			<td>[% INCLUDE site/lab_number.tt want_url = 1
			  query_modifiers = [ '_return=1' ] %]</td>
			<td>
				[% patient.last_name | upper %],
				[% format_firstname(patient) || patient.first_name | ucfirst %]
			</td>
			<td>[% data.created_at.strftime('%d.%m.%Y') %]</td>
			<td>
                [% specimen %]
                [% INCLUDE site/snippets/doi.tt
                    IF request_options_map.item(data.id).doi %]
            </td>
			<td>[% data.request_initial_screen.screen.description %]</td>
            [% IF extraction_status.size # only relevant for PCR, not sequencing %]
                <td>
                  [%
                    enabled = 0; # reset default disabled
                    FOREACH e IN extraction; # PROCESS dumper.tt dump = e;
                      # should only be 1 extraction per test, but 1-2-many rel in db:
                      IF e.status.match('complete'); enabled = 1; END;
                  %]
                    <div>[%	e.test_name.remove('_extraction').upper; ' [';
                      e.status.replace('complete','extracted').upper; ']' %]
                    </div>
                  [% END %]
                </td>
            [% ELSE; enabled = 1; END # extraction always complete by PCR stage %]
			<td align="center">
			    [% INCLUDE site/lab_number.tt want_url = 0 %]
				<input type="checkbox" name="request_specimen"
                  value="[% data.id %]~[% specimen # request_specimen has no 'id' %]"
				  [% UNLESS enabled %]disabled="disabled"[% END %] />
			</td>
		</tr>
		[% END; END %]
		<tr>
		  <td colspan="2" class="borderless">[% site.html.back_button %]</td>
		  <td colspan="6" class="borderless" align="right">
		    [% site.html.submit %]
		  </td>
		</tr>
	  </table>
	[% ELSE %]
	  <p class="info">No requests for selected test(s).</p>
	  <p>[% site.html.back_button %]</p>
	[% END; END %]
  </div>
  </form>
 <!-- END [% template.name %] -->
