[% USE date %]

[%# RENDERING PDF #%]
[% IF block == 'render' %]
[%
  init_pdf(pdf);

  # PAGE 1
  pdf.prForm(path_to_template('pdf/sla.pdf'), 1);
  pdf.prFont('Helvetica-Bold');
  font(20, 'light blue');
  IF param.exists('hostgroup');
    pdf.prText(40,400,'Hostgroup: ' _ param.hostgroup );
  ELSIF param.exists('servicegroup');
    pdf.prText(40,400,'Servicegroup: ' _ param.servicegroup );
  ELSIF param.exists('service');
    pdf.prText(40,400,'Service: ' _ param.service _ ' on ' _ param.host );
  ELSE;
    pdf.prText(40,400,'Host: ' _ param.host );
  END;
  font(15, 'dark grey');
  pdf.prText(40,350, "Report Timeperiod: " _ format_date(start, "%d.%m.%Y") _ ' - ' _ format_date((end - 1), "%d.%m.%Y"));
  pdf.prText(480, 440, format_date(date.now, "%d.%m.%Y"));

  # availability bar chart
  page = 2;
  IF param.exists('host') OR param.exists('hostgroup') OR param.exists('servicegroup');
    FOREACH hst = avail_data.hosts.keys.sort;
      c.request.parameters.host    = hst;
      c.request.parameters.service = "";
      pdf.prPage();
      pdf.prForm(path_to_template('pdf/sla.pdf'), 3);
      font(20, 'dark grey');
      pdf.prText(40,680, 'Host: ' _ hst);
      bar_pdf = render_bar_chart();
      pdf.prDocForm({file => bar_pdf, x => 25, y => 220});
      font(9, 'dark grey');
      fill_availability_table(40, 147, 'dark grey', 'red');

      page_footer(550, 58, page); page = page + 1;
    END;
  END;

  IF param.exists('service') OR param.exists('servicegroup');
    FOREACH hst = avail_data.services.keys.sort;
      FOREACH svc = avail_data.services.$hst.keys.sort;
        c.request.parameters.host    = hst;
        c.request.parameters.service = svc;
        pdf.prPage();
        pdf.prForm(path_to_template('pdf/sla.pdf'), 3);
        font(20, 'dark grey');
        pdf.prText(40,680, 'Service: ' _ svc _ ' on ' _ hst);
        bar_pdf = render_bar_chart();
        pdf.prDocForm({file => bar_pdf, x => 25, y => 220});
        font(9, 'dark grey');
        fill_availability_table(40, 147, 'dark grey', 'red');

        page_footer(550, 58, page); page = page + 1;
      END;
    END;
  END;


  # LAST PAGE
  # logfile entries
  IF param.exists('host') OR param.exists('service');
    pdf.prPage();
    pdf.prForm(path_to_template('pdf/sla.pdf'), 2);
    font(20, 'dark grey');
    pdf.prText(40,680, 'Details');

    font(10, 'dark grey');
    outages(logs, start, end, 50, 640, 17, 20, 10);

    page_footer(550, 58, page); page = page + 1;
  END;
%]
[% END %]
