<!-- BEGIN record/report.tt -->
	[% PROCESS site/popup.tt height=280, width=900, top=280 %]

	[% # PROCESS dumper.tt dump = previous_diagnoses %]
	[% # PROCESS dumper.tt dump = history.keys %]
	[% # PROCESS dumper.tt dump = all_results %]
	[% # PROCESS dumper.tt dump = has_optional %]
    
    [% # need to prevent non-lab-staff users viewing unauthorised report:
        is_lab_staff = c.is_lab_staff;
        
        IF has_optional.report
            AND status_options.authorised.is_active.match('yes')
            AND NOT history.authorised
            AND request_data.year > 2005 # eg Molecular Misc requests pre-2006 never auth'd
            AND NOT is_lab_staff %]
        <p class="indent">[report pending authorisation]</p>
    [% ELSE;
            class = 'normal'; # for hidden div:
            IF has_optional.report
                AND NOT history.authorised
                AND status_options.authorised.is_active.match('yes')
                AND is_lab_staff; # hide it if lab-staff and not auth'd:
                class = 'itemhidden';
            %]	
            <p class="indent"><span class="grey">view unauthorised report</span>:
                [% div_name = 'report'; INCLUDE site/snippets/toggleview.tt %]
            </p>
            [% END %]
    
	<div id="[% div_name || 'normal' %]" class="[% class %]">
    <table>
      <tr>
        <td class="label">Clinical details:</td>
        <td class="data">
          [% IF has_optional.report %]
              [% request_data.request_report.clinical_details %]
          [% ELSE %]
              <span class="grey">[pending]</span>
          [% END %]
        </td>
      </tr>

      <tr>
        <td class="label">Specimen details:</td>
        <td class="data"> 
        [% IF has_optional.report %]
          <i>Quality</i>:
          [% request_data.request_report.specimen_quality -%]
        [%- ELSE -%]
          <span class="grey">[pending]</span>
        [%- END; IF request_data.request_gross_description.detail -%].
          <i>Gross Description</i>:
          [% request_data.request_gross_description.detail %]
        [% END %]
        </td>
      </tr>

      [% IF result_summaries %]
        [% FOREACH entry IN result_summaries; section_name = entry.key %]
          <tr>
            <td class="label" style="vertical-align: top">
              [% section_name.replace(' ', '&nbsp;') # prevent line-break %]:
            </td>
            <td class="data">
              [% symbolise(entry.value.results_summary) # escapeHTML already done %]
			  
			  [% test_results = []; FOREACH test IN all_results.item(section_name).test;
				NEXT UNLESS test.value.result; # PROCESS dumper.tt dump = test;
				test_results.push(test); END;
				IF test_results.size;
			  %]
				[% div_name = section_name _ '_tests';
				  INCLUDE site/snippets/toggleview.tt %]
				<div id="[% div_name %]" class="itemhidden">
				  <p class="classname">
				  [% FOREACH test IN test_results %]
					<i>[% test.key %]</i><sup>[% test.value.result %]</sup>[% ','
					  UNLESS loop.last %]
				  [% END %]
				  </p>
				</div>
			  [% END %]
            </td>
          </tr>
        [% END %]
      [% END %]

      <tr>
        <td class="label" style="vertical-align: top">Morphology/Comment:</td>
        <td class="data">
          [% IF has_optional.report %]
            [% symbolise(request_data.request_report.comment) | html_line_break %]
          [% ELSE %]
            <span class="grey">[pending]</span>
          [% END %]
        </td>
      </tr>

      <tr>
        <td class="label">Diagnosis:</td>
        <td class="data">
          [% IF has_optional.report %]
            <strong>
            [% IF c.user_can('report') AND previous_diagnoses.size %]
              <a href="[% app_url %]/history/previous_diagnosis/[% request_data.id %]"
                onclick="return popup(this.href);">
                [% request_data.request_report.diagnosis.name | html %]						
              </a> [ [% previous_diagnoses.size %] ]
            [% ELSE %]
              [% request_data.request_report.diagnosis.name | html %]
            [% END %]
            [% UNLESS request_data.request_report.status.match('default') %]
              [ <span class="red">[% request_data.request_report.status | upper %]</span> ]
            [% END %]
            </strong>
          [% ELSE %]
            <span class="grey">[pending]</span>
          [% END %]

          [% IF request_data.request_report.diagnosis.icdo3 AND c.cfg('settings').hmrn_link %]
              <a href="[% app_url %]/hmrn/info/[% request_data.request_report.diagnosis.id %]"
                    target="_blank">
                <span title="further information from HMRN web-site; opens in external link"
                    class="acronym"><img src="/images/hmrn_info.png" />
                </span>
              </a>
          [% END %]
          
          [% FOREACH entry IN history; # PROCESS dumper.tt dump = entry.value.time;
              IF entry.value.action.match('diagnosis status alert'); # if multiple 'cc':
                diagnosis_alert = entry; # only need 1st one
                LAST;
              END;
            END;
            IF diagnosis_alert;
          %]
            <span style="margin-left: 1em; vertical-align: middle">
              <img src="/images/emailed.jpg" />
              [[% diagnosis_alert.value.time.strftime('%d.%b.%Y @ %H:%M') %]]
            </span>
          [% ELSIF # new/relasped case:
              NOT request_data.request_report.status.match('default')
              AND c.cfg('settings').have_diagnosis_status_alert # or ? use cron script instead
              AND ( # authorised (if in use):
                history.authorised OR
                ( status_options.authorised.is_active.match('no') AND history.reported )
              ) 
              AND c.user_can('report') # has an mdt contact address configured:
              AND has_mdt_contact(request_data.patient_case.referral_source_id) 
          %]
            <span style="margin-left: 1em">
              <a href="[% app_url %]/request/email_alert/[% request_data.id %]">
                <img class="menu" src="/images/email.jpg"
                  style="vertical-align: middle; border: 1px solid red !important" />
              </a>
            </span>
            <span class="red">
              <span class="blink">&#171; e-mail alert</span>
            </span>            
          [% END %]
        </td>
      </tr>
    </table>
	</div>
    [% END # lab-staff section %]

    [% # PROCESS dumper.tt dump = history %]

    [% IF history.reported; user = history.reported.user %]
      <p>
        Reported by
          <b>[% user.first_name | ucfirst %]</b>
          <b>[% user.last_name  | ucfirst %]</b>
          on <b>[% history.reported.time.strftime('%a %d.%b.%Y @ %H:%M') %]</b>
      </p>
    [% END %]

    [% IF history.authorised; user = history.authorised.user %]
      <p>
        Authorised by
          <b>[% user.first_name | ucfirst %]</b>
          <b>[% user.last_name  | ucfirst %]</b>
          on <b>[% history.authorised.time.strftime('%a %d.%b.%Y @ %H:%M') %]</b>
      </p>
    [% END %]
<!-- END record/report.tt -->
