[% USE date %]
[% temp = 'reports/' _ r.template %]
[% edit_types = '' %]
[% counter = 0 %]
[% PROCESS $temp block = 'edit' %]

[% FOREACH f = required_fields %]
  [% PROCESS field f=f %]
[% END %]

[% BLOCK field %]
  [% fh = normalize_required_field(c, f) %]
  [% IF fh.defined("hidden") && fh.hidden; NEXT; END %]
  <tr class="report_type_[% fh.type | html %] js-report-options">
    <th class="align-top">
      [% fh.desc %]
      [% IF fh.required %]<span class="required">*</span>[% END %]
      [% IF fh.details != '' %]<br><span class="whitespace-normal text-xs font-normal">[% fh.details %]</span>[% END %]
    </th>
    <td colspan=2>
      [% PROCESS field_form fh=fh %]
    </td>
  </tr>
[% END %]

[% BLOCK field_form %]
  [% key       = fh.name %]
  [% name      = fh.desc %]
  [% type      = fh.type %]
  [% details   = fh.details %]
  [% required  = fh.required %]
  [% default   = fh.default %]
  [% extra     = fh.extra %]
  [% f = [ name, type, default, details, required, extra]; %][%# for compatibility with old reports #%]
  [% value   = default %]
  [% IF r.params.exists(key) %][% value = r.params.$key %][% END %]
  [% IF type == 'int' %]
    <input type="text" name="params.[% key %]" value="[% value | html %]" class="w-56">
  [% ELSIF type == 'int_box' %]
    <div class="flexrow flex-nowrap gap-1 items-center w-56">
      <input type="hidden" name="optional.[% key %]" value="1">[% checked = 0 %]
      <input type="checkbox" name="enabled.[% key %]" value="1" onchange="var el = document.getElementsByName('params.[% key %]')[0]; if(this.checked) { el.disabled = false; } else { el.disabled = true;  }"[% IF value != -1; checked = 1; %]checked[% END %]>
      [% IF value == -1 || value == "" %][% value = default %][% END %]
      <input type="text" name="params.[% key %]" value="[% IF default == value %][% extra | html %][% ELSE %][% value | html %][% END %]" class="w-full with_checkbox"[% IF ! checked %] disabled[% END %]>
    </div>
  [% ELSIF type == 'textarea' %]
    <textarea name="params.[% key %]" wrap="hard" class="w-full">[% value | html %]</textarea>
  [% ELSIF type == 'yes_no' %]
    <select name="params.[% key %]" class="w-56">
      <option value="yes"[% IF value == 'yes' %] selected[% END %]>Yes</option>
      <option value="no"[%  IF value == 'no'  %] selected[% END %]>No</option>
    </select>
  [% ELSIF type == 'select' %]
    <select name="params.[% key %]" class="w-56">
      [% FOREACH o = fh.options %]
      [% o = o.list; v = o.0; IF o.size > 1; k = o.1; ELSE; k = o.0; END %]
      <option value="[% v | html %]"[%  IF value == v  %] selected[% END %]>[% k %]</option>
      [% END %]
    </select>
  [% ELSIF type == 'hosts' OR type == 'hostgroups' OR type == 'servicegroups' %]
    [% inputid = "params." _ key _ "_" _ counter; counter = counter + 1 %]
    [% IF type == 'hosts'; last_host_id = inputid; END %]
    <input class="w-full" type="text" id="[% inputid | html %]" name="params.[% key %]" value="[% value | html %]" onfocus="this.click()" onclick="ajax_search.init(this, '[% type | html %]', { url: 'status.cgi?format=search&amp;type=[% type | html %]', autosubmit:false, backend_select:'report_backends', list: '\\s*,\\s*', onselect:reports_update_affected_sla_objects })" onchange="reports_update_affected_sla_objects(this)">
  [% ELSIF type == 'services' %]
    <input class="w-full" type="text" name="params.[% key %]" value="[% value | html %]" onfocus="this.click()" onclick="ajax_search.init(this, 'service', { url: 'status.cgi?format=search&amp;type=services&amp;host=', autosubmit:false, append_value_of: '[% last_host_id | html %]', backend_select:'report_backends', list: '\\s*,\\s*', onselect:reports_update_affected_sla_objects })" onchange="reports_update_affected_sla_objects(this)">
  [% ELSIF type == 'affected_sla_objects' %]
    <div class="flexrow flex-nowrap justify-end">
      <div class="w-full text-right">
        <span class="name">expected objects:</span>
        <span class="value inline-block w-40 text-left pl-2" id="reports_expected_objects">&nbsp;</span>
      </div>
      <div class="w-5">
        <div id="reports_waiting" style="display: none;">
          <div class="spinner w-3 h-3" title="getting expected objects"></div>
        </div>
      </div>
    </div>
    <script>
      window.setInterval(function() {
        var input = jQuery("#reports_waiting");
        reports_update_affected_sla_objects(input[0]);
      }, 500);
    </script>
  [% ELSIF type == 'rptimeperiod' %]
    [% t1=date.now; IF r.params.defined("t1"); t1 = r.params.t1; END %]
    [% t2=date.now; IF r.params.defined("t2"); t2 = r.params.t2; END %]
    [% custom=1; IF value == "custom"; custom=2; END %]
    [% PROCESS _timeperiods_select_options.tt selected_timeperiod=value custom=custom name="params." _ key t1=t1 t2=t2 current=0 show_hint=1 width="w-56" %]
  [% ELSIF type == 'breakdowns' %]
    <select name="params.[% key %]" class="w-56">
      <option value="months"[% IF value == 'months' %] selected[% END %]>Months</option>
      <option value="weeks"[%  IF value == 'weeks'  %] selected[% END %]>Weeks</option>
      <option value="days"[%   IF value == 'days'   %] selected[% END %]>Days</option>
    </select>
  [% ELSIF type == 'rpttimeperiod' %]
    <select name="params.[% key %]" class="w-56">
      <option value="">None</option>
      [% FOREACH t = timeperiods %]
      <option value="[% t.name | html %]"[% IF value == t.name %] selected[% END %]>[% t.name | html %]</option>
      [% END %]
    </select>
  [% ELSIF type == 'option' %]
    <input type="checkbox" name="params.[% key %]" value="1"[% IF value %] checked[% END %]>
  [% ELSIF type == 'hst_unavailable' %]
    <input type="hidden" name="params.[% key %]" value="">
    [% lookup = {} %]
    [% FOREACH tmp = value %][% lookup.$tmp = 1 %][% END %]
    <table class="w-fit innercellborder unavailable_states">
      <tr>
        <th class="w-24">State</th>
        <th class="text-center">Normal</th>
        <th class="text-center">During Downtime</th>
      </tr>
      <tr>
        <td>Up</td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="up"[% IF lookup.exists('up') %] checked[% END %]></td>
        <td></td>
      </tr>
      <tr>
        <td>Down</td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="down"[% IF lookup.exists('down') %] checked[% END %]></td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="down_downtime"[% IF lookup.exists('down_downtime') %] checked[% END %]></td>
      </tr>
      <tr>
        <td>Unreachable</td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="unreachable"[% IF lookup.exists('unreachable') %] checked[% END %]></td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="unreachable_downtime"[% IF lookup.exists('unreachable_downtime') %] checked[% END %]></td>
      </tr>
    </table>
  [% ELSIF type == 'svc_unavailable' %]
    <input type="hidden" name="params.[% key %]" value="">
    [% lookup = {} %]
    [% FOREACH tmp = value %][% lookup.$tmp = 1 %][% END %]
    <table class="w-fit innercellborder unavailable_states">
      <tr>
        <th class="w-24">State</th>
        <th class="text-center">Normal</th>
        <th class="text-center">During Downtime</th>
      </tr>
      <tr>
        <td>Ok</td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="ok"[% IF lookup.exists('ok') %] checked[% END %]></td>
        <td></td>
      </tr>
      <tr>
        <td>Warning</td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="warning"[% IF lookup.exists('warning') %] checked[% END %]></td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="warning_downtime"[% IF lookup.exists('warning_downtime') %] checked[% END %]></td>
      </tr>
      <tr>
        <td>Critical</td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="critical"[% IF lookup.exists('critical') %] checked[% END %]></td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="critical_downtime"[% IF lookup.exists('critical_downtime') %] checked[% END %]></td>
      </tr>
      <tr>
        <td>Unknown</td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="unknown"[% IF lookup.exists('unknown') %] checked[% END %]></td>
        <td class="text-center"><input type="checkbox" name="params.[% key %]" value="unknown_downtime"[% IF lookup.exists('unknown_downtime') %] checked[% END %]></td>
      </tr>
    </table>
  [% ELSIF type == 'hst_initialassumedstate' %]
    <select name='params.[% key %]' class="w-56">
      <option value=0[% IF  value ==  '0' %] selected[% END %]>Unspecified</option>
      <option value=-1[% IF value == '-1' %] selected[% END %]>Current State</option>
      <option value=3[% IF  value ==  '3' %] selected[% END %]>Host Up</option>
      <option value=4[% IF  value ==  '4' %] selected[% END %]>Host Down</option>
      <option value=5[% IF  value ==  '5' %] selected[% END %]>Host Unreachable</option>
    </select>
  [% ELSIF type == 'svc_initialassumedstate' %]
    <select name='params.[% key %]' class="w-56">
      <option value="0"[% IF  value ==  '0' %] selected[% END %]>Unspecified</option>
      <option value="-1"[% IF value == '-1' %] selected[% END %]>Current State</option>
      <option value="6"[% IF  value ==  '6' %] selected[% END %]>Service Ok</option>
      <option value="8"[% IF  value ==  '8' %] selected[% END %]>Service Warning</option>
      <option value="7"[% IF  value ==  '7' %] selected[% END %]>Service Unknown</option>
      <option value="9"[% IF  value ==  '9' %] selected[% END %]>Service Critical</option>
    </select>
  [% ELSIF type == 'logclasses' %]
    <input type="hidden" name="params.[% key %]" value="">
    [% lookup = {} %]
    [% FOREACH tmp = value %][% lookup.$tmp = 1 %][% END %]
    <div class="flexrow justify-between">
      <table class="w-fit h-fit">
        <tr>
          <th class="pl-0" colspan=2>Host Alerts</th>
        </tr>
        <tr>
          <td class="pl-0" colspan=2>
            <select name='params.[% key %]'>
              <option value="host_state_hard"[% IF lookup.exists('host_state_hard') %] selected[% END %]>Hard States</option>
              <option value="host_state_soft"[% IF lookup.exists('host_state_soft') %] selected[% END %]>Soft States</option>
              <option value="host_state_both"[% IF lookup.exists('host_state_both') %] selected[% END %]>Both</option>
            </select>
          </td>
        </tr>
        <tr>
          <td class="pl-0"><input type="checkbox" name="params.[% key %]" value="host_up"[% IF lookup.exists('host_up') %] checked[% END %]></td><td>Up</td>
        </tr>
        <tr>
          <td class="pl-0"><input type="checkbox" name="params.[% key %]" value="host_down"[% IF lookup.exists('host_down') %] checked[% END %]></td><td>Down</td>
        </tr>
        <tr>
          <td class="pl-0"><input type="checkbox" name="params.[% key %]" value="host_unreachable"[% IF lookup.exists('host_unreachable') %] checked[% END %]></td><td>Unreachable</td>
        </tr>
      </table>

      <table class="w-fit h-fit">
        <tr>
          <th colspan=2>Service Alerts</th>
        </tr>
        <tr>
          <td colspan=2>
              <select name='params.[% key %]'>
                <option value="service_state_hard"[% IF lookup.exists('service_state_hard') %] selected[% END %]>Hard States</option>
                <option value="service_state_soft"[% IF lookup.exists('service_state_soft') %] selected[% END %]>Soft States</option>
                <option value="service_state_both"[% IF lookup.exists('service_state_both') %] selected[% END %]>Both</option>
              </select>
          </td>
        </tr>
        <tr>
          <td><input type="checkbox" name="params.[% key %]" value="service_ok"[% IF lookup.exists('service_ok') %] checked[% END %]></td><td>OK</td>
        </tr>
        <tr>
          <td><input type="checkbox" name="params.[% key %]" value="service_warning"[% IF lookup.exists('service_warning') %] checked[% END %]></td><td>Warning</td>
        </tr>
        <tr>
          <td><input type="checkbox" name="params.[% key %]" value="service_unknown"[% IF lookup.exists('service_unknown') %] checked[% END %]></td><td>Unknown</td>
        </tr>
        <tr>
          <td><input type="checkbox" name="params.[% key %]" value="service_critical"[% IF lookup.exists('service_critical') %] checked[% END %]></td><td>Critical</td>
        </tr>
      </table>

      <table class="w-fit h-fit">
        <tr>
          <th colspan=2>Notifications</th>
        </tr>
        <tr>
          <td><input type="checkbox" name="params.[% key %]" value="notification_host"[%  IF lookup.exists('notification_host') %] checked[% END %]></td><td>Host</td>
        </tr>
        <tr>
          <td><input type="checkbox" name="params.[% key %]" value="notification_service"[% IF lookup.exists('notification_service') %] checked[% END %]></td><td>Service</td>
        </tr>
      </table>
    </div>
  [% ELSIF type == 'radio' %]
    <div class="radiogroup">
      [% FOR v = fh.extra %]
        <input type="radio" id="[% v | html %]" name="params.[% key %]" value="[% v | html %]"[% IF v == value %] checked[% END %] onchange="reports_update_affected_sla_objects(this)">
        <label for="[% v | html %]">[% v | html %]</label>
      [% END %]
    </div>
  [% ELSIF type == 'themes' %]
    <select name='params.[% key %]' class="w-56">
      [% FOR t = c.config.themes %]
      <option value="[% t | html %]"[% IF t == value %] selected[% END %]>[% t | html %]</option>
      [% END %]
    </select>
  [% ELSIF type == 'report_theme' %]
    <select name='params.[% key %]' class="w-56">
      <option value="default">default</option>
      [% FOR t = report_themes %]
      <option value="[% t.name | html %]"[% IF t.name == value %] selected[% END %]>[% t.name | html %]</option>
      [% END %]
    </select>
  [% ELSIF type == 'language' %]
    <select name='params.[% key %]' class="w-56">
      [% FOR l = languages.keys.sort %]
      <option value="[% languages.$l.abrv | html %]"[% IF languages.$l.abrv == value %] selected[% END %]>[% l %]</option>
      [% END %]
    </select>
  [% ELSIF type == 'dateformat' %]
    <input type="text" class="w-56" name="params.[% key %]" value="[% value | html %]" placeholder="%d %b %Y">
  [% ELSIF type == 'datetimeformat' %]
    <input type="text" class="w-56" name="params.[% key %]" value="[% value | html %]" placeholder="%d %b %Y  %H:%M:%S">
  [% ELSIF type == 'hostformat' %]
    <select name="params.[% key %]" class="w-56" onchange="var el = document.getElementById('params.[% key %]_cust'); if(this.value == 'hostcustom') { el.style.display = ''; } else { el.style.display = 'none'; }">
      <option value="hostname"[%        IF value == 'hostname'        %] selected[% END %]>Name</option>
      <option value="hostalias"[%       IF value == 'hostalias'       %] selected[% END %]>Alias</option>
      <option value="hostdisplayname"[% IF value == 'hostdisplayname' %] selected[% END %]>Displayname</option>
      <option value="hostcustom"[%      IF value == 'hostcustom'      %] selected[% END %]>Customvariable</option>
    </select>
    [% cust_value = ""; cust_key = key _ '_cust'; IF r.params.exists(cust_key) %][% cust_value = r.params.$cust_key %][% END %]
    <input type="text" id="params.[% key %]_cust" name="params.[% key %]_cust" value="[% cust_value | html %]" onfocus="this.click()" onclick="ajax_search.init(this, 'custom variable', { url: 'status.cgi?format=search&amp;type=custom variable&exposed_only=1', autosubmit:false, backend_select:'report_backends', list: '\\s*,\\s*' })" style="width: 150px; [% IF value != 'hostcustom' %]display: none;[% END %]">
  [% ELSIF type == 'serviceformat' %]
    <select name="params.[% key %]" class="w-56" onchange="var el = document.getElementById('params.[% key %]_cust'); if(this.value == 'servicecustom') { el.style.display = ''; } else { el.style.display = 'none'; }">
      <option value="servicedescription"[% IF value == 'description'        %] selected[% END %]>Description</option>
      <option value="servicedisplayname"[% IF value == 'servicedisplayname' %] selected[% END %]>Displayname</option>
      <option value="servicecustom"[%      IF value == 'servicecustom'      %] selected[% END %]>Customvariable</option>
    </select>
    [% cust_value = ""; cust_key = key _ '_cust'; IF r.params.exists(cust_key) %][% cust_value = r.params.$cust_key %][% END %]
    <input type="text" id="params.[% key %]_cust" name="params.[% key %]_cust" value="[% cust_value | html %]" onfocus="this.click()" onclick="ajax_search.init(this, 'custom variable', { url: 'status.cgi?format=search&amp;type=custom variable&exposed_only=1', autosubmit:false, backend_select:'report_backends', list: '\\s*,\\s*' })" style="width: 150px; [% IF value != 'servicecustom' %]display: none;[% END %]">
  [% ELSIF type == 'label' %]
    [% fh.label %]
  [% ELSIF type == 'formlist' %]
    [% parent = fh %]
    [% parentvalue = value %]
    <table class="cellspacing-y multiformlist" id="[% tblID = "table_" _ random_id(); tblID %]">
      <tbody>
        <tr>
          [% IF parent.draggable %]
          <th></th>
          [% END %]
          [% FOREACH ch = parent.childs %]
          <th>[% ch.desc %]</th>
          [% END %]
          [% IF parent.multiple %]
          <th></th>
          [% END %]
        </tr>
        [% IF parent.multiple %]
        <tr[% IF parent.draggable %] class="sortable clickable" onmouseover="initTableRowSorting('[% tblID %]')"[% END %] style="display: none;" id="formlist_row_0">
          [% IF parent.draggable %]
            <td class="cursor-move"><i class="uil uil-draggabledots"></i></td>
          [% END %]
          [% FOREACH ch = parent.childs; ch.origname = ch.name; ch.name = parent.name _ "." _ ch.name %]
            <td>
              [% PROCESS field_form fh=ch %]
            </td>
          [% END %]
          [% IF parent.multiple %]
            <td class='clickable hoverable rounded px-1' onclick="delete_form_row(this); return false;"><i class="uil uil-times" title="remove this row"></td>
          [% END %]
        </tr>
        [% END %]

        [% FOREACH chv = parentvalue.list %]
        <tr[% IF parent.draggable %] class="sortable clickable" onmouseover="initTableRowSorting('[% tblID %]')[% END %]" id="formlist_row_[% loop.count %]">
          [% IF parent.draggable %]
            <td class="cursor-move"><i class="uil uil-draggabledots"></i></td>
          [% END %]
          [% FOREACH ch = parent.childs %]
            <td>
              [% subkey = ch.name; origname = ch.origname; IF chv.exists(origname); r.params.$subkey = chv.$origname; END %]
              [% PROCESS field_form fh=ch %]
            </td>
          [% END %]
          [% IF parent.multiple %]
            <td class='clickable hoverable rounded px-1' onclick="delete_form_row(this); return false;"><i class="uil uil-times" title="remove this row"></td>
          [% END %]
        </tr>
        [% END %]
        [% IF parent.multiple %]
        <tr>
          <td colspan=100 class='clickable hoverable rounded px-1 min-w-[100px] whitespace-nowrap' onclick="add_form_row(this, 1); return false;">
              <i class="uil uil-angle-down" title="add new entry"></i> add more
          </td>
        </tr>
        [% END %]
      </tbody>
    </table>
  [% ELSIF type == 'filter' %]
    [% substyle   = 'service' %]
    [% paneprefix = sprintf('f%02d_', loop.index)  %]
    <input type="hidden" name="filter.[% key %]" value="[% paneprefix %]">

    <div class="w-full flexrow flex-nowrap gap-0">
      <script>
        var ajax_search_backend_select = 'report_backends';
      </script>
      <div id="[% paneprefix %]filter_pane" class="flexrow flex-wrap gap-0">
        [% FOR search = get_searches(c, 'dfl_', value, 1) %]
          [% SET prefix = 's' _ loop.index %]
          [% PROCESS _status_filter_single.tt onchange="" %]
        [% END %]
      </div>
      <div class="clickable hoverable p-0 rounded-br-lg" onClick="new_filter('[% paneprefix %]filter_pane')" id="[% paneprefix %]new_filter_box_btn">
        <div class="flexcol gap-1 text-center pt-10">
          <i class="uil uil-angle-right text-lg" title="add new filter"></i>
          <span>or</span>
        </div>
      </div>
    </div>
  [% ELSE %]
  [% PROCESS $temp block = 'edit_types' %]
  [% PROCESS custom_reports_edit_step2.tt %]
    [% IF key %]
    <input type="text" name="params.[% key %]" value="[% escape_html(value) %]" class="w-full">
    [% END %]
  [% END %]
[% END %]
<style>
  .multiformlist .w-56 {
    width: min-content !important;
  }
</style>
