<?php
/**
 * @copyright (c) Kufer Software GmbH, Loisachstrasse 4, D-84453 Mühldorf
 */

namespace Kufer\Web\Webbasys\Includee\Datamenu\TerminAnfrage;

use Kufer\Web\Webbasys\Includee\anmeldung as NsCheck;

class DataMenuTerminAnfrageFormular extends \DataMenuMainTopLeft
{
  private $pflichtfeldFehler = array();
  private $formatFehler = array();

  private $exportadapterKurse;

  private $sightId = 805;

  private $factory;

  public function __construct($urlparameter, $factory)
  {
    parent::__construct($urlparameter);

    $this->exportadapter = new \Kufer\Web\Classes\ExportAdapter\ExpTerminAnfrage();
    $this->exportadapter->enforceParamForValue();
    $this->exportadapterKurse = new \ExpKurse();

    $this->factory = $factory;
  }

  public function prepareStep()
  {
    if (isset($this->urlparameter->knr) && $this->urlparameter->knr !== '') {
      $this->factory->setKnrToSession($this->urlparameter->knr);
    }
  }

  public function canAccess(): bool
  {
    return true;
  }

  public function checkEingaben($terminAnfrage): bool
  {
    if (!is_null($terminAnfrage)) {
      $check = new NsCheck\WBTerminAnfrageFormularCheck();
      $this->getsight();

      $check->vorlage = $this->sight->body . ' ' . $this->sight->footer;
      $check->exportAdapter = $this->exportadapter;
      $check->setDatamodul($terminAnfrage);
      if (!$check->check()) {
        $this->pflichtfeldFehler = $check->pflichtfeldFehlerArray;
        $this->formatFehler = $check->formatFehlerArray;

        return false;
      }
    }

    return true;
  }

  public function getsight($sightid = 0)
  {
    parent::getsight($this->sightId);
  }

  public function getdataarrayheader()
  {
    $knr = $this->factory->getKnrFromSession();
    $this->dataarrayheader = array(\WBKursStore::getKurs($knr));
  }

  protected function innerContent() : void
  {
    if (!\SessionOperations::isDmzBetrieb()) {
      echo '<div class="error_message>Diese Funktion steht in dieser Betriebsart nicht zur Verfügung</div>';

      return;
    }

    
    $terminAnfrage = \SessionOperations::getTerminAnfrageFromSession();
    if ($terminAnfrage == null) {
      $terminAnfrage = new \Kufer\Web\Classes\Datamodul\TerminAnfrage();
    }

    $this->dataarrayfooter = $this->dataarraybody = array($terminAnfrage);
    $this->exportadapter->setFehler($this->pflichtfeldFehler, $this->formatFehler);

    echo $this->getsightcomplete($this->sightId, $this->exportadapter, $this->exportadapterKurse);
  }
}