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

<form action="[% app_url %]/patient/update_patient/[% patient.id %]" method="post"
	name="patient_edit" id="patient_edit" [% js_edit_patient_onsubmit %]>
    
  [% 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 %]

    [% 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="info">Usage: select another patient record or edit current record</p>
        
        <hr />
        <table class="indent">
            <tr class="borderless">
                <td colspan="6" class="highlight">Other patient records:</td>
            </tr>
            <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">
        [% IF similar_entries.patients.size # to distinguish from previous %]
            <tr class="borderless">
                <td colspan="2" class="highlight">Current patient record:</td>
            </tr>
        [% END %]
            <tr>
                <td class="label">Last Name:</td>
                <td class="content">
                    <input class="field" type="text" name="last_name" size="40"
                        value="[% ( c.query.param('last_name') OR patient.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="[% ( c.query.param('first_name') OR patient.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="[% ( c.query.param('middle_name') OR patient.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="[% c.query.param('nhs_number') OR patient.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="[% c.query.param('day') OR dob.strftime('%d') %]"
						[% PROCESS validate_date %] maxlength="2" class="[% class %]"
						[% site.html.on_focus %] />
    
                    <input type="text" name="month" size="2" id="is_valid_date_month"
                        value="[% c.query.param('month') OR dob.strftime('%m') %]"
						[% PROCESS validate_date %] maxlength="2" class="[% class %]"
						[% site.html.on_focus %] />
    
                    <input type="text" name="year" size="4" id="is_valid_date_year"
                        value="[% c.query.param('year') OR dob.strftime('%Y') %]"
						[% 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 />
		
		<ul>
		  [% UNLESS registration_edit # can't select request at registration %]
			<li class="bold">Change to affect this request only 
				[% div_name = 'this_request';
					INCLUDE site/snippets/toggleview.tt %]
				<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>
			</li>
		  [% END %]
	
			<li class="bold">Change to affect all requests attached to this patient
				[%  div_name = 'all_requests';
					class = registration_edit ? 'itemshow' : 'itemhidden';
					INCLUDE site/snippets/toggleview.tt %]
				<div class="[% class %] indent" id="[% div_name %]">
					<p>
						<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>
					<p class="indent">
						Confirm: <input type="checkbox" name="all_records" value="1" />
						[% UNLESS registration_edit %]
							<span class="error">&#171; this action is NOT reversible</span>
						[% END %]
					</p>
				</div>
			</li>
		</ul>
		
		<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 %]
	
	[% IF is_pds_exempt AND NOT pds_query # exempt names / trials never get pds.tt to allow override %]
		<input type="hidden" name="_skip_pds" value="1" />
	[% END %]
</form>
<!-- END patient/edit_patient.tt -->