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

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

    [% multi_email_enabled = c.user_can('email_report') # and not too many selected:
        AND records.size < 11 ? 1 : 0 %]

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

	<p>
        Search returned <b>[% total_count %]</b> matches:
	</p>
    <div class="indent">
    <form name="form1" action="[% app_url %]/request/multi_email">
        <input type="hidden" name="return_url" value="[% c.query.self_url %]" />
    <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>
            [% IF multi_email_enabled %]<th>Email</th>[% END %]
        </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>
            [% IF multi_email_enabled %]
                <td align="center">
                    <input type="checkbox" name="multi_email_request_id"
                        value="[% data.id %]"
                        [% 'disabled=1' UNLESS var # var only set if authorised %] />
                </td>
            [% END %]
        </tr>
    [% END %]
    [% IF multi_email_enabled %]
        <tr class="borderless">
            <td colspan="11" align="right">[% site.html.go_btn %]</td>
        </tr>
    [% END %]
    </table>
    </form>
    </div>

    [% PROCESS site/cgi_pager.tt %]

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