  [% META title = 'Admin &raquo; Report Notification' -%]

<!-- BEGIN [% template.name %] -->
	[% # PROCESS dumper.tt dump = ec.as_tree FOREACH ec IN email_contacts %]
	[% # PROCESS dumper.tt dump = departments %]
  [% # PROCESS dumper.tt dump = get_parent(33) %]
  [% # PROCESS dumper.tt dump = c.stash.errs %]

  [% report_notifications_dfv_js %]

	[% meta_data = c.get_meta_data('ReportNotification'); # INCLUDE dumper.tt dump = meta_data %]

    <p class="title">
		[% div_name = 'new'; INCLUDE site/snippets/toggleview.tt %]
		Add new entry
	</p>

	[% INCLUDE site/snippets/help.tt file = 'reportnotification' %]

	<div class="[% dfv_errors ? 'visible' : 'itemhidden' %]" id="[% div_name %]">
		<form method="post" action="[% app_url %]/config/report-notifications/update"
			[% report_notifications_onsubmit %] name="report_notification"
              id="report-notification">
		<div class="indent">
			<table>
				<tr>
					<td class="label">Display name:</td>
					<td class="content">
                    <input type="text" name="name" value="" size="40" />
						[% error_name %] [validate]
 					</td>
				</tr>
				<tr>
					<td class="label">Type:</td>
					<td class="content">
						<select name="type" [% site.html.onfocus %] id="type">
						<option value=""></option>
						[% FOREACH item IN meta_data.column('type').check_in;
							NEXT IF item.match('referrer') # self-registration for referrers %]
                 <option value="[% item %]">[% item %]</option>
						[% END %]
						</select>
						[% error_type %]
 					</td>
				</tr>

		[% PROCESS site/autosuggest.tt %]
		<tr>
			<td class="label">Location:</td>
			<td class="content">
				<input style="width: 300px" type="text" id="location_name" />
				<input type="hidden" id="source_id" name="referral_source_id" />
			</td>
		</tr>

		  <tr>
        <td class="label">Department:</td>
				<td class="content">
          <select name="department_id">
            <option value="">All departments (default) or select one:</option>
            [% FOREACH d IN departments.keys.sort;
              # INCLUDE dumper.tt dump = [d, departments.$d] %]
              <option value="[% departments.$d %]">[% d %]</option>
            [% END %]
          </select>
        </td>
      </tr>

				<tr>
					<td class="label">Status:</td>
					<td class="content">
						<select name="type" [% site.html.onfocus %] id="type">
						<option value=""></option>
						[% FOREACH item IN meta_data.column('status').check_in %]
                 <option value="[% item %]">[% item %]</option>
						[% END %]
						</select> [diagnosis status 'new' or 'all']
						[% error_status %]
 					</td>
				</tr>

				<tr>
					<td class="label">Email address:</td>
					<td class="content">
                    <input type="text" name="contact_address" value="" size="50" />
						[% error_contact_address %]
 					</td>
				</tr>
				<tr>
					<td class="label">Active:</td>
					<td class="content">
						<select name="is_active" [% site.html.onfocus %]>
						[% FOREACH option IN ['yes', 'no'] %]
						 <option value="[% option %]">[% option %]</option>
						[% END %]
						</select>
						[% error_is_active %]
 					</td>
				</tr>
    </table>

			<p class="indent">[% site.html.submit %] [% site.html.reset %]</p>
		</div>
		</form>
	</div>

    [% IF email_contacts.size # only empty if new db %]
	<hr />
	<form action="[% c.query.self_url %]" method="get">
		<div class="indent">
			<span class="bold">Filter:</span>
			[% site.html.go_btn %]
			<div class="indent">
				<label for="referrer">
					<input type="radio" name="filter" value="referrer" id="referrer"
						[% 'checked' IF c.query.param('filter').match('referrer') %] />
                    Referrers
				</label>
			</div>
			<div class="indent">
				<label for="location">
					<input type="radio" name="filter" value="location" id="location"
					[% 'checked' IF c.query.param('filter').match('location') %] />
                    Locations
				</label>
			</div>
			<div class="indent">
				<label for="all">
					<input type="radio" name="filter" value="" id="all"
					[% 'checked' UNLESS c.query.param('filter') %] />
                    Display all
				</label>
			</div>
		</div>
	</form>

	<p class="spacer"></p>

	<div class="[% dfv_errors ? 'itemhidden' : 'visible' %]">
		<table>
			<tr>
				<th></th>
				<th>Display Name</th>
				<th>Type</th>
				[% UNLESS c.query.param('filter').match('referrer') # irrelevant for referrer %]
					<th>Department</th>
				[% END %]
				<th>Identifier/Referral Source</th>
                <th>Status</th>
				<th>Email Address</th>
				<th>Active</th>
			</tr>

			[% FOREACH ec IN email_contacts; # PROCESS dumper.tt dump = ec.as_tree;
    			  NEXT IF c.query.param('filter').match('referrer') AND NOT ec.type.match('referrer');
				  NEXT IF c.query.param('filter').match('location') AND ec.type.match('referrer');
			%]
			<tr>
				<td>[% i = ( i || 0 ) + 1; i # can't use loop.count if using filter %]</td>
				<td>
					<input type="text" value="[% ec.name | html %]"
						[% site.html.disabled %] />
				</td>
				<td>
					<input type="text" value="[% ec.type %]" size="10"
                        [% site.html.disabled %] />
				</td>
				[% UNLESS c.query.param('filter').match('referrer') # irrelevant for referrers %]
				  <td>
						[% IF ec.type.match('referrer'); '[NOT APPLICABLE]'; ELSE; # if no filter selected
								FOREACH d IN departments;
                  NEXT UNLESS d.value == ec.department_id %]
								<input type="text" value="[% d.key %]" [% site.html.disabled %] />
                [% END; END %]
				  </td>
				[% END %]
                <td>
                    [%
                        IF ec.type.match('referrer');
                            display = ec.identifier | html;
                        ELSIF ec.type.match('hospital');
                            display = get_ref_src(ec.identifier);
                        ELSIF ec.type.match('organisation');
                            display = get_parent(ec.identifier);
                        END;
                    %]
					[% INCLUDE site/truncate.tt str = display %]
                 </td>
				<td>
					<input type="text" value="[% ec.status %]" size="5"
                        [% site.html.disabled %] />
				</td>
				<td>
					<input type="text" value="[% ec.contact_address %]" size="30"
                        [% site.html.disabled %] />
				</td>
				<td>
					<select name="is_unique" [% site.html.disabled %]>
					[% FOREACH option IN ['yes', 'no'] %]
						 <option value="[% option %]" [% 'selected' IF option == ec.is_active %]>
							[% option %]
						</option>
					[% END %]
					</select>
				</td>
				<td align="center">
					<a href="[% app_url %]/config/report-notifications/edit/[% ec.id %]">Edit</a>
				</td>
			 </tr>
			[% END %]
		</table>
	</div>
	[% END %]
<!-- END [% template.name %] -->
