<!-- BEGIN [% template.name %] -->

  [% META title = 'Worklist &raquo; Outreach Pack Overdue Letter Dispatches' -%]

    [% # PROCESS dumper.tt dump = requests %]
    [% # PROCESS dumper.tt dump = notifications %]
    
  [% # define some data structures:
    headers = [ '' 'LabNo' 'Name' 'NHS No.' 'Date' 'Location' 'Pack sent' 'Days' '' ];
  %]
  
  [% BLOCK headers %]
      <tr>
        [% FOREACH item IN headers; # PROCESS dumper.tt dump = item.length %]
            [% IF item.length %]<th scope="">[% item | html_line_break %]</th>
            [% ELSE %]<td class="borderless"></td>[% END %]
        [% END %]
      </tr>
  [% END %]
  
  [% BLOCK row;
        patient = data.patient_case.patient; %]
      <tr>
        <td>[% i = i + 1; i %]
       <td>[% INCLUDE site/lab_number.tt want_url = 1 %]</td>
       <td>
           [% patient.last_name  | upper %],
           [% patient.first_name | ucfirst %]
       </td>
       <td>[% patient.nhs_number %]</td>
       <td>[% data.created_at.strftime('%d.%b.%Y') %]</td>
       <td>[% data.patient_case.referral_source.display_name %]</td>
       <td>[% data.outreach_request_pack_dispatch.pack_sent.strftime('%d.%b.%Y') %]</td>
       <td>[% data.delta_days %]</td>
       <td align="center">
          <input type="checkbox" name="request_id" value="[% data.id %]" />
       </td>
      </tr>
    [% END %]

  [% IF requests.size; # calculate how many in each time zone:
    totals.ninety = 0; totals.sixty = 0; totals.thirty = 0; # PROCESS dumper.tt dump = totals;
    FOREACH r IN requests; # PROCESS dumper.tt dump = data;
           IF r.delta_days >= 90; totals.ninety = totals.ninety + 1;
        ELSIF r.delta_days >= 60; totals.sixty  = totals.sixty  + 1;
        ELSIF r.delta_days >= 30; totals.thirty = totals.thirty + 1;   END;
    END; # PROCESS dumper.tt dump = totals;
  %]
  <h3 class="header">Outreach pack overdue letter dispatches</h3>

  <h4>Return to clinic notifications [[% totals.ninety %]]:</h4>
  [% IF totals.ninety %]
  <form method="post" action="[% app_url %]/outreach/do_letter_dispatch/90">
    <table class="indent">
      [% INCLUDE headers %]
        [% i = 0 %]
      [% FOREACH data IN requests; # PROCESS dumper.tt dump = data;
            NEXT UNLESS data.delta_days >= 90; # return to clinic letter:
            PROCESS row;
        END %]
      <tr class="borderless">
        <td colspan="9" align="right">[% site.html.submit %]</td>
      </tr>
    </table>
  </form>  
  [% END %]

  <h4>60 day notifications [[% totals.sixty %]]:</h4>
  [% IF totals.sixty %]
  <form method="post" action="[% app_url %]/outreach/do_letter_dispatch/60">
    <table class="indent">
      [% INCLUDE headers %]
        [% i = 0 %]
      [% FOREACH data IN requests; # PROCESS dumper.tt dump = data;
            NEXT UNLESS ( data.delta_days < 90 AND data.delta_days >= 60 ); # 60 - 89
            PROCESS row;
      END %]
      <tr class="borderless">
        <td colspan="9" align="right">[% site.html.submit %]</td>
      </tr>
    </table>
  </form>  
  [% END %]

  <h4>30 day notifications [[% totals.thirty %]]:</h4>
  [% IF totals.thirty %]
  <form method="post" action="[% app_url %]/outreach/do_letter_dispatch/30">
    <table class="indent">
      [% INCLUDE headers %]
        [% i = 0 %]
      [% FOREACH data IN requests; # PROCESS dumper.tt dump = data;
            NEXT UNLESS ( data.delta_days < 60 AND data.delta_days >= 30 ); # 30 - 59
            PROCESS row;
      END %]
      <tr class="borderless">
        <td colspan="9" align="right">[% site.html.submit %]</td>
      </tr>
    </table>
  </form>
  [% END %]
  [% ELSE %]
      <p class="info">No overdue packs</p>
  [% END %]
<!-- END [% template.name %] -->
