	[% META title="Record Search" %]
<!-- BEGIN [% template.name %] -->

    [% # PROCESS dumper.tt dump = histories %]
    [% # PROCESS dumper.tt dump = request_opts %]

    [% IF c.query.param('_return') %]
        <div>[% site.html.back_button %]</div>
    [% END %]

	<p>
        Search returned <b>[% total_count %]</b> matches:
	</p>
    <div class="indent">
    <table>
        <tr>
            <th>
			[% q = c.query;
				IF c.user_can('patient_merge') AND (
					q.param('last_name') OR q.param('unit_number') OR q.param('year')
				);
			%]
				<a href="[% app_url %]/admin/patient_merge/search?last_name=[%
					q.param('last_name') %];first_name=[% q.param('first_name')
					%];unit_number=[% q.param('unit_number') %];year=[% q.param('year')
					%];month=[% q.param('month') %];day=[% q.param('day') %]"
					target="_blank">
						<span class="acronym" title="merge patients">id</span>
				</a>
			[% ELSE %]id
			[% END %]
			</th>
            <th>Name</th>
            <th>Lab No.</th>
            <th>DoB</th>
            <th>Unit Number</th>
            <th>NHS Number</th>
            <th>Date</th>
            <th>Source</th>
            <th>Sample</th>
            <th>*</th>
        </tr>

    [% FOREACH data IN records; # PROCESS dumper.tt dump = data.as_tree;
        patient = data.patient_case.patient %]
        <tr>
            <td>[% patient.id %]</td>
            <td>
                [% patient.last_name | upper %], 
                [% format_firstname(patient) || patient.first_name | ucfirst %]
                [% patient.middle_name | ucfirst %]
            </td>
            <td>[% PROCESS site/lab_number.tt want_url = 1 # expects request object 'data' %]
            </td>
            <td>[% patient.dob.strftime('%d.%b.%Y') || site.html.grey_null %]</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>
				[% data.created_at.strftime('%d.%b.%Y')
					# history_map.item(data.id).strftime('%d.%b.%Y') %]
			</td>
            <td>
                [% SET location = data.patient_case.referral_source.display_name %]
                [% PROCESS site/display_name.tt %]
            </td>
            <td>
                [% specimen_map.item(data.id).sample_code.join(', ') %]
                [% IF c.is_lab_staff AND request_opts.item(data.id).doi;
                    INCLUDE site/snippets/doi.tt; END %]
            </td>
            <td>
                [%
                    request_status = data.status_option.description;
                    IF request_status == 'new'; 'U';
                    ELSIF request_status == 'screened'; 'S';
                    ELSIF request_status == 'reported'; 'R';
                    ELSE; # complete or authorised - show diagnosis:
                        diagnosis = data.request_report.diagnosis.name;                    
                        diagnosis_status = data.request_report.status;
                        h = {
                            new = 'n',
                            relapse = 'r',
                        };
                        var = h.item(diagnosis_status) || 'a'; # default
                    %]
                        <span class="acronym" title="[% diagnosis %]">
                            [% IF c.user_can('print_all'); # direct link to print: %]
                            <a href="[% app_url %]/request/print_record/[% data.id %]">
                                [% var | upper %]
                            </a>
                            [% ELSE; var | upper; END %]
                        </span>
                [% END %]
            </td>
        </tr>
    [% END %]
    </table>
    </div>

    [% PROCESS site/cgi_pager.tt %]
    
    <p>* (U)nscreened, (S)creened, (R)eported, (A)uthorised, (N)ew diagnosis</p>
<!-- END [% template.name %] -->