[% USE date %]
[% size = 1 %]
[% IF data %][% size = data.size + 1 %][% END %]
  <worksheet name='[% name %]' autofilter='A1:[% last_col %][% size %]'>
    <row>
      <format bold='1' bg_color='silver' align='center'>
        [% FOREACH col = columns %]
        [% IF    col == "Site"          %]<cell width='15'>Site</cell>
        [% ELSIF col == "Hostname"      %]<cell width='30'>Hostname</cell>
        [% ELSIF col == "Displayname"   %]<cell width='30'>Hostname</cell>
        [% ELSIF col == "Alias"         %]<cell width='30'>Alias</cell>
        [% ELSIF col == "IP"            %]<cell width='10'>IP</cell>
        [% ELSIF col == "Status"        %]<cell width='20'>Status</cell>
        [% ELSIF col == "Parents"       %]<cell width='30'>Parents</cell>
        [% ELSIF col == "Groups"        %]<cell width='30'>Groups</cell>
        [% ELSIF col == "Acknowledged"  %]<cell width='14'>Acknowledged</cell>
        [% ELSIF col == "Downtime"      %]<cell width='12'>Downtime</cell>
        [% ELSIF col == "Notifications" %]<cell width='12'>Notifications</cell>
        [% ELSIF col == "Active Checks" %]<cell width='14'>Active Checks</cell>
        [% ELSIF col == "Flapping"      %]<cell width='9'>Flapping</cell>
        [% ELSIF col == "Last Check"    %]<cell width='20'>Last Check</cell>
        [% ELSIF col == "Duration"      %]<cell width='15'>Duration</cell>
        [% ELSIF col == "Status Information"       %]<cell width='80'>Status Information</cell>
        [% ELSIF col == "Extra Status Information" %]<cell width='80'>Extra Status Information</cell>
        [% ELSIF col == "Contacts"      %]<cell width='80'>Contacts</cell>
        [% ELSIF col == "Comments"      %]<cell width='80'>Comments</cell>
        [% ELSE %]<cell width='30'>[% escape_xml(col) %]</cell>
        [% END %]
        [% END %]
      </format>
    </row>
    [% FOREACH h = data %]
      [% SET peer_key      = h.peer_key %]
      [% SET program_start = pi_detail.$peer_key.program_start %]

      [% IF h.has_been_checked == 0 %]
        [% state       = 'PENDING' %]
        [% state_color = 'grey' %]
      [% ELSIF h.state == 0 %]
        [% state       = 'OK' %]
        [% state_color = 'green' %]
      [% ELSIF h.state == 1 %]
        [% state       = 'DOWN' %]
        [% state_color = 'red' %]
      [% ELSIF h.state == 2 %]
        [% state       = 'UNREACHABLE' %]
        [% state_color = 'red' %]
      [% END %]
      <row>
        [% FOREACH col = columns %]
        [% IF    col == "Site"          %]<cell>[% escape_xml(pi_detail.$peer_key.peer_name) %]</cell>
        [% ELSIF col == "Hostname"      %]<cell>[% escape_xml(h.name) %]</cell>
        [% ELSIF col == "Displayname"   %]<cell>[% escape_xml(h.display_name) %]</cell>
        [% ELSIF col == "Alias"         %]<cell>[% escape_xml(h.alias) %]</cell>
        [% ELSIF col == "IP"            %]<cell>[% escape_xml(h.address) %]</cell>
        [% ELSIF col == "Status"        %]<format bg_color='[% state_color %]'><cell>[% state %]</cell></format>
        [% ELSIF col == "Parents"       %]<cell>[% escape_xml(h.parents.join(', ')) %]</cell>
        [% ELSIF col == "Groups"        %]<cell>[% escape_xml(h.groups.join(', ')) %]</cell>
        [% ELSIF col == "Acknowledged"  %]<cell>[% IF h.acknowledged             %]Yes[% ELSE %]No[% END %]</cell>
        [% ELSIF col == "Downtime"      %]<cell>[% IF h.scheduled_downtime_depth %]Yes[% ELSE %]No[% END %]</cell>
        [% ELSIF col == "Notifications" %]<cell>[% IF h.notifications_enabled    %]Yes[% ELSE %]No[% END %]</cell>
        [% ELSIF col == "Active Checks" %]<cell>[% IF h.active_checks_enabled    %]Yes[% ELSE %]No[% END %]</cell>
        [% ELSIF col == "Flapping"      %]<cell>[% IF h.is_flapping              %]Yes[% ELSE %]No[% END %]</cell>
        [% ELSIF col == "Last Check"    %]<format num_format='yyyy-mm-dd hh:mm:ss'><cell type='date_time'>[% IF h.has_been_checked == 0 %][% ELSE %][% format_date(h.last_check, '%Y-%m-%dT%H:%M:%S') %][% END %]</cell></format>
        [% ELSIF col == "Duration"                 %]<cell>[% IF h.last_state_change %][% duration(date.now - h.last_state_change) %][% ELSE %][% duration(date.now - program_start) %]+[% END %]</cell>
        [% ELSIF col == "Status Information"       %]<cell>[% IF h.has_been_checked == 0 %]Service check scheduled for [% format_date(h.next_check, datetime_format_long) %][% ELSE %][% escape_xml(h.plugin_output) %][% END %]</cell>
        [% ELSIF col == "Extra Status Information" %]<cell>[% IF h.has_been_checked == 1 %][% escape_xml(h.long_plugin_output) %][% END %]</cell>
        [% ELSIF col == "Contacts"                 %]<cell>[% escape_xml(h.contacts.join(', ')) %]</cell>
        [% ELSIF col == "Comments"      %]
          <cell>
            [%- IF comments_by_host.defined(peer_key) && comments_by_host.$peer_key.defined(h.name) -%]
              [%- host_name = h.name; first = 1; -%]
              [%- FOREACH com = comments_by_host.$peer_key.$host_name -%]
                [%- IF com.defined("start_time") -%]
                  [% IF !first; ", "; END; first = 0; %]Downtime: [% escape_xml(com.author) %]: [% escape_xml(com.comment) %] ([% format_date(com.start_time, datetime_format) %] - [% format_date(com.end_time, datetime_format) %])
                [%- ELSIF com.entry_type == 1 || com.entry_type == 4 -%]
                  [%- IF com.entry_type == 1 %][% IF !first; ", "; END; first = 0; %]Comment[% ELSIF com.entry_type == 4 %]Acknowledgement[% END %]: [% escape_xml(com.author) | html %]: [% escape_xml(com.comment) -%]
                [%- END -%]
              [%- END -%]
            [%- END -%]
          </cell>
        [% ELSE %]
          [% val = "";
             cust_vars = get_custom_vars(c, h);
             IF cust_vars.exists(col); val = cust_vars.$col;
             ELSE;
               field = col.replace('^_', '');
               IF cust_vars.exists(field); val = cust_vars.$field;
               END;
             END;
          %]
          <cell>[% escape_xml(val) %]</cell>
        [% END %]
        [% END %]
      </row>
    [% END %]
  </worksheet>
