    [% META title = 'Registration &raquo; Patient Search' -%]

<!-- BEGIN register/select_patient.tt -->
 	[% # PROCESS dumper.tt dump = patients %]
 	[% # PROCESS dumper.tt dump = results.cases_map %]
    [% # PROCESS dumper.tt dump = c.cfg('settings') %]
	<p>
		[% # 06register.t requires next line to output to single line:
			n = total_count; %]
		Found <b>[% n %]</b> record[% IF n != 1 %]s[% END %] matching <b>[% search_term %]</b>
		[ <a href="[% app_url %]/patient">NEW PATIENT</a> ]
	</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>LastName</th>
                <th>FirstName(s)</th>
                <th>Sex</th>
                <th>DoB [age]</th>
                <th>NHS No.</th>
                <td class="borderless">&nbsp;</td>
                <td class="borderless">&nbsp;</td>
			</tr>

		[%
			patients  = results.patients;
			cases_map = results.cases_map;

			BLOCK dob_age;
				patient.dob.strftime('%d.%b.%Y'); ' ['; age_now; ']';
			END;
			FOREACH patient IN patients; # PROCESS dumper.tt dump = patient;
		%]
            <tr>
                <td>[% patient.id %]</td>
                <td class="bkgd_light_yellow">
					[% patient.last_name | upper %]
				</td>
                <td class="bkgd_light_yellow">
                    <span>
						[% patient.first_name  | ucfirst %]
						[% patient.middle_name | ucfirst %]
					</span>
                </td>
                <td align="center" class="bkgd_light_yellow">
					[% patient.gender || site.html.grey_null %]
				</td>
                <td class="bkgd_light_yellow">
					[% IF patient.dob;
						age_now = calculate_age(patient.dob, c.time_now);
						class = age_now > 100 ? 'red' : 'normal';
					%]
						<span class="[% class %]">
							[% IF age_now > 100 %]
								<span class="acronym" title="check DoB">
									[% INCLUDE dob_age %]
								</span>
							[% ELSE; INCLUDE dob_age; END %]
						</span>
					[% ELSE; site.html.grey_null; END %]
				</td>
                <td class="bkgd_light_yellow">
					[% nhs_number = patient.nhs_number; INCLUDE site/nhs_number.tt %]
				</td>
                <td>
					[% IF c.user_can('edit_pid') %]
					<a class="href" href="[% app_url %]/patient/register_edit_patient/[% patient.id %]">
						<img class="href" src="/images/edit.jpg" alt="Edit" title="Edit patient" />
					</a>
					[% ELSE %]
						<img src="/images/edit.jpg" alt="Edit" />
					[% END %]
				</td>
				[% IF c.cfg('settings').disable_duplicate_patient_requests;
					# INCLUDE dumper.tt dump = results.cases_map.item(patient.id);
					existing_request_id = 0; # reset
					FOREACH r IN results.cases_map.item(patient.id); # only ever 1 or 0
                        # INCLUDE dumper.tt dump = r;
						existing_request_id = r.request.0.id IF r.request.size;
					END; END;
				%]
				<td class="borderless">
					[% IF existing_request_id %]
						<span class="warning">Request already registered</span>[
                            <a href="[% app_url %]/search/=/[% existing_request_id
                                %]?_return=1" title="view">
                                    <i class="fa fa-share-square-o fa-lg"></i>
                            </a> ]
					[% ELSE %]
						<a class="href" alt="Select" title = "Select patient"
						   href="[% app_url %]/patient/select_patient/[% patient.id %]">
							<img class="href" src="/images/select.png" alt="Select" />
						</a>
					[% END %]
	 			</td>
			</tr>

			<tr class="bold borderless">
				<td></td>
				<td colspan="5">
					<span class="italic-title">referral details</span>
					[% div_name = 'ID' _ patient.id;
						INCLUDE site/snippets/toggleview.tt %]
					<ol class="itemhidden" id="[% div_name %]">
					  [% FOREACH case IN cases_map.item(patient.id) %]
						<li class="register">
							[% location = case.referral_source.display_name;
								INCLUDE site/display_name.tt %] ::
							[% case.unit_number || site.html.grey_null %]
						</li>
					  [% END %]
					</ol>
				</td>
				<td colspan="3"></td>
            </tr>

		[% END %]
        </table>

	</div>
	[% PROCESS site/cgi_pager.tt %]
<!-- END register/select_patient.tt -->
