<!-- BEGIN [% component.name %] -->
    [% messages = c.messages('demographics');
		# PROCESS dumper.tt dump = patient; 
		# PROCESS dumper.tt dump = pds_query;
        # PROCESS dumper.tt dump = pds_codes;
        # PROCESS dumper.tt dump = c.query; %]

    [% IF is_code_ref(pds_query).match('HASH') %]
        [% IF pds_query.ErrorCode.match('SMSP-0000');
            fields = [ 'last_name' 'first_name' 'middle_name' 'gender' 'dob' 'nhs_number' ];
        %]
        <div id="pas">
            <h4>PDS query
                [% IF pds_query.demographics_verified %]
                    <span class="info">[patient details verified]</span>
                [% ELSE %]
					<span class="red">[patient details unverified]</span>
                [% END %]
            </h4>
            <p class="pas">
                [% FOREACH field IN fields;
                    a = pds_query.item(field) | lower; # PROCESS dumper.tt dump = a;
                    b = ( field == 'dob' ) # string (new record) or DateTime object (existing):
                        ? ( patient.dob.ymd || patient.dob ) # ymd will be empty if dob = str
						: patient.item(field) | lower; # PROCESS dumper.tt dump = b;
                %]
    
                    [% IF a != b # highlight it %]
                        <span class="pas">[% pds_query.item(field) || '[NULL]' %]</span>
                    [% ELSIF pds_query.item(field); pds_query.item(field); ELSE; NEXT; END # skip if empty %]
                    [% ' :: ' IF loop.next # add separator unless end of array %]
                [% END %]
                
                [% IF pds_query.home_address; ' :: ' _ pds_query.home_address _ ', '
                    _ pds_query.post_code; END %]
            </p>
            
            [% IF pds_query.demographics_verified # only relevant for _check_new_patient() %]
				<input type="hidden" name="_pid_confirmed" value="1" />
			[% ELSIF is_new_patient || is_patient_edit # flag from Patient::add_new() or :: update_patient() %]
                <p class="bold">Patient details do not match PDS entry.
                    Please amend, or confirm details below are correct:
                    <input type="checkbox" name="_pid_confirmed" value="1" />
                </p>
			[% ELSE # have selected existing patient %]
				<p class="bold">Patient details do not match PDS entry. Do you
				need to edit selected patient?</p>
            [% END %]
        </div>
        
        [% ELSE; # PDS returned a not-OK error code:
            error_code = pds_query.ErrorCode;
            error_str  = pds_codes.item(error_code); # empty for a PDS timeout
        %]
            <p class="warning">PDS returned:
                <span class="highlight">[% error_code %] [% error_str %]</span>
                
                [% IF error_code.match('timeout') # LIMS error msg, not PDS error code %]
                    <a href="[% c.query.self_url %]">
                        <span style="margin-left: 5px">GO &#187;</span>
                    </a>
                [% ELSIF error_code.match('DEMOG-000[17]'); # no or multiple matches
                    IF patient.id;
                        uri = patient.id;
                    ELSE; params = [];
                        FOREACH entry IN ['last_name' 'first_name' 'nhs_number' 'gender'
                            'unit_number' 'year' 'month' 'day' 'referral_source_id'];
                            params.push(entry _ '=' _ patient.item(entry));
                        END;
                        uri = '?' _ params.join(';'); # need leading '?' or entire str taken as id
                    END; %]		
                        <a href="[% app_url %]/patient/pds_query/[% uri %]">
                            <span style="margin-left: 5px">REFINE SEARCH &#187;</span>
                        </a>
				[% ELSIF error_code.match('SMSP-0001'); # message validation error %]
					<span style="margin-left: 5px">PLEASE CHECK ALL FIELDS</span>
                [% END %]
            </p>
            [%  # allow PDS query override or no chance of registering if timeout or anon details:
				message = error_code.match('timeout')
					? 'patient demographic service appears to be off-line'					
					: 'this is an anonymised patient, or PDS cannot find a match'; 
			%]
            <p class="info indent">If [% message %], skip repeat PDS:
                <input type="checkbox" name="_skip_pds" value="1" />
            </p>
        [% END %]
    [% ELSE %]
        <p class="error">PDS did not return expected format: [% pds_query %]</p>
        <input type="hidden" name="_skip_pds" value="1" />[%# to break out of cycle %]
    [% END %]
<!-- END [% component.name %] -->
