  [% META title = 'Admin &raquo; Email Contacts' -%]

<!-- BEGIN [% template.name %] -->
	[% # PROCESS dumper.tt dump = c.config('msg') %]

	[% PROCESS site/autosuggest.tt varname = 'hospital' %]

	[% email_contacts_dfv_js %]

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

	[% # PROCESS admin/misc_nav.tt page = 'error-codes' %]

  <script language="javascript" type="text/javascript">
    $(document).ready(function() {
      $('#type').blur(function() {
        var f = this.value; // alert(f);
        if (f.match('report')) {
          $('#department').show();
        }
        else {
          $('#department').hide();
        }
      });
    });
  </script>

  <h2 align="center">Email Contacts</h2>

    [% IF dfv_errors %]
        <p class="error">[% c.config('msg').dfv_errors %]</p>
    [% END %]

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

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

	<div class="[% dfv_errors ? 'visible' : 'itemhidden' %]" id="[% div_name %]">
		<form method="post" action="[% app_url %]/config/email-contacts/update"
			[% email_contacts_onsubmit %] name="email-contact" id="email-contact">
		<div class="indent">
			<table>
				<tr>
					<td class="label">Display name:</td>
					<td class="content">
            <input type="text" name="display_name" value="" size="40" />
						[% error_display_name %]
 					</td>
				</tr>
				<tr>
					<td class="label">Scope:</td>
					<td class="content">
						<select name="scope">
						<option value=""></option>
						[% FOREACH item IN meta_data.column('scope').check_in %]
                        <option value="[% item %]">[% item %]</option>
                     [% END %]
                        </select>
						[% error_scope %]
 					</td>
				</tr>
				<tr>
					<td class="label">Location:</td>
					<td class="search_bar">
            <input style="width: 300px" type="text" id="location_name" name="_display_name"
              value="[% c.query.param('_display_name') %]"/>
            <input type="hidden" id="source_id" name="referral_source_id" />
              [% IF error_referral_source_id %]
                [% error_referral_source_id %]
              [% ELSE %]
                [ <a href="[% app_url %]/config/referral-sources">new</a> ]
              [% END %]
          </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 %]
                         <option value="[% item %]">[% item %]</option>
						[% END %]
						</select>
						[% error_type %]
 					</td>
				</tr>

		[% display = c.query.param('department_id') ? 'visible' : 'none' %]
				<tr id="department" style="display:[% display %]">
                    <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="status" [% site.html.onfocus %]>
						<option value=""></option>
						[% FOREACH item IN meta_data.column('status').check_in %]
                            <option value="[% item %]">[% item %]</option>
						[% END %]
						</select>
						[% 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="mdt">
					<input type="radio" name="filter" value="mdt" id="mdt"
						[% 'checked' IF c.query.param('filter').match('mdt') %] />MDT only
				</label>
			</div>
			<div class="indent">
				<label for="report">
					<input type="radio" name="filter" value="report" id="report"
					[% 'checked' IF c.query.param('filter').match('report') %] />Report only
				</label>
			</div>
			<div class="indent">
				<label for="none">
					<input type="radio" name="filter" value="none" id="none"
					[% 'checked' IF NOT c.query.param('filter').match('mdt|report') %] />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>Scope</th>
				[% UNLESS c.query.param('filter').match('mdt') # irrelevant for MDT %]
					<th>Department</th>
				[% END %]
				<th>Location</th>
				<th>Type</th>
				<th>Status</th>
				<th>Email Address</th>
				<th>Active</th>
			</tr>

			[% FOREACH ec IN email_contacts; # PROCESS dumper.tt dump = ec.as_tree;
					FOREACH f IN ['mdt' 'report']; # doesn't work
						# NEXT IF c.query.param('filter').match(f) AND NOT ec.type.match(f);
					END;
					NEXT IF c.query.param('filter').match('mdt') AND NOT ec.type.match('mdt');
					NEXT IF c.query.param('filter').match('report') AND NOT ec.type.match('report');
			%]
			<tr>
				<td>[% i = ( i || 0 ) + 1; i # can't use loop.count if using filter %]</td>
				<td>
					<input type="text" value="[% ec.display_name | html %]" size="30"
						[% site.html.disabled %] />
				</td>
				<td>
					<input type="text" value="[% ec.scope %]" size="10"
                           [% site.html.disabled %] />
				</td>
				[% UNLESS c.query.param('filter').match('mdt') # irrelevant for MDT %]
				  <td>
                    [% FOREACH d IN departments;
                        NEXT UNLESS d.value == ec.department_id %]
					<input type="text" value="[% d.key %]"
                        [% site.html.disabled %] />
                    [%  END %]
				  </td>
				[% END %]
				<td>
					<input type="text" value="[% ec.referral_source.display_name %]"
                         size="30" [% site.html.disabled %] />
				</td>
				<td>
					<input type="text" value="[% ec.type %]" size="5"
                        [% site.html.disabled %] />
				</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/email-contacts/edit/[% ec.id %]">Edit</a>
				</td>
			 </tr>
			[% END %]
		</table>
	</div>
	[% END %]
<!-- END [% template.name %] -->
