<!-- BEGIN [% component.name %] -->
    [% # INCLUDE dumper.tt dump = history.keys %]
    [% # INCLUDE dumper.tt dump = active_link %]
    [% # INCLUDE dumper.tt dump = all_results %]
    [% # INCLUDE dumper.tt dump = status_options %]
    [% # INCLUDE dumper.tt dump = result_summaries %]
    [% # INCLUDE dumper.tt dump = specimen_map.item(data.id) %]

    [% # INCLUDE dumper.tt dump = request_data # = data.as_tree %]
    [% # INCLUDE dumper.tt dump = report       # = data.request_report %]
    [% # INCLUDE dumper.tt dump = patient      # = data.patient_case.patient %]

   [% have_secondary_diagnosis = request_data.request_secondary_diagnosis  ? 1 : 0 %]
  [% IF active_link.match('search'); # don't load for any print function
    href = app_url _ '/request/print_record/' _ data.id;
  %]
	<p class="printHide">
        <input type="button" value="&lt;&lt; Back" class="button"
            onclick="history.go(-1);" />
        <!--
        <input type="button" value="Print" class="button"
            onclick="window.location.href='[% href %]'" />
        -->
      [% IF c.user_can('print_one'); %]
        <a href="[% href %]"><img src="/images/print_icon.gif" alt="Print" /></a>
      [% END %]

      [% IF c.user_can('view_history') %]
        <a href="[% app_url %]/history/=/[% data.id %]">
          <span class="label label-warning">History</span>
        </a>
      [% END %]
      <!-- // not required
        <a href="[% app_url %]/image/=/[% data.id %]">
            <span class="label">Data files</span>
        </a>
      -->
        <a href="[% app_url %]/search/do_search/?patient_id=[% patient.id %]">
          <span class="label label-success">Other reports</span>
        </a>
        :::
        <a href="[% app_url %]/resources">
          <span class="label label-important">Resources</span>
        </a>
        <a href="[% app_url %]/search">
          <span class="label label-info">New search</span>
        </a>
        [% IF need_to_hide_from_mozilla # Moz can't load this!! %]<!--[if IE]>
          ... <![endif]-->[% END %]
        :::
        <a href="[% app_url %]/login/hello">
          <i class="fa fa-home fa-lg"></i>
        </a>
    </p>
  [% END %]

<div class="printHide" [% # only load #report-body for html page, not for PDF:
        IF active_link.match('search') %]id="report-body"[% END %]>
 <table width="100%">
	<tr>
		<td align="right">
            <img src="/images/[% c.cfg('settings').report_header %]" />
        </td>
    </tr>
    <tr>
		<td>
			<div class="service-name">
				<font color="blue" size="4">
					[% c.cfg('settings').service_name %]
				</font>
			</div>
            [%
              footer = c.cfg('settings').service_address
                  _ ' Tel. ' _ c.cfg('settings').service_telno;
            %]
            <div align="center"><font size=2>[% footer %]</font></div>
		</td>
	</tr>
  </table>

  <h4 align="center">
    Pathology Report on [% c.cfg('settings').lab_name_abbreviation %] Ref No:
    [% PROCESS site/lab_number.tt; want_url=0 %]
  </h4>

  <table width="100%" cellpadding="3">
    <tr>
      <td valign="top">
        <i><font size=3>Patient ID</font></i>
        <blockquote>
		<font size=2>
          Name: <b>
            [% patient.last_name   | upper %],
            [% patient.first_name  | ucfirst %]
            [% patient.middle_name | ucfirst %]
          </b> <br />
          DoB: [% patient.dob.strftime('%d.%b.%Y') %] <br />
		  [% IF patient.nhs_number %]
			NHS No: [% PROCESS site/nhs_number.tt nhs_number = patient.nhs_number %]
			[% IF data.patient_case.unit_number %]
			  [[% data.patient_case.unit_number %]]
		    [% END %]
		  [% ELSE %]
			  [% UNLESS # not exempted categories:
				  data.request_initial_screen.screen.description.match('HIV') OR
				  data.clinical_details.match('HIV') %]
				NHS No: <font color="red"><b>please use the NHS no</b></font><br />
			  [% END %]
		    Hospital No: [% data.patient_case.unit_number || 'unknown' %]
		  [% END # IF patient.nhs_number %]
		  </font>
        </blockquote>
      </td>
      <td valign="top"><i><font size=3>Source</font></i>
        <blockquote>
		<font size=2>
          Referrer: [% data.referrer_department.referrer.name %]
		  [[% data.referrer_department.hospital_department.display_name %]] <br />
          Source: [% data.patient_case.referral_source.display_name %] <br />
          Date received: [% data.created_at.strftime('%d.%b.%Y') %]
		 </font>
        </blockquote>
      </td>
    </tr>
  </table>

  <p>
    <font size=3><i>Specimen</i></font>
  </p>

  <blockquote>
    Type: [% specimen_map.item(data.id).description.join('; ') %]
	[% IF request_data.request_external_ref # use as_tree version to prevent db lookups %]
		[external ref: [% data.request_external_ref.external_reference | html %]]
	[% END %]<br />
    [% IF request_data.request_gross_description # use as_tree version to prevent db lookups %]
	  Gross Description: [% data.request_gross_description.detail %]<br />
	[% END %]
    Quality: [% data.request_report.specimen_quality %]
  </blockquote>

[% IF NOT report %]
    <div align="center" class="in-progress">[ REPORT IN PROGRESS ]</div>
[% ELSE %]
  <!-- // begin report -->
  [% UNLESS special_formatting.skip_clinical_details %]
    <p>
      <font size=3><i>Clinical Details</i></font>
    </p>

    <blockquote>
      [% symbolise(data.request_report.clinical_details) %]
    </blockquote>
  [% END # UNLESS special_formatting.skip_clinical_details %]

  [% IF result_summaries.size;
	FOREACH section IN all_results; # INCLUDE dumper.tt dump = section;
            # INCLUDE dumper.tt dump = result_summaries.${section.key};
        results = section.value.test; # INCLUDE dumper.tt dump = results;
        section_name = section.key; # INCLUDE dumper.tt dump = result_summaries.${section_name};
      	section_result_summary = result_summaries.${section_name};
            # INCLUDE dumper.tt dump = section_result_summary;
		# only want sections with active results_summaries:
		NEXT UNLESS section_result_summary.lab_section.has_result_summary.match('yes');

		test_results = [ ]; # reset array
		FOREACH t IN results; # INCLUDE dumper.tt dump = t;
		  NEXT UNLESS t.value.result;
		  IF t.value.result.match('^\d+$'); # add % sign to numerical-only result:
			t.value.result = t.value.result _ '%'; END;
		  result = t.key _ '<sup>' _ t.value.result _ '</sup>';
		  test_results.push(result);
		END; # FOREACH t IN results
	  %]

	  [% # skip unless results summary or test results:
		NEXT UNLESS section_result_summary.results_summary || test_results.size;
      %]
	  <div>
		[% section_name = section.key | lower %]
		<font size=3>
		  <div>[% # ? override default section name eg imatinib monitoring:
                special_formatting.section_name_map.item(section_name)
                || section.key | ucfirst %]
		  </div>
		</font>
	  </div>

	  [% IF test_results.size %]
		<blockquote>
            <font size=2>[% test_results.join(' ') %]</font>
        </blockquote>
	  [% END %]

	  [% IF section_result_summary.results_summary;
        # INCLUDE dumper.tt dump = section_result_summary %]
	 <blockquote>
		<font size=2>
		  [% UNLESS special_formatting.no_comment # eg imatinib monitoring %]
            Comment:
		  [% END %]
		</font>
		  [% # highlight section if timestamp > auth/report time:
			ref_time = status_options.authorised.is_active.match('yes')
			  ? history.authorised.time : history.reported.time;
			font_color = section_result_summary.time > ref_time
              ? 'red' : 'black';
		  %]
		  <font color="[% font_color %]" size=2>
			[% symbolise(section_result_summary.results_summary)
			  | html_line_break # escapeHTML already done %]
		  </font>
          <div>
            <font color="[% font_color %]" size=2>
                [%
                    user = section_result_summary.user;
                    time = section_result_summary.time;
                    user.first_name.ucfirst _ ' ' _
                    user.last_name.ucfirst _ ' [' _
                    time.strftime('%a %d.%b.%Y') _ ' @ ' _
                    time.strftime('%H:%M') _ ']';
                %]
            </font>
          </div>
     </blockquote>

	  [% END # IF section_result_summary.results_summary %]

	  [% BLOCK chart_url %]
		<!-- BEGIN chart -->
		<blockquote>
		  [% IF 1 # html output - can call image generation from browser: %]
			<p><img src="[% app_url %]/chart/[% chart_type %]/[% data.id %]" /></p>
		  [% ELSE; # pdf output - have to create tmp image 1st:
			 image = create_chart(chart_type, data.id); %]
			 <p><img src="http://localhost/images/chart/[% image %].png" /></p>
		  [% END %]
		</blockquote>
		<!-- END chart -->
	  [% END # BLOCK chart_url %]

	  [% # draw graphs/charts if required:
		IF special_formatting.has_glivec AND patient.nhs_number;
		  chart_type = 'imatinib';
		ELSIF special_formatting.has_hiv;
		  chart_type = 'hiv';
		ELSIF special_formatting.has_pnh;
		  chart_type = 'pnh';
		END;
		IF chart_type; INCLUDE chart_url; END;
	  %]

	[% END # FOREACH section IN all_results %]
  [% END # IF result_summaries.size %]

  [% IF data.request_report.comment AND NOT special_formatting.skip_morphology %]
  <p>
    <font size=3><i>Comments</i></font>
  </p>

  <blockquote>
	[%
	  raw_comment = line_break(data.request_report.comment);
	  formatted_comment = symbolise(raw_comment);
		# highlight 'additional comment':
	  text = '<font color="red">ADDITIONAL COMMENT</font>';
	  formatted_comment.replace('[Aa]dditional [Cc]omment', text) | html_line_break;
	%]
  </blockquote>
  [% END # IF data.request_report.comment AND NOT special_formatting.skip_morphology %]

  [% UNLESS special_formatting.skip_diagnosis %]
  <p>
    <font size=3>
      <i>Diagnosis</i>: <b>[% data.request_report.diagnosis.name | html %]</b>
	  [% IF data.request_report.diagnosis.icdo3 %]
		[[% data.request_report.diagnosis.icdo3 %]]
	  [% END %]
    </font>
  </p>
    [% IF have_secondary_diagnosis %]
  <p>
    <font size=3>
      <i>Secondary Diagnosis</i>: <b>[% data.request_secondary_diagnosis.diagnosis.name | html %]</b>
	  [% IF data.request_secondary_diagnosis.diagnosis.icdo3 %]
		[[% data.request_secondary_diagnosis.diagnosis.icdo3 %]]
	  [% END %]
    </font>
  </p>
    [% END %]
  [% END # UNLESS special_formatting.skip_diagnosis %]

  [% IF context_warning_map.item(data.request_report.diagnosis.id) %]
	<p><b>Note</b>:
	  [% context_warning_map.item(data.request_report.diagnosis.id) %]
	</p>
  [% END %]

  [% IF request_errors.size;
	  errs = [ ];
	  FOREACH entry IN request_errors; # only want reportable errs:
		  err_code = entry.error_code.code; uc_err_code = err_code | upper;
		  NEXT UNLESS report_error_codes.item(err_code);
		  err_str = report_error_codes.item(err_code).description
			_ ' [' _ uc_err_code _ ']';
		  errs.push(err_str);
		END; # FOREACH entry IN request_errors
	  IF errs.size; %]
	  <p>
		<font color="red">
		  <b>PLEASE NOTE - request was received with the following
			error[% IF request_errors.size > 1 %]s[% END %]</b>:
			<blockquote>
			[% FOREACH err IN errs %]
			  * [% err %]<br />
			[% END %]
			</blockquote>
		  </b>
		</font>
	  </p>
  [% END; END; # IF request_errors.size %]

  <p align="right">
  <font size=2>
    Reported by:
	<b>[% history.reported.user.first_name | ucfirst %]</b>
	<b>[% history.reported.user.last_name  | ucfirst %]</b>
	on [% history.reported.time.strftime('%d.%b.%Y at %H:%M') %]
  [% IF history.authorised %]
	<br />
    Report authorised by:
	<b>[% history.authorised.user.first_name | ucfirst %]</b>
	<b>[% history.authorised.user.last_name  | ucfirst %]</b>
	on [% history.authorised.time.strftime('%d.%b.%Y at %H:%M') %]
  [% END # IF history.authorised %]
  </font>
  </p>

  [% BLOCK amended_report_update;
	  FOREACH entry IN previous_diagnoses;
          # PROCESS dumper.tt dump = entry.as_tree %]
        <p align="right"><font size=2>
          Amended report issued by:
            <b>[% entry.user.first_name | ucfirst %]</b>
            <b>[% entry.user.last_name  | ucfirst %]</b>
            on [% entry.time.strftime('%d.%b.%Y') %]
            at [% entry.time.strftime('%T').match('00:00:00') # for legacy data
                ? '??:??' : entry.time.strftime('%H:%M') %]
			</font>
        </p>
	  <p align="center">
		<font color="red" size=2>
		  <strong>** please note this is an amended report which supercedes any
			previous report on this specimen **</strong>
		</font>
	  </p>
    [% END # FOREACH entry IN previous_diagnoses %]
  [% END # BLOCK amended_report_update %]

  [% # PROCESS dumper.tt dump = previous_diagnoses %]
  [%
    IF previous_diagnoses.size;
        FOREACH entry IN previous_diagnoses;
          IF history.reported.time.delta_days(entry.time).delta_days; # ie not same date
            PROCESS amended_report_update;
            LAST; # only needed once
          END;
        END;
    END; # IF previous_diagnoses.size;
   %]

   [% IF dispatch_log.size %]
	<p align="right">
	[% FOREACH report IN dispatch_log %]
	  Report sent to:
		[% report.recipient %] on [% report.time.strftime('%d.%b.%Y at %H:%M') %]
		[% UNLESS loop.last %]<br />[% END %]
	[% END %]
	</p>
  [% END # IF dispatch_log.size %]

  <!-- // end report -->
[% END # IF NOT report %]
</div>
<div class="viewHide error">
    <hr />
    THIS PAGE VIEW IS NOT FORMATTED FOR PRINTING.
    PLEASE USE THE "PRINT" ICON IN THE UPPER NAVIGATION SECTION.
    <hr />
</div>
<!-- END [% component.name %] -->
