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

namespace Kufer\KuferTools\Utils\WssField;

class WssFieldTeilnehmerErmaessigungProzent extends WssField
{
  protected function getValue($datamodulWert, $isFormular = false)
  { 
    if ($isFormular) {
      $combobox = \ComboboxFactory::getComboboxByColumn($this->datamodulColumn, parent::getValue($datamodulWert, $isFormular));

      return $combobox->getSelectedText();
    }

    return parent::getValue($datamodulWert);
  }

  public function getChangedInfo($columnValues, $changedDm, $changedFields): array
  {
    if ($this->datamodulColumn->accesslevel == 2 && $this->datamodulColumn->canedit == 1) {
      $datamodul = $this->getDatamodulName();
      $fieldname = $this->getFieldName();

      if (isset($columnValues[$datamodul][$fieldname])) {
        $value1 = $changedDm->getVarValueEx('erm_grund', $datamodul);
        $value2 = \PostOperations::getVarValue('teilnehmer_erm_grund_und_proz', 0);
        if ($value1 != $value2) {
          $dbwErmaessigung = new \DBWorkErmaessigung(1, true);
          $ermaessigungAry = $dbwErmaessigung->getErmaessigungbyNr($value2);
          if (count($ermaessigungAry) > 0) {
            $changedDm->setValue2VarEx('erm_grund', $ermaessigungAry[0]->nr, $datamodul);
            $changedDm->setValue2VarEx('erm_proz', $ermaessigungAry[0]->prozente, $datamodul);
            $changedFields[] = 'erm_grund';
            $changedFields[] = 'erm_proz';
          }
        }
      }
    }

    return array('changed' => $changedDm, 'changedFields' => $changedFields);
  }
}