<!-- BEGIN [% component.name %] -->
    [% # PROCESS dumper.tt dump = resultable_tests %]
    [% # PROCESS dumper.tt dump = data_map.data_options %]
    [% # PROCESS dumper.tt dump = validation_failures %]

	[% addcss = [ 'jquery.datetimepicker.css' ] # for result data-type = date %]
	<script type="text/javascript" src="/js/jquery.datetimepicker.js"></script>
	<script type="text/javascript">
	  $( function(){ jQuery('.datetimepicker').datetimepicker(
		  { step: 5, format: 'Y-m-d H:i:s' });
	  });
	  $( function(){ jQuery('.datepicker').datetimepicker(
		  { timepicker: false, format: 'Y-m-d' });
	  });
	</script>

  <p class="indent"><span class="title">Results:</span>
    [% div_name = sectionName _ '_data'; INCLUDE site/snippets/toggleview.tt %]
  </p>

  <div id="[% div_name %]" class="[% toggle_class %]">
<!--
    <div class="warning">this_section.id:[% this_section.id %]</div>
    <div class="warning">sectionName:[% sectionName %]</div>
-->
    <div class="test_results">
      [% UNLESS is_locked # can still view results section if locked %]
      <form action="[% app_url %]/result/data_entry/[% data.id %]" method="post"
        name="[% sectionName %]_results" id="[% sectionName %]_results">
          <input type="hidden" name="_section_id" value="[% this_section.id %]" />
          <input type="hidden" name="_section_name" value="[% section %]" />
      [% END %]

        [% sorted_tests = natural_sort(resultable_tests);
          # PROCESS dumper.tt dump = sorted_tests;
        %]

        <table class="results">
          <tr>
            [%
                count = 1;
                # calculate max length of test labels & result options;
                max_label_length  = 1; # initialise
                max_result_length = 1;
                FOREACH t IN sorted_tests; # INCLUDE dumper.tt dump = [ t, t.length ];
                    # label length:
                    IF t.length > max_label_length; max_label_length = t.length; END;
                    # result options:
                    test_data = this_section.test.item(t);
                    data_type = data_map.lab_test_data_type.item(test_data.id).data_type;
                    UNLESS test_data.result; # unless already have result:
                        NEXT UNLESS test_data.is_active.match('yes'); # skip inactive test
                        NEXT IF data_type AND data_type.active.match('no'); # skip inactive result entry
                    END;
                    options = data_map.data_options.item(data_type.id);
                    FOREACH o IN options; # INCLUDE dumper.tt dump = o;
                        option_length = o.value.length;
                        IF option_length > max_result_length;
                            max_result_length = option_length;
                        END;
                    END;
                END;
                td_width = 7 * max_label_length; # roughly enough for label
                IF td_width < 70; td_width = 70; END;  # set minimum width (max = 7 * 25 = 175)
                    # INCLUDE dumper.tt dump = max_label_length;
                    # INCLUDE dumper.tt dump = max_result_length;
                # cols = 3, unless combined label + result options too log:
                col_num = max_result_length + max_label_length > 45 ? 2 : 3;
            %]

            [% FOREACH test_name IN sorted_tests;
                  # PROCESS dumper.tt dump = this_section.test.item(test_name);
                test_data = this_section.test.item(test_name);
                  # PROCESS dumper.tt dump = test_data;
                data_type = data_map.lab_test_data_type.item(test_data.id).data_type;
                  # PROCESS dumper.tt dump = data_type;

                UNLESS test_data.result; # unless already have result:
                    NEXT UNLESS test_data.is_active.match('yes'); # skip inactive test
                    NEXT IF data_type AND data_type.active.match('no'); # skip inactive result entry
                    # can't do next line - skips Cut-up & processing tests:
                    # NEXT IF data_map.requested_tests.size
                    #   AND NOT data_map.requested_tests.item(test_data.id);
                END;

                # NEXT IF c.query.param('results_only') AND NOT test_data.result; # discontinued

			       IF test_data.is_active.match('no'); class = 'grey';
                ELSIF test_data.result.defined; class = 'title';
                ELSIF data_map.requested_tests.item(test_data.id); class = 'red';
                ELSIF test_data.status; class = 'red'; # awaiting result
                ELSE; class = 'normal'; END;
            %]
            <td align="right" width="[% td_width %]" class="_result-data-label"
                    style="/* border: solid 1px red */">
              <span class="[% class %]">[% test_name | html %]</span>
           </td>
            <td width="250" class="_result-data-entry">[% # INCLUDE dumper.tt dump = data_type %]
              [% IF validation_failures.${test_data.id} %]
                <span class="highlight">!</span>
              [% END %]
              [% IF data_type.type.match('free_text') OR is_locked %]
                <input type="text" name="lab_test_id_[% test_data.id %]"
                  value="[% test_data.result %]" size="20" [% IF is_locked %]
                    readonly="readonly"[% END %] />
              [% ELSIF data_type.type.match('^date$') %]
				<input type="text" name="lab_test_id_[% test_data.id %]"
                  value="[% test_data.result %]" placeholder="yyyy-mm-dd"
				  class="datepicker" />
              [% ELSIF data_type.type.match('^datetime$') %]
				<input type="text" name="lab_test_id_[% test_data.id %]"
				  value="[% test_data.result %]" placeholder="yyyy-mm-dd hh:mm:ss"
				  class="datetimepicker" />
			  [% ELSIF data_type.type.match('^numeric_range');
				  range = menu_function(data_type.type);
				  # INCLUDE dumper.tt dump = range
				%]
				  <select name="lab_test_id_[% test_data.id %]">
					<option value="">&nbsp;</option>
				  [% FOREACH i IN [ range.low .. range.high ] %]
					<option value="[% i %]"
					  [% 'selected' IF test_data.result == i %]>[% i %]
					</option>
				  [% END %]
				  </select>
              [% ELSIF data_type.type.match('^(integer|decimal)$') %]
                    <input type="text" name="lab_test_id_[% test_data.id %]"
                        value="[% test_data.result %]" size="20" />
			  [% ELSE; # PROCESS dumper.tt dump = data_map.data_options.item(data_type.id); %]
              <select name="lab_test_id_[% test_data.id %]">
                <option value="">&nbsp;</option>
                [% FOREACH opt IN data_map.data_options.item(data_type.id);
                    # PROCESS dumper.tt dump = [opt, test_data.result];
                    NEXT IF opt.is_active.match('no')
                      AND test_data.result != opt.value; # for legacy data before opt disabled
                %]
                <option value="[% opt.value %]"
                    [% 'selected' IF test_data.result == opt.value %]>
                  [% opt.value %]
                </option>
                [% END %]
              </select>
          [% END %]
            </td>

          [% # n_cols = data_type.type == 'text' ? 2 : 6 # text longer than others %]
          [% UNLESS loop.last OR # don't start new table unless multiple of $n_cols:
            count % col_num %]
              </tr>
            </table>
            <table class="results">
              <tr>
          [% END %]

          [% count = count + 1 %]
      [% END %]

          </tr>
        </table>

        <!-- BEGIN result summary -->
          [% IF this_section.has_result_summary.match('yes')
              AND active_tests.size # only show results summaries if at least 1 test %]
            <p class="indent">
              <span class="title">Results summary:</span>
            </p>

            [% IF results_summary_opts.item(section) # may not have select menu opts
                AND NOT is_locked %]
            <blockquote>
                <select name="_results_summary">
                    [% label = '' # initialise var in case optgroup %]
                  <option value="">&nbsp;</option>
                  [% FOREACH option IN results_summary_opts.item(section);
                        NEXT UNLESS option.is_active.match('yes');
                        # PROCESS dumper.tt dump = option; %]
                    [% IF option.description.match('~'); # numerical ordering
                        split = option.description.split('~'); # PROCESS dumper.tt dump = split;
                    %]
                        <option>[% split.1 # RHS value %]</option>
                    [% ELSIF option.description.match('::'); # optgroup symbol
                        split = option.description.split('::'); # PROCESS dumper.tt dump = split;
                    %]
                        [% IF split.0 != label # time to change optgroup label: %]
                            [% '</optgroup>' IF label # close opgroup block %]
                            [% label = split.0 # set new label %]
                            <optgroup label="[% label %]">
                                <option>[% split.1 # RHS value %]</option>
                        [% ELSE # discard split.0 (optgroup label) %]
                            <option>[% split.1 # RHS value %]</option>
                        [% END %]
                        [% '</optgroup>' IF loop.last %]
                    [% ELSE # not optgroup or numerical ordering %]
                        <option>[% option.description %]</option>
                    [% END %]
                  [% END %]
                </select>
            </blockquote>
            [% END %]

            <blockquote>
              <p class="classname">
                <textarea name="results_summary" rows="1" cols="80"
                    class="expand20-200" [% IF is_locked %]readonly="readonly"[%
                    END %]>[% result_summaries.item(section).results_summary # DO NOT WRAP THIS %]</textarea>
              </p>
            </blockquote>
          [% END %]
        <!-- END result summary -->

    [% UNLESS is_locked %]
        [% IF this_section.has_test_sign_out.match('yes')
            AND c.user_can('modify_results') %]
          <p class="indent">
            <span class="title">Sign-off all tests in this section:</span>
            <input type="checkbox" name="complete_all_tests" value="1" />
          </p>
        [% END %]

        <p align="center">
            [% site.html.submit %]
            [% # site.html.reset # disabled due to complaints !! %]
        </p>
      </form>
    [% END %]

	  <!-- added by pete for canned report (results_template) -->
	  [% IF this_section.has_result_summary.match('yes') AND active_tests.size
		AND NOT is_locked %]
        <!-- the javascript function to change the source of the frame -->
        <script type="text/javascript">
          function load_[% sectionName %]_template()  {
            var html = document.getElementById('[% sectionName %]_id').contentWindow.document.body.innerHTML;
            document.[% sectionName %]_results.results_summary.value=document.[% sectionName %]_results.results_summary.value + html
          }
        </script>

		<!-- set up a frame to hold the information we require -->
        <p class="indent">
          <span class="title">Results summary templates:</span>
		  [% div_name = sectionName _ '_tmpl';
			INCLUDE site/snippets/toggleview.tt %]
        </p>

        <div id="[% div_name %]" class="itemhidden">
          <iframe name="[% sectionName %]_frame" class="indent"
            id="[% sectionName %]_id" width="600"></iframe>

          <!-- set up a form to search for a canned report file locally and submit
              to a javascript function -->
          <form name="[% sectionName %]_tmpl" enctype="multipart/form-data"
              method="post" action="[% app_url %]/ajax/load_template"
              target="[% sectionName %]_frame">
            <p class="indent">
              <!-- set up a file type input field which submits the form on change -->
              <input type="file" name="template" size="30" onChange="form.submit()" />
              <input type="button" onclick="load_[% sectionName %]_template();"
                  value="Import Template" />
            </p>
          </form>
        </div>
	  [% END %]

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