<%!
    import pytz

    from crab.util.string import mergelines
    from crab.util.web import abbr

    scripts = ["editnotify"]
%>
<%inherit file="base.html"/>

<%block name="links">
% if not match_mode:
<a href="/host/${info['host'] | h}">${info['host'] | h}</a>
<a href="/user/${info['user'] | h}">${info['user'] | h}</a>
%     if info['crabid'] is not None:
<a href="/job/${id | h}">${info['crabid'] | h}</a>
%     else:
<a href="/job/${id | h}">${info['command'] | abbr}</a>
%     endif
% endif
<span>notifications</span>
</%block>

<%def name="notifyrow(n)" filter="mergelines">
<%
          nid = n['notifyid']
%>
<tr id="row_${nid | h}">
% if match_mode:
    <td><input name="host_${nid | h}"
%     if n.get('host') is not None:
            value="${n['host'] | h}"
%     else:
            value=""
%     endif
    /></td>
    <td><input name="user_${nid | h}"
%     if n.get('user') is not None:
            value="${n['user'] | h}"
%     else:
            value=""
%     endif
    /></td>
% endif
    <td><select name="method_${nid | h}">
% for (method, methodname) in [('email', 'Email')]:
        <option value="${method}"
% if method == n.get('method'):
            selected="selected"
% endif
        >${methodname}</option>
% endfor
    </select></td>
    <td><input id="address_${nid | h}" name="address_${nid | h}"
% if n.get('address') is not None:
            value="${n['address'] | h}"
% else:
            value=""
% endif
        /></td>
    <td><input name="time_${nid | h}"
% if n.get('time') is not None:
            value="${n['time'] | h}"
% else:
            value=""
% endif
        /></td>
    <td><select name="timezone_${nid | h}">
        <option value=""
% if n.get('timezone') is None:
            selected="selected"
% endif
        >Unspecified</option>
% for tz in pytz.common_timezones:
        <option value="${tz | h}"
% if tz == n.get('timezone'):
            selected="selected"
% endif
        >${tz | h}</option>
% endfor
    </select></td>
    <td><input name="include_ok_${nid | h}" type="checkbox"
% if not n.get('skip_ok'):
        checked="checked"
% endif
    /></td>
    <td><input name="include_warning_${nid | h}" type="checkbox"
% if not n.get('skip_warning'):
        checked="checked"
% endif
    /></td>
    <td><input name="include_error_${nid | h}" type="checkbox"
% if not n.get('skip_error'):
        checked="checked"
% endif
    /></td>
    <td><input name="include_output_${nid | h}" type="checkbox"
% if n.get('include_output'):
        checked="checked"
% endif
    /></td>
    <td class="linkcell">
        <a href="#" id="delete_${nid | h}"><span class="icon-trash"></span> Delete</a>
    </td>
</tr>
</%def>

% if match_mode:
<h2>Configure Host/User Notifications</h2>
<p>
This page allows notifications to be configured
by host name and/or by user name.
If the host or user box is left blank,
then the notifications
will apply to all entries for that parameter.
</p>
<p>
Notifications also can be attached to specific jobs
by navigating to each job's information page.
</p>
% else:
<h2>Configure Job Notifications</h2>
<p>
This page allows notifications to be attached to
this specific job.
</p>
<p>
Notifications can also be configured
by host name and/or by user name on the
<a href="/notify">host/user notifications</a> page.
</p>
% endif

% if match_mode:
<form method="post" action="/notify">
% else:
<form method="post" action="/job/${id | h}/notify">
% endif
<table id="notifylist">
<tr>
% if match_mode:
    <th>Host name</th>
    <th>User name</th>
% endif
    <th>Method</th>
    <th>Address</th>
    <th>Schedule</th>
    <th>Timezone</th>
    <th>Successes</th>
    <th>Warnings</th>
    <th>Errors</th>
    <th>Output</th>
    <th>Actions</th>
</tr>
% if notifications is not None:
%     for n in notifications:
          ${notifyrow(n)}
%     endfor
% endif
</table>
<p>
<a href="#" id="add_notification"><span class="icon-plus-sign"></span> Add notification.</a>
</p>
<p>
<input type="submit" name="submit_notify" value="Configure" />
</p>
</form>

<script>
<% n = {'notifyid': 'XXX'} %>
var notifyrowtemplate = '${notifyrow(n)}';
</script>
