	[% META title="Results" %]

<!-- BEGIN [% template.name %] -->
	[%
        patient = data.patient_case.patient;
        request_data = data.as_tree; # save multiple lookups if undefined
            # PROCESS dumper.tt dump =
            # request_data;
            # all_results;
            # result_summaries;
            # results_summary_opts;
            # data_map.requested_tests;
            # data_files;
            # c.stash.error_msg;
            # validation_failures;
            # raw_lab_test_data.haematology_data;
            # section_with_result_errors_id;
            # c.debug('######################'); # to reveal any db calls from templates
    %]

    [% # PROCESS site/js/jquery.tt # already loaded in html_wrapper %]
    <script type="text/javascript" src="/js/jquery.textarea-expander.js"></script>

    [% PROCESS record/content_header.tt %]

    [% IF c.is_lab_staff;
      patient_notes = patient.patient_note || outreach.demographics.patient_notes;
      INCLUDE site/previous_requests.tt; # don't use PROCESS or 'data' gets clobbered
	  INCLUDE site/previous_lab_tests.tt IF previous_requests.size;
    %]
	  <form method="post" name="patientNotes" id="patientNotes" action="[%
		app_url %]/patient/patient_notes/[% request_data.id %]/[% patient.id %]">
		  <p class="title">
			  Patient notes: <textarea name="patient_notes" rows="1" cols="70"
			  style="vertical-align: middle" class="expand20-200"
			  id="patient_notes">[% patient_notes.detail %]</textarea>
			  <input class="button" type="submit" value="Update" />
		  </p>
          <input type="hidden" name="fwd_to" value="[% c.query.url(path_info=1) %]" />
	  </form>
	[% END %]

    <p class="title">Report
	  [% div_name = 'lab_results'; INCLUDE site/snippets/toggleview.tt %]
    </p>

    <div id="[% div_name %]" class="itemhidden">
      [% PROCESS record/report.tt %]
    </div>

    <form action="[% app_url %]/request/general_notes/[% request_data.id %]"
          method="post" name="notes" id="notes">
        <input type="hidden" name="rm" value="result" />
      <p class="title">Request notes:
        [% notes = request_data.request_general_note.detail %]
        <textarea name="general_notes" rows="1" cols="70" class="expand"
          [% site.html.onfocus %] style="vertical-align: middle"
          [% IF is_locked %]readonly="readonly"[% END %]>[% notes %]</textarea>
        [% site.html.submit UNLESS is_locked %]
      </p>
    </form>

    <p class="section_header">Lab Investigations</p>

    [% haem_data = raw_lab_test_data.haematology_data %]
	[% IF haem_data AND
	  ( haem_data.status.match('verified') OR c.user_can('modify_results') ) %]
    <div class="indent">
      <div class="linkwithicon">
		[% div_name = 'HaemData';
		  INCLUDE site/snippets/toggleview.tt type = 'folder' %]

        <div class="aftericon" id="toggleHaemData">
          <span class="title">Haematology</span>
		  [% IF NOT haem_data.status.match('verified') %]
			<span class="red">[ UNVERIFIED ]</span>
		  [% END %]
        </div>
      </div>
      <div id="HaemData" class="itemhidden">
        [% INCLUDE result/haem_data.tt function = 'result'%]
      </div>
    </div>
    [% END %]

  [%
    # FOREACH section IN all_results.keys.sort; # doesn't handle Genomics section names
    nsorted_sections = natural_sort(all_results.keys); # PROCESS dumper.tt dump = nsorted_sections;
    FOREACH section IN nsorted_sections; # PROCESS dumper.tt dump = all_results.item(section);
        # do some data manipulations:
      this_section = all_results.item(section); # PROCESS dumper.tt dump = this_section;
      sectionName  = section.replace('\W', '_') | lower; # for result summary tmpls
      NEXT UNLESS this_section.is_active.match('yes');

      # get list of active (ie requested) lab_test field labels:
	    incomplete_tests = [ ];
      active_tests     = [ ]; # reset array for each section
      FOREACH entry IN this_section.panel; # PROCESS dumper.tt dump = entry;
        test_data = entry.value; # PROCESS dumper.tt dump = test_data;
        active_tests.push(entry.key) IF test_data.status; # been requested if defined status
			incomplete_tests.push(entry.key) IF test_data.status
				AND NOT test_data.status.match('complete');
	    END;

      # get list of resultable lab_test field labels:
      resultable_tests = [ ]; # reset array for each section
      FOREACH entry IN this_section.test; # PROCESS dumper.tt dump = entry;
        test_data = entry.value; # PROCESS dumper.tt dump = test_data;
        resultable_tests.push(test_data.field_label) IF test_data.has_results.match('yes');
        active_tests.push(entry.key) IF test_data.status; # been requested if defined status
				incomplete_tests.push(entry.key) IF test_data.status
					AND NOT test_data.status.match('complete');
      END;
        # PROCESS dumper.tt dump = active_tests;
        # PROCESS dumper.tt dump = resultable_tests;
        # PROCESS dumper.tt dump = incomplete_tests;
    %]

    <div class="indent">
      <div class="linkwithicon">
		[% div_name = sectionName;
		  INCLUDE site/snippets/toggleview.tt type = 'folder' %]

        <!-- BEGIN section title & investigations summary: -->
        [% PROCESS result/section_header.tt %]
        <!-- END section title & investigations summary -->
      </div>
            [% site.html.ie_span_fix %]
        [% toggle_class =
            section_with_result_errors_id.match('^' _ this_section.id _ '$')
                ? 'itemshow' : 'itemhidden' %]
      <div id="[% sectionName %]" class="[% toggle_class %]">
        [% # if section uses results import and not already got them:
          IF this_section.has_results_import.match('yes')
						AND NOT is_locked
            AND NOT result_summaries.item(section).results_summary; %]

        [% # load link to datafile if exists AND record screened:
          IF has_optional.initial_screen
            AND have_data_files(); # callback sub in C::Result::load() %]
          <p class="indent">
            [ <a href="[% app_url %]/result/preview_datafile/[% request_data.id %]">
              import results data</a> ]
          </p>
        [% END; END %]

        <p class="indent"><span class="title">Requests:</span>
          [% div_name = sectionName _ '_requests'; INCLUDE site/snippets/toggleview.tt %]
          [% IF c.user_can('modify_results') AND incomplete_tests.size;
              USE url(
                app_url _ '/worklist/display/' _ this_section.id,
                request_id = data.id,
                display_format='Data Entry');
          %]
            <a href="[% url %]" title="data entry worklist">
              <i style="vertical-align: text-top" class="fa fa-file-word-o"></i>
            </a>
          [% END %]
        </p>

        <div id="[% div_name %]" class="itemhidden">

        [% div_class = 'normal' # for panels.tt %]

        <div class="test_results">

        [% IF this_section.panel.size; # PROCESS dumper.tt dump = this_section.panel %]
            [%  div_class = 'indent' # override previous settings; for panels.tt %]
          <p class="indent"><span class="title">Investigations:</span></p>
        [% END %]

		[% UNLESS is_locked # can still view results section if locked %]
        <form action="[% app_url %]/result/update/[% request_data.id %]" method="post"
            name="[% sectionName %]_requests" id="[% sectionName %]_requests">
          <input type="hidden" name="_section_id" value="[% this_section.id %]" />
            [% # IF we_still_need_this - only used by remote section (foreign_id) %]
          <input type="hidden" name="_section_name" value="[% section %]" />
            [% # END %]
        [% END %]

          <!-- BEGIN panels -->
          [% PROCESS result/panels.tt IF this_section.panel.size %]
          <!-- END panels -->

          <!-- BEGIN single tests -->
          [% PROCESS result/single_tests.tt IF this_section.test.size %]
          <!-- END single tests -->

		  <!-- BEGIN remote system id -->
		  [% IF this_section.has_foreign_id.match('yes') %]
		  <p class="indent">
			<span class="title">Remote system ID</span>:
			<input type="text" name="foreign_id"
				value="[% remote_system_ids.item(section).foreign_id %]" />
		  </p>
		  [% END %]
		  <!-- END remote system id -->

          <!-- BEGIN section notes -->
          [% IF this_section.has_section_notes.match('yes') %]
            <p class="indent">
              <span class="title">Section notes:</span>
            </p>
            <blockquote>
              <p class="classname">
                [%
                  section_notes = lab_section_notes.item(this_section.id);
                  meta_data = c.get_meta_data('ScreenLabTestDetail');
                  max_length = meta_data.column('test_details').length;
                %]

                <textarea name="section_notes" rows="1" cols="60" class="expand20-200"
                  [% # PROCESS site/limit_text.tt input_name = 'section_notes' %]
                  >[% section_notes # DO NOT WRAP THIS %]</textarea>
                  <br />
                  <!-- <span class="indent">[% PROCESS site/countdown.tt %]</span> -->
              </p>
            </blockquote>
          [% END %]
          <!-- END section notes -->

		  [% UNLESS is_locked # can view results section if locked %]
          <div class="indent">
            <p class="indent">
              [% site.html.submit %]
              [% site.html.reset %]
            </p>
          </div>
        </form>
		  [% END # start form tag also blocked if record locked %]
        </div>
       </div>

        <!-- BEGIN resultable tests: -->
        [% IF this_section.has_result_summary.match('yes') OR resultable_tests.size %]
            [% PROCESS result/data_entry.tt %]
        [% END %]
        <!-- END resultable tests -->

      </div>
    </div>
  [% END %]
<!-- END [% template.name %] -->
