[% META title = 'Patient &raquo; PDS Query' -%]
<!-- BEGIN [% template.name %] -->
	[% # PROCESS dumper.tt dump = patient.as_tree %]
	
  [% IF pds_query; # PROCESS dumper.tt dump = pds_query;
		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_code_ref(pds_query).match('HASH'); %]
      <h3 class="header">PDS query results</h3>

      [% IF pds_query.ErrorCode.match('SMSP-0000'); %]
        <table class="indent">
          <tr>
            <th>LastName</th>
            <th>FirstName</th>
            <th>DoB</th>
            <th>Gender</th>
            <th>NHSNo</th>
            <th>Address</th>
            <th>Post Code</th>
            <td class="borderless"></td>
          </tr>
          <tr>
            <td>[% pds_query.last_name %]</td>
            <td>[% pds_query.first_name %]</td>
            <td>[% pds_query.dob %]</td>
            <td>[% pds_query.gender %]</td>
            <td>[% pds_query.nhs_number %]</td>
            <td>[% pds_query.home_address %]</td>
            <td>[% pds_query.post_code %]</td>
            <td>
              [%	# split DoB into components:
                ymd = pds_query.dob.split('-'); # PROCESS dumper.tt dump = ymd;

                params = {
                  last_name		=> pds_query.last_name,
                  first_name	=> pds_query.first_name,
                  middle_name	=> pds_query.middle_name,
                  nhs_number	=> pds_query.nhs_number,
                  gender      => pds_query.gender,
                  year			=> ymd.0,
                  month			=> ymd.1,
                  day				=> ymd.2,
                  # add ref_src_id passed in via hidden field:
                  referral_source_id => c.query.param('referral_source_id'),
                };
              %]
              [%
                IF patient.id; # ie patient exists in patient table
                  my_params = [];
                  fields = [ 'last_name' 'first_name' 'middle_name' 'nhs_number'
                          'gender' 'year' 'month' 'day' ];
                  FOREACH entry IN fields; # only want selected fields from params:
                    my_params.push(entry _ '=' _ params.$entry);
                  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>
         
          <tr>
              <td colspan="8" align="right" class="borderless">
                <span class="info">Not the above -
                  <a href="[% app_url %]/patient[% return_link %]?_skip_pds=1">
                    continue &#187;
                  </a>
                </span>
            </td>
          </tr>
        </table>
      [% ELSE; # PROCESS dumper.tt dump = pds_query; # pds_query.Error Code isn't SMSP-0000
        error_code = pds_query.ErrorCode;
        error_str  = pds_codes.item(error_code); # empty for a PDS timeout
      %]
        <p class="indent">
          <span class="warning">PDS returned:</span>
          <span class="highlight">[% error_code %] [% error_str %]</span>
          <span class="bold">
            [% IF error_code.match('timeout') # LIMS error msg, not PDS error code %]
              please repeat query below, or 
              <a href="[% app_url %]/patient[% return_link %]?_skip_pds=1">
                skip PDS query &#187;
              </a>
            [% ELSE %]please refine query and repeat:[% END %]
          </span>
        </p>
      [% END %]
    [% ELSE # pds_query isn't an arrayref %]
        <p class="info">[% pds_query %]
          No PDS matches found :: <!-- // include flag to skip PAS query: -->
          <a href="[% app_url %]/patient[% return_link %]?_skip_pas=1">
            continue &#187;
          </a>
    		</p>
    [% END %]
  [% END %]
  
  <h3 class="header">PDS 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 indent">Mandatory fields last-name, gender &amp; dob for PDS query:</p>
    
  [% BLOCK row %]
    <tr>        
      <td class="[% required ? 'red' : 'normal' %]">[% label %]:</td>
      <td>
       	<input type="text" name="[% field %]" size="10" id="[% field %]" />
    </td>
		<td>
		[% IF patient.$field; # requires var (not item) for dob to work
        value = ( field == 'dob' )
          ? patient.dob.ymd : patient.item(field); %]
				<a href="#" onclick="javascript:set_proposed('#[% field %]', 
					'[% value %]'); return false">&#171;</a>
				[% value %]
			[% END %]
			</td>
    </tr>
  [% END %]
    
  <form method="post" action="[% app_url %]/patient/do_pds_search">
    <input type="hidden" name="_patient_id" value="[% patient.id %]" />    
  	<input type="hidden" name="referral_source_id"
			value="[% c.query.param('referral_source_id') %]" />

    <table class="indent borderless">
      [% label = 'Last name';     field = 'last_name';  required = 1; INCLUDE row; %]
      [% label = 'First name';    field = 'first_name'; required = 0; INCLUDE row  %]
      [% label = 'Date of birth'; field = 'dob';        required = 1; INCLUDE row; %]
      [% label = 'NHS number';    field = 'nhs_number'; required = 0; INCLUDE row; %]

<!-- // PDS doesn't use this:
      <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 class="red">Gender:</td>
				<td>
					<input type="text" name="gender" size="10" id="gender" />
				</td>
        <td>
				[% IF patient.gender.match('M|F') # 'U' no use for PDS %]
					<a href="#" onclick="javascript:set_proposed('#gender', 
						'[% patient.gender %]')">&#171;</a> [% patient.gender %]
        [% ELSE; FOREACH entry IN [ 'M' 'F' ] %]
          <a href="#" onclick="javascript:set_proposed('#gender', 
            '[% entry %]')">&#171;</a> [% entry %]<br />[% END %]	
				[% END %]
				</td>
      </tr>
      <tr>        
        <td>Post code:</td>
        <td>
					<input type="text" name="post_code" size="10" />
				</td>
        <td>
					<input type="submit" name="_is_pds_search" value="Go" class="button" />
				</td>
      </tr>        
    </table>

    <p>[% site.html.back_button %]</p>
  </form>
<!-- END [% template.name %] -->