[% USE date %]
[% PROCESS _blocks.tt %]
[% PROCESS custom_perf_bar_adjustments.tt %]

<table class="[% paneprefix %]_table mainTable striped" onmouseout='reset_all_hosts_and_services();'>
  <thead title="click to sort, double click to rename, drag columns to reorder">
    <tr onmouseover="initStatusTableColumnSorting('[% paneprefix %]', 'mainTable');">
      [% FOREACH col = table_columns.$paneprefix %]
        [% center = "" %]
        [% IF
               col.field == "check_interval"
            || col.field == "check_period"
            || col.field == "current_attempt"
            || col.field == "duration"
            || col.field == "execution_time"
            || col.field == "in_check_period"
            || col.field == "in_notification_period"
            || col.field == "last_check"
            || col.field == "last_state_change"
            || col.field == "latency"
            || col.field == "lmd_last_cache_update"
            || col.field == "next_check"
            || col.field == "notification_period"
            || col.field == "peer_name"
            || col.field == "percent_state_change"
            || col.field == "state"
          %]
          [% center = " text-center" %]
        [% END %]
        <th class="status col_[% col.field | html %][% center %]"[% IF ! col.checked %] style="display: none;"[% END %]>
          <a class="status col_[% col.field | html %] sort-by" href='#' data-sort-type[% sortprefix %]="1" data-sort-option[% sortprefix %]="[% col.sortby %]">[% col.title | html %]</a>
        </th>
      [% END %]
    </tr>
  </thead>

  <tbody>
    [%  FOREACH h = data;
      SET class = 'statusEven' IF loop.odd;
      SET class = 'statusOdd'  IF loop.even;

      SET peer_key      = h.peer_key;
      SET program_start = pi_detail.$peer_key.program_start;

      SET comment_count = h.comments.size;

      IF h.has_been_checked == 0;
        status ='PENDING';
      ELSIF h.state == 0;
        status ='UP';
      ELSIF h.state == 1;
        status ='DOWN';
        class = 'statusBGDOWN';
      ELSIF h.state == 2;
        status ='UNREACHABLE';
        class = 'statusBGDOWN';
      END;

      IF !status_color_background;
        SET class = 'statusEven' IF loop.odd;
        SET class = 'statusOdd'  IF loop.even;
      END;

      rowclasses = "";
      IF h.state_type == 1;
        rowclasses = "hard_state";
      ELSE;
        rowclasses = "soft_state";
      END;
    %]
      <tr id="[% paneprefix %]r[% loop.index %]"[% UNLESS authorized_for_read_only %] onmouseover="addRowSelector(this.id, 'host')"[% END %] class="[% rowclasses %]">
      [% loop_index = loop.index %]
      [% FOREACH col = table_columns.$paneprefix %]
        [% NEXT UNLESS col.checked %]
        [% IF col.field == "name" %]
          <td class='[% class %] name'>
            <div class="status col_host_name flex items-center min-w-max">
              <div class="flex-1">
                <a class="link" href="extinfo.cgi?type=1&amp;host=[% h.name | uri %]&amp;backend=[% h.peer_key | uri %]" title="[% h.address | html %] - [% h.alias | html %]">[% _host(h) | html %]</a>
              </div>
              [% PROCESS _status_host_attributes hostprefix="" host=h host_comment_count=comment_count with_status=1 %]
            </div>
          </td>
        [% ELSIF col.field == "peer_name" %]
          <td class='[% class %] backend_[% peer_key %] peer_name text-center'>[% pi_detail.$peer_key.peer_name %]</td>
        [% ELSIF col.field == "state" %]
          <td class='[% status %] state'>
            <div class='status col_state text-center'>[% status %]</div>
          </td>
        [% ELSIF col.field == "last_check" %]
          <td class='[% class %] last_check text-center'>[% last_check(c, h.last_check) %]</td>
        [% ELSIF col.field == "duration" %]
          <td class='duration text-center' title="[% IF h.last_state_change %][% format_date(h.last_state_change, datetime_format_long) %][% ELSE %]host has never changed its state yet.[% END %]">
            [% IF h.last_state_change %][% duration(date.now - h.last_state_change, 6) %][% ELSE %][% duration(date.now - program_start, 6) %]+[% END %]
          </td>
        [% ELSIF col.field == "current_attempt" %]
          [% current_attempt = h.current_attempt %]
          [% IF h.state_type == 1 && h.current_attempt == 1 && h.state != 0 %][% current_attempt = h.max_check_attempts %][% END %]
          <td class='[% class %] current_attempt text-center'>[% current_attempt %]/[% h.max_check_attempts %][% IF show_notification_number %][% IF h.current_notification_number && h.current_notification_number > 0 %] <span title="This host has sent out [% h.current_notification_number %] notifications">#[% h.current_notification_number %]</span>[% END %][% IF h.first_notification_delay && h.first_notification_delay > 0 %][% first_remaining = calculate_first_notification_delay_remaining(h) %][% IF first_remaining >= 0 %] <span title="This host will send out notifications in [% first_remaining %] minutes">~[% first_remaining %]min</span>[% END %][% END %][% END %]</td>
        [% ELSIF col.field == "plugin_output" %]
          [% IF h.has_been_checked == 0 %]
            <td class='[% class %] col_plugin_output truncate w-1/2 max-w-xs'>
              [% IF h.active_checks_enabled == 0 %]
                <div class="status col_plugin_output truncate">Host is not scheduled to be checked</div>
              [% ELSE %]
                <div class="status col_plugin_output truncate">Host check scheduled for [% format_date(h.next_check, datetime_format_long) %]</div>
              [% END %]
            </td>
          [% ELSE %]
            [% plugin_output      = remove_html_comments(h.plugin_output) %]
            [% on_demand_long_plugin_output = 0 %]
            [% IF h.defined("has_long_plugin_output") %]
              [% IF h.has_long_plugin_output %]
                [% on_demand_long_plugin_output = 1 %]
                [% long_plugin_output = "..." %]
              [% ELSE %]
                [% long_plugin_output = "" %]
              [% END %]
            [% ELSE %]
              [% long_plugin_output = remove_html_comments(h.long_plugin_output) %]
            [% END %]
            [% orig_plugin_output = plugin_output %]
            [% IF escape_html_tags %]
              [% long_plugin_output = escape_html(long_plugin_output) %]
              [% plugin_output      = escape_html(plugin_output) %]
            [% END %]
            <td class='plugin_output status col_plugin_output truncate w-1/2 max-w-xs'
              [% IF long_plugin_output and show_long_plugin_output == 'popup' %]
                [% IF on_demand_long_plugin_output %]
                  onclick="return(show_plugin_output_popup(this, '[% escape_js(h.name) %]', '', '[% escape_js(h.peer_key) %]', [% IF escape_html_tags %]true[% ELSE %]false[% END %], { 'width': 600 }));"
                [% ELSE %]
                  onclick="return(overcard({ 'bodyCls': 'p-2', 'body': this.abbr, 'caption': '[% escape_html(escape_js(_host(h))) %]', 'width': 600 }));"
                  abbr="[% plugin_output | replace_links | replace_copy_paste | escape_html %]&lt;br&gt;[% maybe_pre(long_plugin_output) | nl2br | replace_links | replace_copy_paste | escape_html %]"
                [% END %]
              [% END %]
            >
              <div class="status col_plugin_output flex items-center truncate">
                [% IF long_plugin_output and show_long_plugin_output == 'popup' %]
                  <div class="status col_plugin_output has_long_plugin_output font-bold link clickable flex-1 truncate">[% plugin_output | replace_links | replace_copy_paste %]</div>
                [% ELSE %]
                  <div class="status col_plugin_output flex-1 truncate">
                    [% plugin_output | replace_links | replace_copy_paste %]
                  </div>
                [% END %]
                [% IF long_plugin_output and show_long_plugin_output == 'inline' %]
                  <br>[% nl2br(maybe_pre(long_plugin_output)) | replace_links | replace_copy_paste %]
                [% END %]
                <div>
                  [% PROCESS _perfdata_table add_link="1" state=h.state plugin_output=orig_plugin_output perfdata=h.perf_data check_command=h.check_command hst=h.name svc="_HOST_" pnp_url=get_pnp_url(c, h, 1) %]
                </div>
              </div>
            </td>
          [% END %]
        [% ELSIF col.field == "address" %]
          <td class='[% class %] address'>[% h.address | html %]</td>
        [% ELSIF col.field == "alias" %]
          <td class='[% class %] alias'>[% h.alias | html %]</td>
        [% ELSIF col.field == "parents" %]
          <td class='[% class %] parents'>[% h.parents.join(', ') | html %]</td>
        [% ELSIF col.field == "check_command" %]
          <td class='[% class %] check_command'>[% h.check_command | strip_command_args | html %]</td>
        [% ELSIF col.field == "check_interval" %]
          <td class='[% class %] check_interval text-center'>[% h.check_interval | html %]</td>
        [% ELSIF col.field == "check_period" %]
          <td class='[% class %] check_period text-center'>[% h.check_period | html %]</td>
        [% ELSIF col.field == "contacts" %]
          <td class='[% class %] contacts truncate'>[% h.contacts.join(', ') | html %]</td>
        [% ELSIF col.field == "comments" %]
          <td class='[% class %] comments'>
            [% IF comments_by_host.defined(peer_key) && comments_by_host.$peer_key.defined(h.name) %]
              [% host_name = h.name %]
              [% FOREACH com = comments_by_host.$peer_key.$host_name %]
                [% IF com.defined("start_time") %]
                  Downtime: [% com.author | html %]: [% com.comment | html %] ([% date_format(c, com.start_time) %] - [% date_format(c, com.end_time) %])<br>
                [% ELSIF com.entry_type == 1 || com.entry_type == 4 %]
                  [% IF com.entry_type == 1 %]Comment[% ELSIF com.entry_type == 4 %]Acknowledgement[% END %]: [% com.author | html %]: [% com.comment | html %]<br>
                [% END %]
              [% END %]
            [% END %]
            </td>
        [% ELSIF col.field == "event_handler" %]
          <td class='[% class %] event_handler'>[% h.event_handler.replace("\!.*$", "") %]</td>
        [% ELSIF col.field == "execution_time" %]
          <td class='[% class %] execution_time text-center'>[% sprintf('%.2f', h.execution_time || 0) %]s</td>
        [% ELSIF col.field == "groups" %]
          <td class='[% class %] groups'>[% h.groups.join(', ') %]</td>
        [% ELSIF col.field == "latency" %]
          <td class='[% class %] latency text-center'>[% sprintf('%.2f', h.latency || 0) %]s</td>
        [% ELSIF col.field == "next_check" %]
          <td class='[% class %] next_check text-center'>[% IF h.next_check %][% date_format(c, h.next_check) %][% ELSE %]not scheduled[% END %]</td>
        [% ELSIF col.field == "notification_period" %]
          <td class='[% class %] notification_period text-center'>[% h.notification_period %]</td>
        [% ELSIF col.field == "percent_state_change" %]
          <td class='[% class %] percent_state_change text-center'>[% sprintf('%.2f', h.percent_state_change) %]%</td>
        [% ELSIF col.field == "in_notification_period" %]
          <td class='[% class %] in_notification_period text-center'>[% IF h.in_notification_period %]Yes[% ELSE %]No[% END %]</td>
        [% ELSIF col.field == "in_check_period" %]
          <td class='[% class %] in_check_period text-center'>[% IF h.in_check_period %]Yes[% ELSE %]No[% END %]</td>
        [% ELSIF col.field == "last_state_change" %]
          <td class='[% class %] last_state_change text-center'>[% last_check(c, h.last_state_change) %]</td>
        [% ELSIF col.field.match('^cust_') %]
          [% val = "";
            field = col.field.replace('^cust_', '');
            cust_vars = get_custom_vars(c, h, '', 1);
            IF cust_vars.exists(field); val = cust_vars.$field;
            ELSIF cust_vars.exists('HOST' _ field); field = 'HOST' _ field; val = cust_vars.$field;
            END;
          %]
          <td class="[% col.field | html %]">[% val %]</td>
        [% ELSIF col.field == "lmd_last_cache_update" && has_lmd %]
          <td class='[% class %] lmd_last_cache_update text-center'>[% date_format(c, h.lmd_last_cache_update) %]</td>
        [% ELSE %]
          <td></td>
        [% END %]
      [% END %]
      </tr>
    [% END %]
  </tbody>
</table>

<script>
<!--
  if(!window.default_columns) { default_columns = {}; }
  default_columns["[% paneprefix %]"] = "[% default_host_columns %]";
-->
</script>
