<!-- BEGIN [% component.name %] -->

    [% # PROCESS dumper.tt dump = c.stash.errs %]
    [% # PROCESS dumper.tt dump = hmrn_data.treatments %]
    [% # PROCESS dumper.tt dump = hmrn_data.maps %]
	
	<script type="text/javascript">
		$(document).ready(function() {
			$("#referral").validationEngine()
		})
	</script>
	
	<h3 class="header">Referral pathway:</h3>
    
	[% IF hmrn_data.referrals.size %]	
    <table class="indent">
        <tr>
			<td class="borderless"></td>
			<th scope="col">Referral date</th>
			<th scope="col">Referral type</th>
 			<th scope="col">Referred from</th>
 			<th scope="col">Referred to</th>
 			<th scope="col">Date 1st seen</th>
        </tr>
		[% FOREACH episode IN hmrn_data.referrals %]
		<tr>
			<td>[% loop.count %]</td>
			<td>
				<input type="text" value="[% episode.referral_date.strftime('%d.%b.%Y') %]" [% site.html.readonly %] />
			</td>
			<td>
				<input type="text" value="[% episode.referral_type %]" [% site.html.readonly %] />
			</td>
			<td>
				<input type="text" value="[% episode.referred_from %]" [% site.html.readonly %] />
			</td>
			<td>
				<input type="text" value="[% episode.referred_to %]" [% site.html.readonly %] />
			</td>
			<td>
				<input type="text" value="[% episode.date_first_seen.strftime('%d.%b.%Y') %]"
					[% site.html.readonly %] />
			</td>
			[% IF user_can_enter_data %]
			<td>
				<a href="[% app_url %]/hmrn_data/edit_referral/[% data.id %]/[% episode.id %]">
					edit
				</a>
			</td>
			[% END %]
		</tr>
		[% END %]
	</table>
	[% ELSE %]
		<p class="indent info">No referral data recorded</p>
	[% END %]
	
	[% IF user_can_enter_data %]
    <p class="title">
		[% div_name = 'new_referral'; INCLUDE site/snippets/toggleview.tt %]
		Add new entry
	</p>

    [% IF # provide a name="error" for internal anchor:
        error_referral_date     ||
        error_referral_type_id  ||
        error_from_source_id    ||
        error_to_source_id      ||
        error_date_first_seen        
    %]<a name="error"><span>&nbsp;</span></a>[% END %]

	<div class="[% dfv_errors ? 'visible' : 'itemhidden' %]" id="[% div_name %]">

	[% # PROCESS dumper.tt dump = c.validate('hmrn_referral') # use this for validation %]
	
    <form method="post" name="referral" id="referral"
		action="[% app_url %]/hmrn_data/referral_pathway/[% data.id %]/[% patient.id %]">

	  <table class="indent">	
		<tr>
			<td class="label">Referral date:</td>
			<td class="content">
				<input type="text" name="referral_date" id="referral_date" size="10"
                    class="validate[ajax[ajaxDate]] datepicker" />
				[% error_referral_date %]
			</td>
		</tr>

		<tr>
			<td class="label">Referral type:</td>
			<td class="content">
				<select name="referral_type_id" class="validate[required]"
						id="referral_type_id">
					<option value=""></option>
					[% FOREACH key IN hmrn_data.maps.referral_types.keys.sort %]
                    <option value="[% hmrn_data.maps.referral_types.$key %]">
                        [% key %]
					</option>
					[% END %]
				</select>
				[% error_referral_type_id %]
			</td>
		</tr>        
		<tr>
			<td class="label">Referred from:</td>
			<td class="content">
				<select name="from_source_id" id="from_source_id" class="validate[required]">
					<option value=""></option>
					[% FOREACH key IN hmrn_data.maps.referral_sources.keys.sort %]
                    <option value="[% hmrn_data.maps.referral_sources.$key %]">
                        [% key %]
					</option>
					[% END %]
				</select>
				[% error_from_source_id %]				
			</td>
		</tr>
		<tr>
			<td class="label">Referred to:</td>
			<td class="content">
				<select name="to_source_id" id="to_source_id" class="validate[required]">
					<option value=""></option>
					[% FOREACH key IN hmrn_data.maps.referral_sources.keys.sort %]
                    <option value="[% hmrn_data.maps.referral_sources.$key %]">
                        [% key %]
					</option>
					[% END %]
				</select>
				[% error_to_source_id %]				
			</td>
		</tr>
		<tr>
			<td class="label">Date 1st seen:</td>
			<td class="content">
				<input type="text" name="date_first_seen" id="date_first_seen" size="10"
                    class="validate[ajax[ajaxDate]] datepicker" />
				[% error_date_first_seen %]
			</td>
		</tr>
	
		[% INCLUDE hmrn_submit %]
      </table>
    </form>
	</div>
	[% END %]
<!-- END [% component.name %] -->