<?php
class PDF extends FPDF
{

  function Header()
  {
    $tom                      = new tom();
    $this->SetFont('Arial','B',15);
    $this->SetY(20);
    $this->SetX(20);
    $this->Cell(250,6,'TOM - ' . $tom->getClientCompany($_GET['id']) . ' - Seite '.$this->PageNo().'/{nb}');
    $this->Ln(10);
  }

  function Footer()
  {
    // Position at 1.5 cm from bottom
    $this->SetY(-15);
    // Arial italic 8
    $this->SetFont('Arial','B',10);
    // Page number
    $this->Cell(0,10,'Seite '.$this->PageNo().'/{nb}',0,0,'');
  }
}


$pdf                              = new PDF('L', 'mm', 'A4');
$pdf->SetCreator("DSB-Holzheu");
$pdf->SetAuthor("DSB-Holzheu");
$pdf->AliasNbPages();
$pdf->AddPage();
$replaceStatisticsArray           = array(
    '&auml;',
    '&ouml;',
    '&uuml;',
    '&Auml;',
    '&Ouml;',
    '&Uuml;',
);
$replacementStatisticsArray       = array(
    'ä',
    'ö',
    'ü',
    'Ä',
    'Ö',
    'Ü',
);

$pdf->SetFont('Arial','',12);
$pdf->SetY(30);
$pdf->SetX(120);
$pdf->Cell(50,3,'gesamt');
$pdf->SetX(140);
$pdf->Cell(50,3,'kritisch');
$pdf->SetX(160);
$pdf->Cell(50,3,'fehlt');
$pdf->SetX(180);
$pdf->Cell(50,3,iconv("UTF-8","iso-8859-1",'erfüllt'));
$pdf->SetX(200);
$pdf->Cell(50,3,'n. z.');
$pdf->SetX(220);
$pdf->Cell(50,3,iconv("UTF-8","iso-8859-1",'prüfen'));
$pdf->SetX(240);
$pdf->Cell(50,3,'unbearbeitet');


$amountBlocks                     = 1;
foreach ($rowsStatistics as $rowStatistics) {
  $y1                             = $pdf->GetY();
  $themenblock                    = str_replace($replaceStatisticsArray, $replacementStatisticsArray, $tom->getThemenblockName($rowStatistics['themenblock_id']));
  $questsTotal                    = $tom->getAmountQuestsPerBlock($_GET['id'], $rowStatistics['themenblock_id']);
  $questsKritisch                 = $tom->getAmountQuestsPerBlockByStatus($_GET['id'], $rowStatistics['themenblock_id'], 1);
  $questsFehlt                    = $tom->getAmountQuestsPerBlockByStatus($_GET['id'], $rowStatistics['themenblock_id'], 2);
  $questsErfuellt                 = $tom->getAmountQuestsPerBlockByStatus($_GET['id'], $rowStatistics['themenblock_id'], 3);
  $questsNichtZutreffend          = $tom->getAmountQuestsPerBlockByStatus($_GET['id'], $rowStatistics['themenblock_id'], 4);
  $questsPruefen                  = $tom->getAmountQuestsPerBlockByStatus($_GET['id'], $rowStatistics['themenblock_id'], 5);
  $questsUnbearbeitet             = $tom->getAmountQuestsPerBlockByStatus($_GET['id'], $rowStatistics['themenblock_id'], 6);
  $pdf->SetFont('Arial','B',12);
  $pdf->SetY($y1 +5);
  $pdf->SetX(20);
  $pdf->Cell(50,4,iconv("UTF-8","iso-8859-1",$themenblock));
  $pdf->SetFont('Arial','',12);
  $pdf->SetY($y1 +5);
  $pdf->SetX(120);
  $pdf->Cell(18,4,$questsTotal,1,2,C);
  $pdf->SetY($y1 +5);
  $pdf->SetX(140);
  if ($questsKritisch             != 0) {
    $pdf->setFillColor(255, 88, 88);
    $pdf->Cell(18,4,$questsKritisch,1,2,L,1);
  } else {
    $pdf->Cell(18,4,$questsKritisch,1,2,C);
  }
  
  $pdf->SetY($y1 +5);
  $pdf->SetX(160);
  if ($questsFehlt                != 0) {
    $pdf->setFillColor(247, 190, 129);
    $pdf->Cell(18,4,$questsFehlt,1,2,L,1);
  } else {
    $pdf->Cell(18,4,$questsFehlt,1,2,C);
  }
  
  $pdf->SetY($y1 +5);
  $pdf->SetX(180);
  if ($questsErfuellt              != 0) {
    $pdf->setFillColor(0, 255, 64);
    $pdf->Cell(18,4,$questsErfuellt,1,2,L,1);
  } else {
    $pdf->Cell(18,4,$questsErfuellt,1,2,C);
  }
  
  $pdf->SetY($y1 +5);
  $pdf->SetX(200);
  if ($questsNichtZutreffend        != 0) {
    $pdf->setFillColor(230, 230, 230);
    $pdf->Cell(18,4,$questsNichtZutreffend,1,2,L,1);
  } else {
    $pdf->Cell(18,4,$questsNichtZutreffend,1,2,C);
  }
  
  $pdf->SetY($y1 +5);
  $pdf->SetX(220);
  if ($questsPruefen                  != 0) {
    $pdf->setFillColor(243, 247, 149);
    $pdf->Cell(18,4,$questsPruefen,1,2,L,1);
  } else {
    $pdf->Cell(18,4,$questsPruefen,1,2,C);
  }
  
  $pdf->SetY($y1 +5);
  $pdf->SetX(240);
  $pdf->Cell(18,4,$questsUnbearbeitet,1,2,C);
  
  $amountBlocks++;
  if ($amountBlocks               >= 17) {
    $amountBlocks                 = 1;
    $pdf->AddPage();
  }
}
$yAfter                           = $pdf->GetY();
$questsTotal                      = $tom->getAmountQuestsTotal($_GET['id']);
$questsKritischTotal              = $tom->getAmountQuestsTotalByStatus($_GET['id'], 1);
$questsFehltTotal                 = $tom->getAmountQuestsTotalByStatus($_GET['id'], 2);
$questsErfuelltTotal              = $tom->getAmountQuestsTotalByStatus($_GET['id'], 3);
$questsNichtZutreffendTotal       = $tom->getAmountQuestsTotalByStatus($_GET['id'], 4);
$questsPruefenTotal               = $tom->getAmountQuestsTotalByStatus($_GET['id'], 5);
$questsUnbearbeitetTotal          = $tom->getAmountQuestsTotalByStatus($_GET['id'], 6);
$pdf->SetFont('Arial','B',12);
$pdf->SetY($yAfter +5);
$pdf->SetX(20);
$pdf->Cell(18,4,'Fragen gesamt');

$pdf->SetFont('Arial','',12);
$pdf->SetY($yAfter +5);
$pdf->SetX(120);
$pdf->Cell(18,4,$questsTotal,1,2,C);
$pdf->SetY($yAfter +5);
$pdf->SetX(140);
$pdf->Cell(18,4,$questsKritischTotal,1,2,C);
$pdf->SetY($yAfter +5);
$pdf->SetX(160);
$pdf->Cell(18,4,$questsFehltTotal,1,2,C);
$pdf->SetY($yAfter +5);
$pdf->SetX(180);
$pdf->Cell(18,4,$questsErfuelltTotal,1,2,C);
$pdf->SetY($yAfter +5);
$pdf->SetX(200);
$pdf->Cell(18,4,$questsNichtZutreffendTotal,1,2,C);
$pdf->SetY($yAfter +5);
$pdf->SetX(220);
$pdf->Cell(18,4,$questsPruefenTotal,1,2,C);
$pdf->SetY($yAfter +5);
$pdf->SetX(240);
$pdf->Cell(18,4,$questsUnbearbeitetTotal,1,2,C);

$pdf->AddPage();
$replaceArray                     = array(
    '&bull;',
    '&quot;',
);
$replacementArray                 = array(
    '- ',
    '"'
);

foreach ($rows as $row) {
  $frage                          = str_replace($replaceArray, $replacementArray, $row['frage']);
  $empfehlung                     = str_replace($replaceArray, $replacementArray, $row['bemerkung_begruendung']);
  $status                         = $tom->getStatusVal($row['status']);
  
  $y1                             = $pdf->GetY();
  $pdf->SetFont('Arial','B',12);
  $pdf->SetY($y1 +10);
  $pdf->SetX(20);
  
  switch ($row['status']) {
    case 1:
      $pdf->setFillColor(255, 88, 88);
      break;
    case 2:
      $pdf->setFillColor(247, 190, 129);
      break;
    case 3:
      $pdf->setFillColor(0, 255, 64);
      break;
    case 4:
      $pdf->setFillColor(230, 230, 230);
      break;
    case 5:
      $pdf->setFillColor(243, 247, 149);
      break;
    case 6:
      $pdf->setFillColor(255, 255, 255);
      break;
    default:
      $pdf->setFillColor(255, 255, 255);
  }
  $pdf->Cell(220,6,'Themenblock: ' . html_entity_decode($tom->getThemenblockName($row['themenblock_id'], ENT_QUOTES, "iso-8859-1")) . ' - Frage: ' . $row['themenfrage_id'] . ' - Status: ' . iconv("UTF-8","iso-8859-1",$status),0,1,L,1);
  $pdf->SetFont('Arial','B',12);
  $pdf->SetY($y1 + 20);
  $pdf->SetX(20);
  $pdf->Cell(100,6,iconv("UTF-8","iso-8859-1","Maßnahme"));
  $pdf->SetX(160);
  $pdf->Cell(85,6,iconv("UTF-8","iso-8859-1","Bemerkung / Begründung"));
  $y2                             = $pdf->GetY();
  $pdf->SetFont('Arial','',10);
  $pdf->SetY($y2 + 10);
  $pdf->SetX(20);
  $pdf->MultiCell(120,6, html_entity_decode($frage, ENT_QUOTES, "iso-8859-1"));
  $y3                             = $pdf->GetY();
  $pdf->SetY($y2 + 10);
  $pdf->SetX(160);
  $pdf->MultiCell(120,6, html_entity_decode($empfehlung, ENT_QUOTES, "iso-8859-1"));
  $y4                         = $pdf->GetY();
  $pdf->SetFont('Arial','B',10);
  
  if ($y4                     >= $y3) {
    $maxY                     = $y4;
  } else {
    $maxY                     = $y3;
  }
  
  $pdf->SetY($maxY + 5);
  $pdf->SetX(20);
  $pdf->Cell(260,1,'',1,2);
   
  if ($maxY                   >= 120) {
    $pdf->AddPage();
  }
}


$pdf->Output( 'tom-report.pdf', 'I' );