[% USE date %]
<div class="card z-50 fixed right-0 top-[50px] mt-0 w-full md:w-[500px] rounded-t-none rounded-r-none" style="display: none;" id="cmd_pane">
  <div class="head">
    <h3>Quick Commands</h3>
    <button class="iconOnly medium absolute right-2 top-2" onClick="selectAllHosts(false,'dfl_');selectAllServices(false,'dfl_');selectAllHosts(false,'svc_');selectAllServices(false,'svc_');selectAllHosts(false,'hst_');return false;"><i class="uil uil-times"></i></button>
  </div>
  <div class="body">
    <form action="cmd.cgi" method="POST" id="quick_cmd_form" onsubmit="return collectFormData('quick_cmd_form')">
      <input type="hidden" name="referer" value="[% short_uri(c) %]" id="form_cmd_referer">
      <input type="hidden" name="selected_services" id="selected_services" value="">
      <input type="hidden" name="selected_hosts"    id="selected_hosts"    value="">
      <input type="hidden" name="CSRFtoken" value="[% get_user_token(c) %]">
[%
  quick_commands = [
    { val => "1",  icon => "fa-solid fa-clock",                label => "Reschedule next check",       hide => !c.config.cmd_quick_status.reschedule,       cmd_chk => [7, 17, 96],                   cls => "",               form => [ 'row_start', 'row_reschedule_options' ] },
    "-",
    { val => "2",  icon => "fa-solid fa-moon",                 label => "Add downtime",                hide => !c.config.cmd_quick_status.downtime,         cmd_chk => [55, 56, 78, 79, 85, 86, 122], cls => "",               form => [ 'row_start' 'row_end' 'row_comment' 'row_downtime_options' ] },
    { val => "5",  icon => "fa-solid fa-moon",                 label => "Remove downtimes",            hide => !c.config.cmd_quick_status.downtime,         cmd_chk => [55, 56, 78, 79, 85, 86, 122], cls => "hover-red-text", form => [ 'row_down_options' ] },
    "-",
    { val => "3",  icon => "fa-solid fa-comment",              label => "Add Comment",                 hide => !c.config.cmd_quick_status.comment,          cmd_chk => [1, 2, 3, 4],                  cls => "",               form => [ 'row_comment', 'row_comment_options' ] },
    { val => "6",  icon => "fa-solid fa-comment",              label => "Remove comments",             hide => !c.config.cmd_quick_status.comment,          cmd_chk => [1, 2, 3, 4],                  cls => "hover-red-text" },
    "-",
    { val => "4",  icon => "fa-solid fa-person-digging",       label => "Add acknowledgement",         hide => !c.config.cmd_quick_status.acknowledgement,  cmd_chk => [33, 34, 51, 52],              cls => "",               form => [ 'row_comment', 'row_ack_options' ] },
    { val => "7",  icon => "fa-solid fa-person-digging",       label => "Remove acknowledgement",      hide => !c.config.cmd_quick_status.acknowledgement,  cmd_chk => [33, 34, 51, 52],              cls => "hover-red-text" },
    "-",
    { val => "8",  icon => "fa-solid fa-check",                label => "Enable active checks",        hide => !c.config.cmd_quick_status.active_checks,    cmd_chk => [5, 6, 15, 16, 47, 48],        cls => "",               form => [ 'row_hostservice_options' ] },
    { val => "9",  icon => "fa-solid fa-xmark",                label => "Disable active checks",       hide => !c.config.cmd_quick_status.active_checks,    cmd_chk => [5, 6, 15, 16, 47, 48],        cls => "hover-red-text", form => [ 'row_comment_disable_cmd', 'row_hostservice_options' ]  },
    "-",
    { val => "10", icon => "fa-solid fa-check",                label => "Enable notifications",        hide => !c.config.cmd_quick_status.notifications,    cmd_chk => [22, 23, 28, 29],              cls => "",               form => [ 'row_hostservice_options' ] },
    { val => "11", icon => "fa-solid fa-xmark",                label => "Disable notifications",       hide => !c.config.cmd_quick_status.notifications,    cmd_chk => [22, 23, 28, 29],              cls => "hover-red-text", form => [ 'row_comment_disable_cmd', 'row_hostservice_options' ] },
    "-",
    { val => "15", icon => "fa-solid fa-check",                label => "Enable eventhandler",         hide => !c.config.cmd_quick_status.eventhandler,     cmd_chk => [43, 44, 45, 46],              cls => "" },
    { val => "16", icon => "fa-solid fa-xmark",                label => "Disable eventhandler",        hide => !c.config.cmd_quick_status.eventhandler,     cmd_chk => [43, 44, 45, 46],              cls => "hover-red-text", form => [ 'row_comment_disable_cmd' ] },
    "-",
    { val => "12", icon => "uil uil-left-arrow-to-left small", label => "Submit passive check result", hide => !c.config.cmd_quick_status.submit_result,    cmd_chk => [30, 87],                      cls => "",               form => [ 'row_submit_options' ] },
    { val => "14", icon => "fa-solid fa-broom",                label => "Reset modified attributes",   hide => !c.config.cmd_quick_status.reset_attributes, cmd_chk => [154, 155],                    cls => "hover-red-text" },
  ];
%]
[% PROCESS user_quick_commands.tt block="init" %]
      <table class="cellspacing">
        <tr>
          <th class="w-24">Command</th>
          <td class="relative">
            <input type='hidden' id="opt_persistent" name='persistent' value="comments">
            <select name="quick_command" id="quick_command" onChange="check_selected_command(this.value)" class="w-full" onclick="toggleElement('command_select', null, true); jQuery(this).blur(); return false;">
              [% FOREACH qc = quick_commands %]
                [% IF qc == "-"; NEXT; END %]
                [% IF !qc.defined("cls");  qc.cls  = ""; END %]
                [% IF !qc.defined("form"); qc.form = []; END %]
                [% IF !qc.defined("cmd_chk") || qc.cmd_chk.list.size == 0; qc.cmd_chk = [ -1 ]; END %]
                [% IF !qc.hide && !command_disabled_all(c, qc.cmd_chk) %]
                  <option
                    hidden
                    value="[% qc.val | html %]"
                    [% IF lc(c.config.cmd_quick_status.default) == lc(qc.label) %] selected[% END %]
                    data-forms="[% qc.form.join(',') | html %]"
                  >[% qc.label | html %]</option>
                [% END %]
              [% END %]
            </select>
            <div class="card shadow-float absolute w-[376px] rounded-t-none overflow-hidden" style="display: none;" id="command_select">
              <div class="actionbox less-padding">
                [% insert_ruler = 0 %]
                [% FOREACH qc = quick_commands %]
                  [% IF qc == "-" %]
                    [% insert_ruler = 1 %]
                    [% NEXT %]
                  [% END %]
                  [% IF !qc.hide && !command_disabled_all(c, qc.cmd_chk) %]
                    [% IF insert_ruler %]
                      <hr class="pt-px mb-px">
                      [% insert_ruler = 0 %]
                    [% END %]
                    <div><a onclick="selectCommand('[% qc.val | html %]'); return false;" class="flex [% qc.cls | html %]" href="#"><i class="[% qc.icon | html %]"></i>[% qc.label | html %]</a></div>
                  [% END %]
                [% END %]
              </div>
            </div>
          </td>
        </tr>
        <tr id="row_comment" class="js-quick-command-form-options">
          <th>Comment</th>
          <td><input type="text" value="[% c.config.cmd_defaults.comment %]" name="com_data" id="com_data" class="w-full" required></td>
        </tr>
        [% IF c.stash.require_comments_for_disable_cmds %]
        <tr id="row_comment_disable_cmd" class="js-quick-command-form-options">
          <th>Comment</th>
          <td><input type="text" value="" name="com_data_disable_cmd" id="com_data_disable_cmd" size="37"></td>
        </tr>
        [% END %]
        <tr id="row_start" class="js-quick-command-form-options">
          <th>Start</th>
          <td class="flexrow gap-1">
            <div class="flex-grow"><input type="text" value="[% format_date(date.now, '%Y-%m-%d %H:%M:00') %]" id="start_time" name="start_time" onkeyup="verify_time(this.id, 'end_time')" class="cal_popup_range_cond cal_popup_select cal_popup_select_future w-full"></div>
            <div class="w-5"><i class="uil uil-schedule text-lg cal_popup for_start_time" title="pick start date"></i></div>
          </td>
        </tr>
        <tr id="row_end" class="js-quick-command-form-options">
          <th>End</th>
          <td class="flexrow gap-1">
            <div class="flex-grow"><input type="text" value="[% format_date(date.now+downtime_duration, '%Y-%m-%d %H:%M:00') %]" id="end_time" name="end_time" size="20" onkeyup="verify_time(this.id, 'start_time')" class="cal_popup_range_cond cal_popup_select cal_popup_select_future w-full"></div>
            <div class="w-5"><i class="uil uil-schedule text-lg cal_popup for_end_time" title="pick end date"></i></div>
          </td>
        </tr>
        <tr id="row_downtime_options" class="js-quick-command-form-options">
          <th class="align-top">Options</th>
          <td class="p-0">
            <table class="w-full cellspacing-x">
              <tr>
                <td class="pt-0">Child hosts</td>
                <td class="pt-0">
                  <select name="childoptions" class="w-full">
                    <option value="0"[% IF c.config.cmd_defaults.childoptions == 0 %] selected[% END %]>Do nothing with child hosts</option>
                    <option value="1"[% IF c.config.cmd_defaults.childoptions == 1 %] selected[% END %]>Triggered downtime for all child hosts</option>
                    <option value="2"[% IF c.config.cmd_defaults.childoptions == 2 %] selected[% END %]>Non-triggered downtime for all childs</option>
                  </select>
                </td>
              </tr>
              <tr>
                <td>Services</td>
                <td>
                  <select name="hostserviceoptions" class="w-full">
                    <option value='0'[% IF c.config.cmd_defaults.hostserviceoptions == 0 %] selected[% END %]>Do nothing with services</option>
                    <option value='1'[% IF c.config.cmd_defaults.hostserviceoptions == 1 %] selected[% END %]>Schedule downtime for all services</option>
                  </select>
                </td>
              </tr>
              <tr>
                <td>Type</td>
                <td>
                  <select name='fixed' onChange="if(this.value==0) {showElement('flexible_options')} else { hideElement('flexible_options') }" class="w-full">
                    <option value="1"> Fixed </option>
                    <option value="0"> Flexible </option>
                  </select>
                </td>
              </tr>
              <tr id="flexible_options" style="display:none">
                <td colspan="2">
                  <span>If Flexible, Duration <input type='text' name='hours' value='2' size= "2" maxlength="2"> Hours <input type='text' name='minutes' value='0' size= "2" maxlength="2"> Minutes</span>
                </td>
              </tr>
              [% IF use_feature_recurring_downtime %]
              <tr>
                <td>Recurring</td>
                <td>
                  <input type="checkbox" id="recurring" name="recurring"> <label for="recurring">create recurring downtime</label>
                </td>
              </tr>
              [% END %]
            </table>
          </td>
        </tr>
        <tr id="row_reschedule_options" class="js-quick-command-form-options">
          <th class="align-top">Options</th>
          <td class="p-0">
            <table class="w-auto cellspacing-x">
              <tr><td class="pt-0 clickable" onclick="toggleCheckBox('opt1')">Force check</td><td class="pt-0"><input type='checkbox' id="opt1" name='force_check'[% IF c.config.cmd_defaults.force_check %] checked[% END %]></td></tr>
              <tr><td class="pt-0">Spread checks</td><td>
                <select name="spread">
                  <option value="0"[% IF c.config.cmd_defaults.reschedule_spread == 0 %] selected[% END %]>no</option>
                  <option value="10"[% IF c.config.cmd_defaults.reschedule_spread == 10 %] selected[% END %]>10 seconds</option>
                  <option value="30"[% IF c.config.cmd_defaults.reschedule_spread == 30 %] selected[% END %]>30 seconds</option>
                  <option value="60"[% IF c.config.cmd_defaults.reschedule_spread == 60 %] selected[% END %]> 1 minute</option>
                  <option value="300"[% IF c.config.cmd_defaults.reschedule_spread == 300 %] selected[% END %]> 5 minutes</option>
                  <option value="900"[% IF c.config.cmd_defaults.reschedule_spread == 900 %] selected[% END %]> 15 minutes</option>
                  <option value="1800"[% IF c.config.cmd_defaults.reschedule_spread == 1800 %] selected[% END %]> 30 minutes</option>
                  <option value="3600"[% IF c.config.cmd_defaults.reschedule_spread == 3600 %] selected[% END %]> 1 hour</option>
                </select>
              </td></tr>
            </table>
          </td>
        </tr>
        <tr id="row_ack_options" class="js-quick-command-form-options">
          <th class="align-top">Options</th>
          <td class="p-0">
            <table class="w-auto cellspacing-x">
              <tr><td class="pt-0 clickable" onclick="toggleCheckBox('opt2')">Sticky acknowledgement</td><td class="pt-0"><input type='checkbox' id="opt2" name='sticky_ack'[% IF c.config.cmd_defaults.sticky_ack || force_sticky_ack %] checked[% END %][% IF force_sticky_ack %] disabled[% END %]></td></tr>
              <tr><td class="clickable" onclick="toggleCheckBox('opt3')">Send notification</td><td><input type='checkbox' id="opt3" name='send_notification'[% IF c.config.cmd_defaults.send_notification || force_send_notification %] checked[% END %][% IF force_send_notification %] disabled[% END %]></td></tr>
              <tr><td class="clickable" onclick="toggleCheckBox('opt4')">Persistent comment</td><td><input type='checkbox' id="opt4" name='persistent_ack'[% IF c.config.cmd_defaults.persistent_ack || force_persistent_ack %] checked[% END %][% IF force_persistent_ack %] disabled[% END %]></td></tr>
              <tr id="opt_expire" class="js-quick-command-form-options"><td class="clickable" onclick="toggleCheckBox('opt5')">Use expire time</td><td><input type='checkbox' id="opt5" name='use_expire'[% IF c.config.cmd_defaults.use_expire %] checked[% END %] onchange="if(this.checked == true) {showElement('row_expire')} else { hideElement('row_expire')};"></td></tr>
            </table>
          </td>
        </tr>
        <tr id="row_expire" class="js-quick-command-form-options">
          <th>Expire</th>
          <td><i class="uil uil-schedule cal_popup for_expire_time" title="pick date"></i> <input type="text" value="[% format_date(date.now+expire_ack_duration, '%Y-%m-%d %H:%M:%S') %]" id="expire_time" name="expire_time" size="20" onkeyup="verify_time(this.id, 'now')" class="cal_popup_clear"></td>
        </tr>
        <tr id="row_comment_options" class="js-quick-command-form-options">
          <th class="align-top">Options</th>
          <td class="p-0">
            <table class="w-auto cellspacing-x">
              <tr><td class="pt-0 clickable" onclick="toggleCheckBox('opt6')">Persistent</td><td class="pt-0"><input type='checkbox' id="opt6" name='persistent_comments'[% IF c.config.cmd_defaults.persistent_comments || force_persistent_comments %] checked[% END %][% IF force_persistent_comments %] disabled[% END %]></td></tr>
            </table>
          </td>
        </tr>
        <tr id="row_down_options" class="js-quick-command-form-options">
          <th class="align-top">Options</th>
          <td class="p-0">
            <table class="w-auto cellspacing-x">
              <tr><td class="pt-0 clickable" onclick="toggleCheckBox('opt7')">Active downtimes</td><td><input type='checkbox' id="opt7" name='active_downtimes' checked></td></tr>
              <tr><td class="pt-0 clickable" onclick="toggleCheckBox('opt8')">Future downtimes</td><td><input type='checkbox' id="opt8" name='future_downtimes'></td></tr>
            </table>
          </td>
        </tr>
        <tr id="row_submit_options" class="js-quick-command-form-options">
          <th class="align-top">Options</th>
          <td class="p-0">
            <table class="w-full cellspacing-x">
              <tr>
                <td class="pt-0">Check result</td>
                <td class="pt-0">
                  <select name="plugin_state" class="w-full">
                    <option value="0">OK</option>
                    <option value="1">WARNING</option>
                    <option value="2">CRITICAL</option>
                    <option value="3">UNKNOWN</option>
                  </select>
                </td>
              </tr>
              <tr>
                <td>Check output</td>
                <td><input type="text" value="" name="plugin_output" id="plugin_output" class="w-full" required></td>
              </tr>
              <tr>
                <td>Performance data</td>
                <td><input type="text" value="" name="performance_data" class="w-full"></td>
              </tr>
            </table>
          </td>
        </tr>
        <tr id="row_hostservice_options" class="js-quick-command-form-options">
          <th class="align-top">Options</th>
          <td class="p-0">
            <table class="w-auto cellspacing-x">
              <tr><td class="pt-0 clickable" onclick="toggleCheckBox('opt9')">For all services too</td><td class="pt-0"><input type='checkbox' id="opt9" name='hostserviceoptions'[% IF c.config.cmd_defaults.hostserviceoptions %] checked[% END %]></td></tr>
            </table>
          </td>
        </tr>
        [% PROCESS user_quick_commands.tt block="form" %]
        <tr>
          <td></td>
          <td><button id="multi_cmd_submit_button" onClick="return collectFormData('quick_cmd_form')" class="w-full submit"></button></td>
        </tr>
      </table>
    </form>
  </div>
</div>

<script>
  /* show/hide options for commands based on the selected command*/
  function check_selected_command() {
      var sel = document.getElementById('quick_command');
      var value = sel.value;

      disableAllFormElement();
      var opt = jQuery('#quick_command').find(":selected");
      opt.data("forms").split(",").forEach(function(n) {
        n = n.trim();
        if(n != "") {
          jQuery("#"+n).css("display", "");
        }
      });

      if(value == 3) { /* add comment */
          document.getElementById('opt_persistent').value = 'comments';
      }
      if(value == 4) { /* add acknowledgement */
          document.getElementById('opt_persistent').value = 'ack';
          if(has_expire_acks) {
              jQuery("#opt_expire").css("display", "");
              if(document.getElementById('opt5').checked == true) {
                  jQuery("#row_expire").css("display", "");
              }
          }
      }
  }

  /* hide all form element rows */
  function disableAllFormElement() {
      jQuery("TR.js-quick-command-form-options").css("display", "none");
  }

  function selectCommand(val) {
    jQuery('#quick_command option').prop('selected', '');
    jQuery('#quick_command option[value="'+val+'"]').prop('selected', 'selected').change();
    hideElement('command_select');
    check_selected_command(val);
  }
</script>
