[% USE date %]
<workbook>
  [% size = 1 %]
  [% IF data %][% size = data.size + 1 %][% END %]
    <worksheet name='Business Process' 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 == "Name"                        %]<cell width='30'>Name</cell>
          [% ELSIF col == "Status"                      %]<cell width='10'>Status</cell>
          [% ELSIF col == "Last Check"                  %]<cell width='16'>Last Check</cell>
          [% ELSIF col == "Duration"                    %]<cell width='14'>Duration</cell>
          [% ELSIF col == "Status Information"          %]<cell width='14'>Status Information</cell>
          [% ELSE %]<cell width='30'>[% escape_xml(col) %]</cell>
          [% END %]
          [% END %]
        </format>
      </row>
      [% FOREACH b = data %]
        [% peer_key = "" %]
        [% IF b.defined("bp_backend"); peer_key = b.bp_backend; END %]
        [% IF b.status == 4 %]
          [% state       = 'PENDING' %]
          [% state_color = 'grey' %]
        [% ELSIF b.status == 0 %]
          [% state       = 'OK' %]
          [% state_color = 'green' %]
        [% ELSIF b.status == 1 %]
          [% state       = 'WARNING' %]
          [% state_color = 'yellow' %]
        [% ELSIF b.status == 2 %]
          [% state       = 'CRITICAL' %]
          [% state_color = 'red' %]
        [% ELSIF b.status == 3 %]
          [% state       = 'UNKNOWN' %]
          [% state_color = 'orange' %]
        [% END %]
        <row>
          [% FOREACH col = columns %]
          [% IF    col == "Site"                     %]<cell>[% IF peer_key; escape_xml(pi_detail.$peer_key.peer_name); END %]</cell>
          [% ELSIF col == "Name"                     %]<cell>[% escape_xml(b.name) %][% IF b.draft %](DRAFT)[% END %]</cell>
          [% ELSIF col == "Status"                   %]<format bg_color='[% state_color %]'><cell>[% state %]</cell></format>
          [% ELSIF col == "Last Check"               %]<format num_format='yyyy-mm-dd hh:mm:ss'><cell type='date_time'>[% IF b.last_check == 0 %][% ELSE %][% format_date(b.last_check, '%Y-%m-%dT%H:%M:%S') %][% END %]</cell></format>
          [% ELSIF col == "Duration"                 %]<cell>[% IF b.last_check %][% duration(date.now - b.last_check, 6) %][% ELSE %][% END %]</cell>
          [% ELSIF col == "Status Information"       %]<cell>[% escape_xml(b.status_text) %]</cell>
          [% ELSE %]
            [% val = "";
              cust_vars = get_custom_vars(c, b,'',1);
              IF cust_vars.exists(col); val = cust_vars.$col;
              ELSIF cust_vars.exists('HOST' _ col); col = 'HOST' _ col; val = cust_vars.$col;
              ELSE;
                field = col.replace('^_', '');
                IF cust_vars.exists(field); val = cust_vars.$field;
                ELSIF cust_vars.exists('HOST' _ field); field = 'HOST' _ field; val = cust_vars.$field;
                END;
              END;
            %]
            <cell>[% escape_xml(val) %]</cell>
          [% END %]
          [% END %]
        </row>
      [% END %]
    </worksheet>
</workbook>
