{extends "manage/manage.tpl.html"}

{function name="rank_icon" direction="down" href=""}
  {include file="include/rank_icon.tpl.html" direction=$direction href=$href}
{/function}

{block "manage_content"}
  <script type="text/javascript">
  <!--
  var url = '{$core.rel_url}manage/reminders.php';
  var rem_id = {$smarty.get.id|intval|default:''};

  function populateIssueComboBox()
  {
      if (rem_id == '') {
          url += '?prj_id=' + Eventum.getField('project').val();
      } else {
          url += '?cat=edit&id=' + rem_id + '&prj_id=' + Eventum.getField('project').val();
      }
      window.location.href = url;
  }
  function toggleReminderTypeFields()
  {
      var reminder_type = $('[name=reminder_type]:checked');
      $('select.type_filter').attr('disabled', true);

      Eventum.getField(reminder_type.val() + 's[]').prop('disabled', false);
  }
  function toggleIssuePriorityField()
  {
      if ($('[name=check_priority]:checked').length < 1) {
          Eventum.getField('priorities[]').prop('disabled', true).val('');
      } else {
          Eventum.getField('priorities[]').prop('disabled', false);
      }
  }
  function toggleIssueSeverityField()
  {
      if ($('[name=check_severity]:checked').length < 1) {
          Eventum.getField('severities[]').prop('disabled', true).val('');
      } else {
          Eventum.getField('severities[]').prop('disabled', false);
      }
  }
  function toggleIssueProductField()
  {
      if ($('[name=check_product]:checked').length < 1) {
          Eventum.getField('products[]').prop('disabled', true).val('');
      } else {
          Eventum.getField('products[]').prop('disabled', false);
      }
  }
  function validateForm()
  {
      if (Eventum.getField('project').val() == -1) {
          Validation.selectField('project')
          alert('{t escape=js}Please choose a project that will be associated with this reminder.{/t}');
          return false;
      }
      if (Validation.isFieldWhitespace('title')) {
          Validation.selectField('title');
          alert('{t escape=js}Please enter the title for this reminder.{/t}');
          return false;
      }
      if (Validation.isFieldWhitespace('rank')) {
          Validation.selectField('rank');
          alert('{t escape=js}Please enter the rank for this reminder.{/t}');
          return false;
      }

      var reminder_type = $('[name=reminder_type]:checked').val();

      if ((reminder_type == 'issue') && (!Validation.hasOneSelected('issues[]'))) {
          alert('{t escape=js}Please enter the issue IDs that will be associated with this reminder.{/t}');
          return false;
      }
      if ((reminder_type == 'support_level') && (!Validation.hasOneSelected('support_levels[]'))) {
          alert('{t escape=js}Please choose the support levels that will be associated with this reminder.{/t}');
          return false;
      }
      if ((reminder_type == 'customer') && (!Validation.hasOneSelected('customers[]'))) {
          alert('{t escape=js}Please choose the customers that will be associated with this reminder.{/t}');
          return false;
      }

      if (($('[name=check_priority]:checked').length > 0) && (!Validation.hasOneSelected('priorities[]'))) {
          alert('{t escape=js}Please choose the priorities that will be associated with this reminder.{/t}');
          return false;
      }
      if (($('[name=check_product]:checked').length > 0) && (!Validation.hasOneSelected('products[]'))) {
          alert('{t escape=js}Please choose the products that will be associated with this reminder.{/t}');
          return false;
      }
      if (($('[name=check_severity]:checked').length > 0) && (!Validation.hasOneSelected('severities[]'))) {
          alert('{t escape=js}Please choose the severities that will be associated with this reminder.{/t}');
          return false;
      }
      return true;
  }
  function checkDelete()
  {
      if (!Validation.hasOneChecked('items[]')) {
          alert('{t escape=js}Please select at least one of the reminders.{/t}');
          return false;
      }
      if (!confirm('{t escape=js}This action will remove the selected entries.{/t}')) {
          return false;
      } else {
          return true;
      }
  }
  $().ready(function() {
      $('#reminder_form').submit(validateForm);
      $('.select_all').click(function() { Eventum.toggleCheckAll('items[]'); });

      $('select[name=project]').change(populateIssueComboBox);
      $('input[name=reminder_type]').click(toggleReminderTypeFields);
      $('input[name=check_priority]').click(toggleIssuePriorityField);
      $('input[name=check_severity]').click(toggleIssueSeverityField);
      $('input[name=check_product]').click(toggleIssueProductField);

      $('#delete_form').submit(checkDelete);

      toggleReminderTypeFields();
      toggleIssuePriorityField();
      toggleIssueSeverityField();
      toggleIssueProductField();
  });
  //-->
  </script>

  <form id="reminder_form" method="post">
  {if $smarty.get.cat|default:'' == 'edit'}
  <input type="hidden" name="cat" value="update">
  <input type="hidden" name="id" value="{$smarty.get.id|intval}">
  {else}
  <input type="hidden" name="cat" value="new">
  {/if}
<table class="bordered">
  <tr class="title">
    <th colspan="2">
      {t}Manage Issue Reminders{/t}
      <div class="right">
        {if $smarty.get.cat|default:'' == 'edit'}{t}Updating Reminder{/t} #{$smarty.get.id|intval}{else}{t}Creating New Reminder{/t}{/if}
      </div>
    </th>
  </tr>
  <tr>
    <th width="120">
      {t}Project{/t}
    </th>
    <td width="85%">
      <select name="project">
        <option value="">{t}Please choose an option{/t}</option>
        {html_options options=$project_list selected=$info.rem_prj_id|default:''}
      </select>
      {include file="error_icon.tpl.html" field="project"}
    </td>
  </tr>
  <tr>
    <th width="120">
      {t}Title{/t}
    </th>
    <td>
      <input type="text" size="50" name="title" value="{$info.rem_title|escape:"html"|default:''}">
      {include file="error_icon.tpl.html" field="title"}
    </td>
  </tr>
  <tr>
    <th width="120">
      {t}Rank{/t}
    </th>
    <td>
      <input type="text" size="10" name="rank" value="{$info.rem_rank|default:''}">
      {include file="error_icon.tpl.html" field="rank"}
    </td>
  </tr>
  <tr>
    <th width="120">
      {t}Reminder Type{/t}:
    </th>
    <td>
      <table cellpadding="1" border="0">
        <tr>
          {counter start=0 assign="next_field"}
          {if $project_has_customer_integration}
          <td>
            <label>
            <input type="radio" name="reminder_type" value="support_level" {if $info.type|default:'' == 'support_level'}checked{/if} onClick="toggleReminderTypeFields();">
            {t}By Support Level{/t}</label>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            {counter}
          <td>
            <label>
            <input type="radio" name="reminder_type" value="customer" {if $info.type|default:'' == 'customer'}checked{/if} onClick="toggleReminderTypeFields();">
            {t}By Customer{/t}</label>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
            {counter}
          {/if}
          <td>
            <label>
            <input type="radio" name="reminder_type" value="issue" {if $info.type|default:'' == 'issue'}checked{/if} onClick="toggleReminderTypeFields();">
            {t}By Issue ID{/t}</label>
          </td>
          {counter}
        </tr>
        <tr>
          {if $project_has_customer_integration}
          <td>
            <select class="type_filter" name="support_levels[]" size="4" multiple>
              {html_options options=$support_levels selected=$info.rer_support_level_id|default:''}
            </select>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>
            <select class="type_filter" name="customers[]" size="4" multiple>
              {html_options options=$customers selected=$info.rer_customer_id|default:''}
            </select>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          {/if}
          <td>
            <select class="type_filter" name="issues[]" size="4" multiple>
              {html_options options=$issues|default:'' selected=$info.rer_iss_id|default:''}
            </select>
          </td>
        </tr>
        <tr>
          <td>
            <label>
            <input type="radio" name="reminder_type" value="all_issues" {if $info.type|default:'' == 'ALL'}checked{/if} onClick="toggleReminderTypeFields();"
              {if $smarty.get.cat|default:'' != 'edit'}checked{/if}>
                {t}All Issues{/t}</label>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>
            <label>
            <input type="checkbox" name="check_priority" value="yes" {if $info.check_priority|default:'' == 'yes'}checked{/if}
              onClick="toggleReminderTypeFields();">
                {t}Also Filter By Issue Priorities{/t}</label>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>
            <label>
            <input type="checkbox" name="check_product" value="yes" {if $info.check_product|default:'' == 'yes'}checked{/if}
                onClick="toggleReminderTypeFields();">
            {t}Also Filter By Issue Product{/t}</label>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>
            <select name="priorities[]" size="4" multiple>
              {html_options options=$priorities|default:'' selected=$info.rer_pri_id|default:''}
            </select>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>
            <select name="products[]" size="4" multiple>
              {html_options options=$products|default:'' selected=$info.rer_pro_id|default:''}
            </select></td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
        </tr>
        <tr>
          <td>
            &nbsp;
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>
            <label>
            <input type="checkbox" name="check_severity" value="yes" {if $info.check_severity|default:'' == 'yes'}checked{/if}
                onClick="javascript:toggleReminderTypeFields();">
            {t}Also Filter By Issue Severities{/t}</label>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
        </tr>
        <tr>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>
            <select name="severities[]" size="4" multiple>
              {html_options options=$severities|default:'' selected=$info.rer_sev_id|default:''}
            </select>
          </td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
          <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
        </tr>
      </table>
    </td>
  </tr>
  <tr>
    <th width="120">
      {t}Skip Weekends{/t}:
    </th>
    <td>
      <input type="radio" name="skip_weekend" value="1" {if $info.rem_skip_weekend|default:'' == 1}CHECKED{/if}>{t}Yes{/t}
      <input type="radio" name="skip_weekend" value="0" {if $info.rem_skip_weekend|default:'' != 1}CHECKED{/if}>{t}No{/t}<br />
      &nbsp;{t}If yes, this reminder will not activate on weekends and time will not accumulate on the weekends.{/t}
    </td>
  </tr>
  <tr class="buttons">
    <td colspan="2">
      {if $smarty.get.cat|default:'' == 'edit'}
      <input type="submit" value="{t}Update Reminder{/t}">
      {else}
      <input type="submit" value="{t}Create Reminder{/t}">
      {/if}
      <input type="reset" value="{t}Reset{/t}">
    </td>
  </tr>
</table>
</form>

<form id="delete_form" method="post">
<input type="hidden" name="cat" value="delete">
<table class="bordered grid">
      <tr class="title">
        <th colspan="8">
          {t}Existing Issue Reminders{/t}:
        </th>
      </tr>
      <tr>
          <th width="4" nowrap><input type="button" value="{t}All{/t}" class="select_all"></th>
          <th>&nbsp;{t}ID{/t}&nbsp;</th>
          <th align="center">&nbsp;{t}Rank{/t}&nbsp;</th>
          <th>&nbsp;{t}Title{/t}</th>
          <th>&nbsp;{t}Project{/t}</th>
          <th>&nbsp;{t}Type{/t}</th>
          <th nowrap>&nbsp;{t}Issue Priorities{/t}&nbsp;</th>
          <th>&nbsp;{t}Details{/t}</th>
        </tr>
        {section name="i" loop=$list}
        <tr class="{cycle values='odd,even'}">
          <td width="4" nowrap align="center"><input type="checkbox" name="items[]" value="{$list[i].rem_id}"></td>
          <td align="center">{$list[i].rem_id}</td>
          <td align="center">
            {rank_icon href="{$core.rel_url}manage/reminders.php?cat=change_rank&id={$list[i].rem_id}&rank=desc" direction="down"}
            {$list[i].rem_rank}
            {rank_icon href="{$core.rel_url}manage/reminders.php?cat=change_rank&id={$list[i].rem_id}&rank=asc" direction="up"}
          </td>
          <td>
            &nbsp;<a href="{$core.rel_url}manage/reminders.php?cat=edit&id={$list[i].rem_id}" title="{t}update this entry{/t}">{$list[i].rem_title|escape:"html"}</a>
          </td>
          <td width="25%">
            &nbsp;{$list[i].prj_title|escape:"html"}
          </td>
          <td width="20%">
            &nbsp;{if $list[i].type == 'ALL'}{t}All Issues{/t}{elseif $list[i].type == 'support_level'}{t}By Support Level{/t}{elseif $list[i].type == 'customer'}{t}By Customer{/t}{elseif $list[i].type == 'issue'}{t}By Issue ID{/t}{/if}
          </td>
          <td width="15%">
            &nbsp;{section name="y" loop=$list[i].priorities}{$list[i].priorities[y]|escape:"html"}{if not $smarty.section.y.last}, {/if}{/section}
          </td>
          <td>
            &nbsp;<a href="reminder_actions.php?rem_id={$list[i].rem_id}">{$list[i].total_actions} Action{if $list[i].total_actions != 1}s{/if}</a>
          </td>
        </tr>
        {sectionelse}
        <tr class="no_results">
          <td colspan="8">
            {t}No reminders could be found.{/t}
          </td>
        </tr>
        {/section}
        <tr class="buttons">
          <td width="4" align="center">
            <input type="button" value="{t}All{/t}" class="select_all">
          </td>
          <td colspan="7">
            <input type="submit" value="{t}Delete{/t}">
          </td>
        </tr>
      </table>
    </form>
{/block}
