	[% META title="Request History" %]
<!-- BEGIN [% template.name %] -->

    [% # PROCESS dumper.tt dump = history.request_data %]
    [% # PROCESS dumper.tt dump = history.error_codes %]
    [% # PROCESS dumper.tt dump = request.as_tree %]
    [% # PROCESS dumper.tt dump = categorical.demographics %]

    [% patient = request.patient_case.patient %]
    <h2>
        Request history on [% patient.last_name | upper %],
        [% format_firstname(patient) || patient.first_name | ucfirst %]
        [ [% PROCESS site/lab_number.tt data = request %] ]
    </h2>
	
   [% IF categorical.patient_data %]
        <h3 class="header">Patient demographics (I):</h3>
        <div class="indent">
            <table class="history">
                <tr>
                    <th>Day</th>
                    <th>Date</th>
                    <th>Time</th>
                    <th>UserID</th>
                    <th>Changed</th>
                    <th>From</th>
                    <th>To</th>
                    <th>Reason</th>
                </tr>
            [% FOREACH entry IN categorical.patient_data %]
                [% import(entry) %]
                <tr>
                    <td>[% changed_on.strftime('%a') %]</td>
                    <td>[% changed_on.strftime('%d.%b.%Y') %]</td>
                    <td>[% changed_on.strftime('%T') %]</td>
                    <td>[% changed_by | upper %]</td>
                    <td>[% field_name | replace('_', ' ') %]</td>
                    <td>[% changed_from || site.html.grey_null %]</td>
                    <td>[% changed_to   || site.html.grey_null %]</td>
                    <td align="center">
                        [% categorical.error_codes.item(error_code).description %]
                        [[% error_code | upper %]]                    
                    </td>
                </tr>
            [% END %]
            </table>
        </div>
    [% END %]

	[% IF categorical.demographics.size %]
        <h3 class="header">Patient demographics (II):</h3>
        <div class="indent">
            <table class="history">
                <tr>
                    <th>Day</th>
                    <th>Date</th>
                    <th>Time</th>
                    <th>UserID</th>
                    <th>Action</th>
                </tr>
            [% FOREACH entry IN categorical.demographics; # PROCESS dumper.tt dump = entry %]
                <tr>
                    <td>[% entry.time.strftime('%a') %]</td>
                    <td>[% entry.time.strftime('%d.%b.%Y') %]</td>
                    <td>[% entry.time.strftime('%T') %]</td>
                    <td>[% entry.user.username | upper %]</td>
                    <td>[% INCLUDE site/truncate.tt
							str = entry.action
							chars = 80
						%]
					</td>
                </tr>
            [% END %]
            </table>
        </div>		
	[% END %]
  <p>
    [% div_name = 'categories'; INCLUDE site/snippets/toggleview.tt %]
    <span class="title">By category</span>
  </p>
  
  <div class='itemhidden' id='categories'>
    [% PROCESS history/categories.tt %]
  </div>

  <p>
	[% div_name = 'chronology'; INCLUDE site/snippets/toggleview.tt %]
    <span class="title">Chronological</span>
  </p>

  <div class='itemshown' id='chronology'>
    [% PROCESS history/chronological.tt %]
  </div>
  
    [% IF categorical.diagnosis_data.size %]
        <h3 class="header">Diagnosis history:</h3>

        <div class="indent">
            <table class="history">
                <tr>
                    <th>Day</th>
                    <th>Date</th>
                    <th>Time</th>
                    <th>UserID</th>
                    <th>Old diagnosis</th>
                    <th>Reason</th>
                </tr>
                [% FOREACH event IN categorical.diagnosis_data %]
                <tr>
                    <td>
                        [% event.time.strftime('%a') %]
                    </td>
                    <td>
                        [% event.time.strftime('%d.%b.%Y') %]
                    </td>
                    <td>
                        [% event.time.strftime('%T').match('00:00:00') # for legacy data:
                            ? '??:??:??' : event.time.strftime('%T') %]
                    </td>
                    <td>
                        [% event.user.username | upper %]
                    </td>
                    <td>
                        [% event.diagnosis.name | html %]
                    </td>
                    <td>
                        [% event.option.option_name | html %]
                    </td>
                </tr>
                [% END %]
            </table>
        </div>
    [% END %]
    
    <p>[% site.html.back_button %]</p>
<!-- END [% template.name %] -->
