[% META title = 'Patient &raquo; PAS Query' -%]
<!-- BEGIN [% template.name %] -->
	[% # PROCESS dumper.tt dump = patient %]
	[% # PROCESS dumper.tt dump = unit_numbers %]
	
    <h3>PAS query</h3>
    
	[% # PROCESS site/js/jquery.tt # already loaded in html_wrapper %]
	<script language="javascript" type="text/javascript">
	<!--
		function set_proposed(id, proposed) { $(id).val(proposed) }
	-->
	</script>
	
	[% # TODO: ? tack request_id on end of url to redirect back to request %]

    <p class="info">Require minumum of 2 fields for PAS search:</p>
    
    <form method="post" action="[% app_url %]/pas/do_search">
        <table class="indent borderless">
            <tr>        
                <td>Last name:</td>
                <td>
					<input type="text" name="last_name" size="10" id="last_name" />
				</td>
				<td>
				[% IF patient.last_name %]
					<a href="#" onclick="javascript:set_proposed('#last_name', 
						'[% patient.last_name %]'); return false">&#171;</a>
					[% patient.last_name %]
				[% END %]
				</td>
            </tr>
            <tr>
				<td>First name:</td>
                <td>
					<input type="text" name="first_name" size="10" id="first_name" />
				</td>
				<td>
				[% IF patient.first_name %]
					<a href="#" onclick="javascript:set_proposed('#first_name', 
						'[% patient.first_name %]')">&#171;</a> [% patient.first_name %]
				[% END %]
				</td>
			</tr>
			<tr>
				<td>Date of Birth:</td>
				<td>
					<input type="text" name="dob" size="10" id="dob" />
                </td>
				<td>
					[% IF patient.dob %]
					<a href="#" onclick="javascript:set_proposed('#dob', 
						'[% patient.dob.ymd %]')">&#171;</a> [% patient.dob.ymd %]					
					[% END %]
				</td>
            </tr>
            <tr>
				<td>Unit Number:</td>
                <td>
					<input type="text" name="unit_number" size="10" id="unit_number" />
				</td>				
				<td>
				[% IF unit_numbers.size; FOREACH entry IN unit_numbers %]
				<a href="#" onclick="javascript:set_proposed('#unit_number', 
					'[% entry %]')">&#171;</a> [% entry %]<br />[% END %]					
				[% ELSIF patient.unit_number # return from PAS %]foo
				<a href="#" onclick="javascript:set_proposed('#unit_number', 
					'[% entry %]')">&#171;</a> [% entry %]<br />				
                [% END %]					
				</td>
            </tr>
            <tr>
				<td>NHS Number:</td>
				<td>
					<input type="text" name="nhs_number" size="10"id="nhs_number" />
				</td>
                <td>
				[% IF patient.nhs_number %]
					<a href="#" onclick="javascript:set_proposed('#nhs_number', 
						'[% patient.nhs_number %]')">&#171;</a> [% patient.nhs_number %]
				[% END %]
				</td>
            </tr>
            <tr>        
                <td>Post code:</td>
                <td>
					<input type="text" name="post_code" size="10" />
				</td>
                <td>
					<input type="hidden" name="referral_source_id"
						value="[% c.query.param('referral_source_id') %]" />
					<input type="submit" name="_do_pas_search" value="Go" class="button" />
				</td>
            </tr>        
        </table>
            
        <p>
            [% site.html.back_button %]              
        </p>
        
        <input type="hidden" name="LN" value="[% c.query.param('LN') %]" />    
        <input type="hidden" name="_patient_id" value="[% patient.id %]" />    
    </form>

    [% IF pas_results; # PROCESS dumper.tt dump = pas_results;
		patient_id = c.query.param('_patient_id'); # means matching patient found 
			return_link = patient_id
				? '/select_patient/' _ patient_id # back to registration form
				: ''; # triggers new patient registration
	%]
        [% IF is_arrayref(pas_results) %]
        <h3>
            PAS query results [[% pas_results.size %] ]:
        </h3>
    
        <table>
        <tr>
            <th>LastName</th>
            <th>FirstName</th>
            <th>DoB</th>
            <th>PatientNo</th>
            <th>NHSNo</th>
            <th>Address</th>
            <th>Post Code</th>
            <td class="borderless"></td>
        </tr>
        [% FOREACH entry IN pas_results %]  [% # PROCESS dumper.tt dump = entry %]
        <tr>
            <td>[% entry.last_name %]</td>
            <td>[% entry.first_name %]</td>
            <td>[% entry.dob %]</td>
            <td>[% entry.unit_number %]</td>
            <td>[% entry.nhs_number %]</td>
            <td>[% entry.address %]</td>
            <td>[% entry.zip %]</td>
            <td>
                [%	# separate DoB & FirstName:
					names   = entry.first_name.split(' '); # PROCESS dumper.tt dump = names;
					dob_ymd = entry.dob.split('-');        # PROCESS dumper.tt dump = dob_ymd;
					
					params = {
						last_name		=> entry.last_name,
						first_name		=> names.0,
						middle_name		=> names.1,
						nhs_number		=> entry.nhs_number,
						unit_number		=> entry.unit_number,
						year			=> dob_ymd.0,
						month			=> dob_ymd.1,
						day				=> dob_ymd.2,
						# add ref_src_id passed in via hidden field:
						referral_source_id => c.query.param('referral_source_id'),
					};
					fields = [ 'last_name' 'first_name' 'middle_name' 'nhs_number'
							'year' 'month' 'day' ]
                %]
				[% IF patient.id; # ie patient exists in patient table
					my_params = [];
					FOREACH param IN fields; # only want selected fields from params:
						my_params.push(param _ '=' _ params.$param);
					END;
				%]
				<a href="[% app_url %]/patient/edit_patient/[% patient.id %]?[% my_params.join(';') %]"
                     class="href">
					<img class="href" src="/images/edit.jpg" alt="Edit" title="Edit patient">
				</a>
				[% ELSE;
					my_params = [];
					FOREACH pair IN params.pairs; # want all params:
						my_params.push(pair.key _ '=' _ pair.value);
					END;
				%]
				<a href="[% app_url %]/patient?[% my_params.join(';') %]" class="href">
					<img class="href" src="/images/record.jpg" alt="Register"
						title="Register patient">
				</a>
				[% END %]
            </td>
        </tr>
        [% END %]
		<tr>
			<td colspan="8" align="right" class="borderless">
				<span class="info">None of the above ::
					<a href="[% app_url %]/patient[% return_link %]?_skip_pas=1">
						continue &#187;
					</a>
				</span>
			</td>
		</tr>
        </table>
        [% ELSE # pas_results isn't an arrayref %]
            <p class="info">
				No PAS matches found :: <!-- // include flag to skip PAS query: -->
				<a href="[% app_url %]/patient[% return_link %]?_skip_pas=1">
					continue &#187;
				</a>
			</p>
        [% END %]
    [% END %]
<!-- END [% template.name %] -->