	[% META title="Results" %]

<!-- [% template.name %] -->
    [% PROCESS record/patient_summary.tt %]

  [% # data structures passed in:
    # PROCESS dumper.tt dump =
        # all_results;
        # result_summaries;
        # results_summary_opts;
        # data_map.requested_tests;
        # data_files;
        # raw_lab_test_data.haematology_data;
        # c.debug('######################'); # to reveal any db calls from templates
        request_data = data.as_tree; # save multiple lookups if undefined
    %]
    
  [% # PROCESS site/js/jquery.tt # already loaded in html_wrapper %]
  <script type="text/javascript" src="/js/jquery.textarea-expander.js"></script>

    <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 %]/result/notes/[% request_data.id %]" method="post"
        name="notes" id="notes">
      <p class="title">General notes:
        [% notes = request_data.request_general_note.detail %]
        <textarea name="general_notes" rows="1" cols="70" class="expand"
          [% site.html.onfocus %] style="vertical-align: text-top"
          [% 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>
        </div>
      </div>
      <div id="HaemData" class="itemhidden">
        [% INCLUDE result/haem_data.tt function = 'result'%]
      </div>
    </div>
    [% END %]

  [% FOREACH section IN all_results.keys.sort; # PROCESS dumper.tt dump = section %]
  
    [% # do some data manipulations:
        # PROCESS dumper.tt dump = all_results.item(section);
      this_section = all_results.item(section); # PROCESS dumper.tt dump = this_section;
      
      sectionName = section.replace(' ', '_') | lower; # for result summary tmpls

      NEXT UNLESS this_section.is_active.match('yes');
        
      # get list of active (ie requested) lab_test field labels: 
      active_tests     = [ ]; # reset array for each section
      FOREACH entry IN this_section.panel; # PROCESS dumper.tt dump = entry;
        active_tests.push(entry.key) IF entry.value.status; # been requested if defined status 
      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 entry.value.status; # been requested if defined status 
      END;
        # PROCESS dumper.tt dump = active_tests;
        # PROCESS dumper.tt dump = resultable_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 %]      
      <div id="[% sectionName %]" class="itemhidden">

        [% # 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 %]
        </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 %] -->
