<!-- BEGIN patient/edit_patient.tt -->
[% META title = 'Patient &raquo; Edit Details' -%]

  [% PROCESS patient/pas.tt IF pas_query %]
  [% PROCESS patient/pds.tt IF pds_query %]
  
  [% # PROCESS dumper.tt dump = request.as_tree %]
  [% # PROCESS dumper.tt dump = pas_query %]
  [% # PROCESS dumper.tt dump = pds_query %]
  [% # PROCESS dumper.tt dump = c.query %]
  [% # PROCESS dumper.tt dump = c.stash.errs %]

    <h2 class="header">Edit patient details</h2>

    <div class="error">WARNING: DO NOT USE FOR CHANGING PATIENT IDENTITY</div>

    <p class="warning">
        <strong>If this is not the correct patient, delete &amp; re-register request instead.</strong>
    </p>

    <p>[% site.html.back_button %]</p>

	[% # PROCESS dumper.tt dump = c.stash.errs %]
	[% # PROCESS dumper.tt dump = similar_entries %]

    [% js_edit_patient_dfv_js %]

    [% IF dfv_errors %]
        <p class="error">[% c.cfg('msg').dfv_errors %]</p>
        [% IF error_patient_id %]
            <ul><li>[% c.cfg('msg').require_one %]</li></ul>
        [% ELSIF error_confirm_change %]
            <ul><li>[% c.cfg('msg').confirm_change %]</li></ul>
        [% END %]
    [% END %]

<form action="[% app_url %]/patient/update_patient/[% patient.id %]" method="post"
	name="patient_edit" id="patient_edit" [% js_edit_patient_onsubmit %]>
    
    [% IF request.id # set hidden flag %]
        <input type="hidden" name="request_id" value="[% request.id %]" />
    [% ELSIF registration_edit %]
        <input type="hidden" name="registration_edit" value="1" />
    [% END %]

    [% IF similar_entries.patients.size %]
        <p class="title">Select a previous record or edit current record:</p>
             <table class="indent">
                <tr>
                    <th scope="col">ID</th>
                    <th scope="col">Last name</th>
                    <th scope="col">First name(s)</th>
                    <th scope="col">D.o.B</th>
                    <th scope="col">M/F</th>
                    <th scope="col">NHS number</th>
                </tr>
            [%
				patients  = similar_entries.patients;
				cases_map = similar_entries.cases_map;

				FOREACH entry IN patients; # PROCESS dumper.tt dump = entry
			%]
                <tr>
                    <td class="content">[% entry.id %]</td>
                    <td class="bkgd_light_yellow">[% entry.last_name | upper %]</td>
                    <td class="bkgd_light_yellow">
                        [% entry.first_name  | ucfirst %]
                        [% entry.middle_name | ucfirst %]
                    </td>
                    <td class="bkgd_light_yellow">
                        [% entry.dob.strftime('%d.%b.%Y') || site.html.grey_null %]
                    </td>
                    <td class="bkgd_light_yellow align="center">
						[% entry.gender || 'U' %]
					</td>
                    <td class="bkgd_light_yellow">
                        [% entry.nhs_number || site.html.grey_null %]
                    </td>
                    <td class="borderless">
                        <input type="radio" name="use_patient_id"
                            value="[% entry.id %]" />
                        <span class="maroon">&#171; select</span>
                    </td>
                </tr>
				
				<tr class="bold borderless">
					<td></td>
					<td colspan="3">
						<span class="italic-title">referral details</span>
						[% div_name = 'ID' _ entry.id;
							INCLUDE site/snippets/toggleview.tt %]
						<ol class="itemshown" id="[% div_name %]">
						  [% FOREACH case IN cases_map.item(entry.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>
    [% END %]

    <div id="request_edit" style="padding: 10px 0px">
        <table class="indent">
            <tr>
                <td class="label">Last Name:</td>
                <td class="content">
                    <input class="field" type="text" name="last_name" size="40"
                        value="[% ( patient.last_name OR c.query.param('last_name') ) | upper %]"
                        [% site.html.on_focus %]  /> [% error_last_name %]
                </td>
            </tr>

            <tr>
                <td class="label">First Name:</td>
                <td class="content">
                    <input class="field" type="text" name="first_name" size="40"
                        value="[% ( patient.first_name OR c.query.param('first_name') ) | ucfirst %]"
						[% site.html.on_focus %] />
                    [% error.first_name %]
                </td>
            </tr>

            <tr>
                <td class="label">Middle Name(s):</td>
                <td class="content">
                    <input class="field" type="text" name="middle_name" size="40"
                        value="[% ( patient.middle_name OR c.query.param('middle_name') ) | ucfirst%]"
						[% site.html.on_focus %] />
                    [% error_middle_name %]
                </td>
            </tr>

            <tr>
                [% SET class = error_patient_id ? 'input_error' : 'field' %]
                <td class="label">NHS No:</td>
                <td class="content">
                    [% # jQuery: onBlur="validate_nhsno(this.value); return true;" %]
                    <input class="[% class %]" type="text" name="nhs_number" size="12"
                        id="is_valid_and_unique_nhsno"
						value="[% patient.nhs_number OR c.query.param('nhs_number') %]"
                        [% IF c.cfg('use_cgi_ajax') %]
                        onChange="do_ajax(['is_valid_and_unique_nhsno'], ['bad_nhsno']); return true;"
                        [% END %]
                    [% readonly || site.html.on_focus %] />
                    [% error_nhs_number %]
                    <span class="ajax_error"><em id="bad_nhsno"></em></span>
                </td>
            </tr>

            [% BLOCK validate_date; IF c.cfg('use_cgi_ajax') %]
                onChange="do_ajax(
                    ['is_valid_date_day','is_valid_date_month','is_valid_date_year'],
                    ['bad_date']
                ); return true;"
            [% END; END %]

            <tr>
                [% SET class = error_year || error_day ? 'input_error' : 'field' %]
                [% SET dob = patient.dob %]
    
                <td class="label">DoB:</td>
                <td class="content">
                    <input type="text" name="day" size="2" id="is_valid_date_day"
                        value="[% dob.strftime('%d') OR c.query.param('day') %]"
						[% PROCESS validate_date %] maxlength="2" class="[% class %]"
						[% site.html.on_focus %] />
    
                    <input type="text" name="month" size="2" id="is_valid_date_month"
                        value="[% dob.strftime('%m') OR c.query.param('month') %]"
						[% PROCESS validate_date %] maxlength="2" class="[% class %]"
						[% site.html.on_focus %] />
    
                    <input type="text" name="year" size="4" id="is_valid_date_year"
                        value="[% dob.strftime('%Y') OR c.query.param('year') %]"
						[% PROCESS validate_date %] maxlength="4" class="[% class %]"
						[% site.html.on_focus %] />
    
                    [% UNLESS error_day || error_year # set if date invalid %]
                        <span class="ajax_error"><em id="bad_date">dd-mm-yyyy</em></span>
                    [% END %]
                    [% error_day || error_year # set if date invalid %]
                </td>
            </tr>

            <tr>
                <td class="label">Gender:</td>
                <td class="content">
                    [% gender = c.query.param('gender') || patient.gender; # query param takes precedence
                        FOREACH entry = ['M','F','U'];
                    %]
                        <input type="radio" name="gender" value="[% entry %]"
                            [% 'checked' IF entry.match(gender) %] /> [% entry %]
                    [% END %]
                [% UNLESS gender.match('M|F') %]
                    <span class="red">&#171; check this</span>
                [% END %]
                    [% this_field.error %]
                </td>
            </tr>
        </table>
    </div>
    
    [% IF count > 1 %]
        <p class="bold">
            <span class="highlight">
                WARNING: change affects
                [% IF ! registration_edit; count - 1; %] other
                [% ELSE; count; END; %]
                record[% 's' IF count > 2 %]
            </span>
        </p>
		
		<hr />
		<p class="bold">1. Change to affect this request only 
		[% div_name = 'this_request';
			INCLUDE site/snippets/toggleview.tt %]</p>
		<div class="itemhidden" id="[% div_name %]">
			<p class="indent info">
				Confirm change to [% PROCESS site/lab_number.tt data = request %] only:
				<input type="checkbox" name="this_record_only" value="1" />
			</p>
		</div>
		
		<p class="bold">2. Change to affect all requests attached to this patient
		[% div_name = 'all_requests';
			INCLUDE site/snippets/toggleview.tt %]</p>
		<div class="itemhidden indent" id="[% div_name %]">
            <img src="/images/stop.gif" alt="STOP" />
            <span class="warning">
                I understand I am about to change ALL [[% count %]] records belonging to
                    [% patient.first_name | ucfirst %]
                    [% patient.middle_name | ucfirst %]
                    [% patient.last_name | upper %] ::
                    [% patient.dob.dmy('/')  %] ::
                    [% patient.nhs_number %]
            </span>
            <img src="/images/stop.gif" alt="STOP" />
			<p class="indent">
				Confirm: <input type="checkbox" name="all_records" value="1" />
				<span class="error">&#171; this action is NOT reversible</span>
			</p>
		</div>
		<hr />
    [% ELSE # send confirmation as hidden field; all_records OK as only 1 anyway %]
        <input type="hidden" name="all_records" value="1" />
        
        [% IF ! count # can delete patient: %]
        <p class="title">
          [ <a href="[% app_url %]/patient/delete_patient/[% patient.id %]">DELETE PATIENT</a> ]
        </p>
        [% ELSE %]
            <p class="info">
                <span class="indent">INFO: change affects this record only</span>
            </p>
        [% END %]
    [% END %]
    
	[% PROCESS site/reason_for_change.tt %]
</form>
<!-- END patient/edit_patient.tt -->