<?php
/**
 * @copyright (c) Kufer Software GmbH, Oderstraße 7, D-84453 Mühldorf
 */


class Einsatzplanliste extends Kurstageliste
{
  
  private $summeKurstage = 0;

  private $showAusgefalleneKT;

  private $showStatus;

  private $firstColBeginn;
  private $firstColBreite;

  protected function getOutputName(): string
  {
    return 'Einsatzplanliste.pdf';
  }

  public function getListID()
  {
    return 13;
  }

  public function getListTyp()
  {
    return '11';
  }

  public function setTitle1()
  {
    $this->settings->title1 = 'Einsatzplan';
  }

  public function loadFactoryDefaults()
  {
    parent::loadFactoryDefaults();
    $this->setTitle1();  
  }

  public function _getKursCellHeight()
  {
    return 7;
  }

  
  protected function _getZeitraumAnzeige()
  {
    return ' von ' . $this->kurstage->von . ' - ' . $this->kurstage->bis;
  }

  
  protected function getDozentAnzeige()
  {
    return ' ';
  }

  
  protected function getZusatzTextFuerUeberschrift()
  {
    return 'für ' . $this->dozent0;
  }

  protected function _getPositionStand()
  {
    return '83%';
  }

  public function setDrucksettings()
  {
    $this->settings = new \webtools\drucklisten\EinsatzplanListSettings();
  }

  private function getDrucksettingsOptions()
  {
    $this->showAusgefalleneKT = ($this->settings->einstellDarstellung->option1 == 1);
    $this->showStatus = ($this->settings->einstellDarstellung->option2 == 1);
  }

  
  public function getEinstellDarstellungText($optionName)
  {
    switch ($optionName) {
      case 'option1':
        return 'mit ausgefallenen Tagen';
      case 'option2':
        return 'mit Kurstage-Status';
      default:
        return;
    }
  }

  
  public function _prepare($pKnr = '')
  {
    parent::_prepare($pKnr);
    $this->getDrucksettingsOptions();

    $this->spaltenbeginnary = array(
        'datum' => 3,
        'von' => 13,
        'bis' => 17,
        'UE' => 21,
        'status' => 25,
        'ort' => 25,
        'nr' => 54,
        'titel' => 63
        );

    $this->spaltenbreiteary = array(
        'datum' => 13,
        'von' => 8.5,
        'bis' => 8.5,
        'UE' => 6,
        'status' => 5,
        'ort' => 24,
        'nr' => 10,
        'titel' => 30
        );

    if ($this->showStatus) {
      $this->spaltenbeginnary = $this->getShiftedBeginnArray($this->spaltenbeginnary, $this->spaltenbreiteary, 'status');
    }

    $this->firstColBeginn = $this->spaltenbeginnary['datum'];
    $this->firstColBreite = $this->spaltenbreiteary['datum'];
  }

  
  private function getShiftedBeginnArray($beginnArray, $breiteArray, $fieldname)
  {
    $newBeginnArray = $beginnArray;
    foreach ($beginnArray as $colName => $beginn) {
      if ($beginn >= $beginnArray[$fieldname] && $colName != $fieldname) {
        $newBeginnArray[$colName] = $beginnArray[$colName] + $breiteArray[$fieldname];
      }
    }

    return $newBeginnArray;
  }

  public function setKurstagary($pDbwkurstage)
  {
    parent::setKurstagary($pDbwkurstage);
    if ($this->settings->einstellDarstellung->option1 == false) {
      $this->kursetage = array_filter($this->kursetage, function($kurstag) {
        return $kurstag->ausfall != 'A';
      });
    }
  }

  

  
  protected function _printDruckListe($pKurs = '')
  {
    $sprachSettings = new SprachSettingsObject();

    
    $this->pdf->CellAtX($this->spaltenbeginnary['datum'] . '%', 'Datum', 'B', '', 11, $this->spaltenbreiteary['datum'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['von'] . '%', 'von', 'B', '', 11, $this->spaltenbreiteary['von'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['bis'] . '%', 'bis', 'B', '', 11, $this->spaltenbreiteary['bis'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['UE'] . '%', 'UE', 'B', '', 11, $this->spaltenbreiteary['UE'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['ort'] . '%', 'Ort', 'B', '', 11, $this->spaltenbreiteary['ort'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['nr'] . '%', $sprachSettings->Kurs['Singular']['Nominativ'] . '-Nr.', 'B', '', 11, $this->spaltenbreiteary['nr'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['titel'] . '%', 'Titel', 'B', '', 11, $this->spaltenbreiteary['titel'] . '%', 0, $this->height);
    if ($this->showStatus) {
      $this->pdf->CellAtX($this->spaltenbeginnary['status'] . '%', 'Status', 'B', '', 11, $this->spaltenbreiteary['status'] . '%', 0, $this->height);
    }

    
    $this->pdf->ln(6);
    $this->pdf->CellAtX('3%', '', '', '', '', '92%', 1, 0);
    $this->pdf->ln(3);

    

    
    foreach ($this->kursetage as $kurstag) {
      $this->_printKursCell($kurstag);
    }

    
    $this->pdf->CellAtX($this->firstColBeginn . '%', '', '', '', '', '92%', 1, 0);
    $this->pdf->ln(2);

    
    $this->pdf->CellAtX($this->firstColBeginn . '%', 'Anzahl der ' . $sprachSettings->Kurstag['Plural']['Nominativ'] . ':', 'B', '', '', '25%', '', $this->height);
    $this->pdf->CellAtX($this->firstColBeginn + 13 . '%', $this->summeKurstage, 'B', '', '', '25%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['UE'] . '%', 'Anzahl der UE:', 'B', '', '', '30%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['UE'] + 10 . '%', MathOperations::floatToString($this->summeUE), 'B', '', '', '30%', '', $this->height);
  }

  
  public function _printKursCell($pKurstag)
  {
    $ue = MathOperations::stringToFloat($pKurstag->ue);
    
    $this->summeKurstage++;
    $this->summeUE += $ue;
    $this->pdf->CellAtX($this->spaltenbeginnary['datum'] . '%', $pKurstag->wotagkurz, '', '', '', $this->spaltenbreiteary['datum'] . '%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['datum'] + 2.5 . '%', $pKurstag->datum, '', '', '', $this->spaltenbreiteary['datum'] - 3 . '%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['von'] . '%', $pKurstag->zeit_beg, '', '', '', $this->spaltenbreiteary['von'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['bis'] . '%', $pKurstag->zeit_ende, '', '', '', $this->spaltenbreiteary['bis'] . '%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['UE'] . '%', MathOperations::floatToString($ue), '', '', '', $this->spaltenbreiteary['UE'] . '%', '', $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['ort'] . '%', $pKurstag->ort, '', '', '', $this->spaltenbreiteary['ort'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['nr'] . '%', $pKurstag->kurs->id, '', '', '', $this->spaltenbreiteary['nr'] . '%', 0, $this->height);
    $this->pdf->CellAtX($this->spaltenbeginnary['titel'] . '%', $pKurstag->kurs->kurzbez, '', '', '', $this->spaltenbreiteary['titel'] . '%', 0, $this->height);
    if ($this->showStatus) {
      $this->pdf->CellAtX($this->spaltenbeginnary['status'] . '%', $pKurstag->ausfall, '', '', '', $this->spaltenbreiteary['status'] . '%', 0, $this->height);
    }
    $this->pdf->ln($this->height);
  }
}