<!-- BEGIN record/patient_summary.tt -->
    [% PROCESS site/request_links.tt %]
    
    [% # PROCESS dumper.tt dump = has_optional %]
    [% # PROCESS dumper.tt dump = history %]
    [% # PROCESS dumper.tt dump = status_options %]
    [% # PROCESS dumper.tt dump = request_options %]
    [% # PROCESS dumper.tt dump = request_consent %]
    [% # PROCESS dumper.tt dump = is_locked %]
    
    [% patient = data.patient_case.patient %]    
    
    <table>
      [% IF request_consent.storage.match('no') AND c.is_lab_staff %]
        <caption id="no-store">**** patient has refused consent for retention of
          body fluids/organs ****
          </caption>
      [% END %]
      <thead>
      <tr class="tbl_header_darken">
        <th>
          [% url_args = 'last_name=' _ patient.last_name _ ';first_name=' _
              patient.first_name _ ';middle_name=' _ patient.middle_name
          %]
          <a href="[% app_url %]/search/do_search?[% url_args | url %]">Name</a>
        </th>
        <th>
          [% url_args = 'dob_day=' _ patient.dob.strftime('%d') _ ';dob_month=' _
              patient.dob.strftime('%m') _ ';dob_year=' _ patient.dob.strftime('%Y')
          %]
          <a href="[% app_url %]/search/do_search?[% url_args | url %]">DoB</a>
        </th>
        <th>
          [% IF data.patient_case.unit_number %]
            [% url_args = 'unit_number=' _ data.patient_case.unit_number %]
            <a href="[% app_url %]/search/do_search?[% url_args | url %]">Unit Number</a>
          [% ELSE %]
            Unit Number
          [% END %]
        </th>
        <th>
          [% IF patient.nhs_number %]
            [% url_args = 'nhs_number=' _ patient.nhs_number %]
            <a href="[% app_url %]/search/do_search?[% url_args | url %]">NHS Number</a>
          [% ELSE %]
            NHS Number
          [% END %]
        </th>
        <th>Lab No.</th>
        <th>Date</th>
        <th>Source</th>
        <th>Referrer</th>
        <th>Sample</th>
        [% IF c.user_can('edit_pid') %]
          <th colspan="3">Edit</th>
        [% END %]
      </tr>
      </thead>
      
      <tbody>
      <tr>
        [% class = request_options.urgent.is_selected ? 'urgent' : 'normal' %]
        <td class="[% class %]">
          [% patient.last_name   | upper %],
          [% format_firstname(patient) || patient.first_name | ucfirst %]
          [% patient.middle_name | ucfirst %]
        </td>
        <td>
          [% IF patient.dob;
                age_now = calculate_age(patient.dob, c.time_now);
                age_at_req = calculate_age(patient.dob, data.created_at) %]
            <span class="acronym" title="age at registration = [% age_at_req
                %]; age now = [% age_now %]">
              [% patient.dob.strftime('%d.%b.%Y') %]
            </span>
          [% ELSE; site.html.grey_null; END %]
        </td>
        <td>
          [% data.patient_case.unit_number || site.html.grey_null %]
        </td>
        <td>
          [% PROCESS site/nhs_number.tt nhs_number = patient.nhs_number %]
        </td>
        <td>
          [% PROCESS site/lab_number.tt want_url = 1 # expects request object 'data' %]
        </td>
        <td>[% data.created_at.strftime('%d.%b.%Y') %]</td>
              
        [% class = has_optional.clinical_trial ? 'trial' : 'normal' %]
        <td class="[% class %]">
          [% SET location = data.patient_case.referral_source.display_name %]
          [% PROCESS site/display_name.tt %]
        </td>
          [%
            SET department = data.referrer_department.hospital_department;
            SET referrer   = data.referrer_department.referrer;
          %]
            
          [% class = request_options.private.is_selected ? 'private' : 'normal' %]
        <td class="[% class %]">
          <span [% IF department %]class="acronym" title="[% department.display_name %]"
              [% END %]>[% referrer.name %]
          </span>
          [% IF request_options.copy_to.is_selected %]
            <sup>[<span class="red">+cc</span>]</sup>
          [% END %]
        </td>

          [% class = request_options.doi.is_selected ? 'doi' : 'normal' %]
        <td class="[% class %]">
          [%
            specimen_code = specimen_map.item(data.id).sample_code.join(', ');
            specimen_long = specimen_map.item(data.id).description.join('; ');
          %]

          [% IF has_optional.external_ref; 
              external_ref = data.request_external_ref.external_reference %]
            <span class="acronym" title="[% specimen_long %]; ref=[%
              external_ref %]">[% specimen_code %]</span>
          [% ELSE %]
            <span class="acronym" title="[% specimen_long %]">
              [% specimen_code %]
            </span>
          [% END %]
        </td>

        [% IF c.user_can('edit_pid') %]
        <td align="center">
          [% IF is_locked %]
            <img class="href" src="/images/locked_small.png" title="record locked" alt="" />
          [% ELSE %]
            <a class="href" href="[% app_url %]/patient/edit_patient/[% patient.id %]/[% data.id %]">
              <img class="href" src="/images/patient.png" title="edit patient" alt="" />
            </a>
            <a class="href" href="[% app_url %]/request/edit/[% data.id %]">
              <img class="href" src="/images/record.jpg" title="edit request" alt="" />
            </a>
            [% IF c.user_can('delete_record') %]
              <a class="href" href="[% app_url %]/request/delete_request/[% data.id %]">
                <img class="href" src="/images/delete.png" title="delete request" alt="" />
              </a>
            [% END %]
          [% END %]
        </td>
        [% END %]
      </tr>
      </tbody>
    </table>

    [% IF has_optional.clinical_trial %]
      [% trial_number = get_trial_number(patient.id, data.request_trial.trial_id) %]
      <p class="indent">
        <span class="trial">Clinical trial</span>:
        [% data.request_trial.trial.trial_name %]
        [% IF trial_number %]
          [ [% trial_number %] ]
        [% END %]
      </p>
    [% ELSIF has_optional.previous_clinical_trials.size  # has requests in trials: %]
      <p class="indent">
        <span class="info">Patient also has clinical trial request(s):</span>
        [% FOREACH trial IN has_optional.previous_clinical_trials %]
          <a href="[% app_url %]/search/do_search?patient_id=[% patient.id
            %];trial_id=[% trial.key %];_return=1">[% trial.value.trial_name %]</a>
            [[% trial.value.trial_number %]]
          [% ' | ' UNLESS loop.last %]
        [% END %]
      </p>
    [% END %]
    
    [% IF request_options.doi.has_others AND c.is_lab_staff %]
      <p class="indent blink error">[% c.messages('patient').biohazard %]</p>
    [% END %]

    [% IF request_errors.size; skip = {} %]
      <ul class="indent warning" id="errorList">
        [% FOREACH err IN request_errors;
          NEXT IF skip.item(err.error_code.code) # already displayed %]
          <li>
            [% err.time.strftime('%d.%b.%Y') %] ::
            [% err.error_code.description %]
            [[% err.error_code.code | upper %]]
          </li>
            [% skip.${err.error_code.code} = 1
              IF err.error_code.code.match('c') # only display 1st time %]
        [% END %]
      </ul>
    [% END %]
    
    <p>
      [% IF has_optional.initial_screen %]
        Screened
        [% IF c.user_can('view_unrestricted') # hide from non-authorised users %]
			as <strong>[% data.request_initial_screen.screen.description | html %]</strong>
		[% END %]
        [% user = history.screened.user %]
          by <strong>[% user.first_name | ucfirst %] [% user.last_name  | ucfirst %]</strong>
          on <strong>[% history.screened.time.strftime('%d.%b.%Y') %]</strong>
      [% ELSE %]
        <b>Record not screened</b>
      [% END %]
    </p>
<!-- END record/patient_summary.tt -->

