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

namespace Kufer\Web\KuferTools\Utils\WssField\KursPlanung;

class WssFieldKursplanungDokumente extends \Kufer\Web\KuferTools\Utils\WssField\WssField
{
  protected function getFieldBearbeitbar($datamodulWert, $isFormular, string $requestPrefix = '')
  {
    return $this->_buildField($this->datamodulColumn, $datamodulWert, 2, false);
  }

  protected function getFieldNichtBearbeitbar($datamodulWert, $isFormular, string $requestPrefix = '')
  {
    if ($isFormular) {
      return $this->_buildField($this->datamodulColumn, $datamodulWert, 1, false);
    } else {
      return parent::getFieldNichtBearbeitbar($datamodulWert, $isFormular, $requestPrefix);
    }
  }

  protected function getValue($datamodulWert, $isFormular = false)
  {
    if (is_array($datamodulWert->dokumente) && count($datamodulWert->dokumente) > 0) {
      $dokZeilen = array();
      foreach ($datamodulWert->dokumente as $dokument) {
        $dokZeilen[] = '<span>' . $this->htmlEncode($dokument->dateiname) . '</span>';
      }

      return implode('<br>', $dokZeilen);
    } else {
      return 'keine Dokumente angegeben';
    }
  }

  protected function encodeValue($value)
  {
    return $this->dontEncode($value);
  }

  public function inPost(string $requestPrefix = ''): bool
  {
    return \PostOperations::isDefined(($requestPrefix === '' ? '' : $requestPrefix . '_') . 'kursplanung_dokumente_hidden');
  }

  protected function isMandatoryValueEmpty($value): bool
  {
    return count($value) === 0;
  }

  public function getValueFromPost(string $requestPrefix = '')
  {
    $jsonBase64 = \PostOperations::getVarValue(($requestPrefix === '' ? '' : $requestPrefix . '_') . 'kursplanung_dokumente_hidden');
    
    $dokumente = \JsonOperations::getFromStringBase64InternalEncoding($jsonBase64);


    $doks = array();
    foreach ($dokumente as $dokument) {
      $dok = new \KursplanungDokumente();
      $dok->id = $dokument->id;
      $dok->dokument = $dokument->dateiinhalt;
      $dok->dateiname = $dokument->dateiname;
      $dok->beschreibung = $dokument->beschreibung;
      $dok->bemerkung = $dokument->bemerkung;
      $doks[] = $dok;
    }

    return $doks;
  }

  public function getChangedInfo($columnValues, $changedDm, $changedFields): array
  {
    $datamodul = $this->datamodulColumn->datamodul;
    $fieldname = $this->datamodulColumn->fieldname;
    if (isset($columnValues[$datamodul][$fieldname])) {
      $newData = $columnValues[$datamodul][$fieldname];
      $changedDm->dokumente = $newData;
      $changedFields[] = 'dokumente';
    }

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

  private function _buildField($field, $datamodulWert, $privileg, $pPrintversion)
  {
    $canedit = ($privileg == 2 && !$pPrintversion && $field->canedit);

    $htmlField = $this->_getArea($datamodulWert, $pPrintversion, $field, $canedit);
    if ($canedit) {
      $htmlField .= '<div class="dokumente_buttons" style="position: absolute; right: 25px; top: 5px; font-size: 2em;">';

      $htmlField
        .= '<div style="resize: none;">'
          . '<button class="button_new_dokument no-button-style" title="Weiteres Dokument" type="button">'
            . '<i class="fa fa-plus hinterlegung-button-icon"></i>'
          . '</button>'
        . '</div>';

      $htmlField
        .= '<div style="resize: none;">'
          . '<button class="button_update_dokument no-button-style" title="Dokument ändern" type="button">'
            . '<i class="fa fa-edit hinterlegung-button-icon"></i>'
          . '</button>'
        . '</div>';

      $htmlField
        .= '<div style="resize: none;">'
          . '<button class="button_delete_dokument no-button-style" title="Dokument entfernen" type="button">'
            . '<i class="fa fa-trash hinterlegung-button-icon"></i>'
          . '</button>'
        . '</div>';

      $htmlField .= '</div>';
    }

    if (!$pPrintversion) {
      
      static $callInit = true;
      if ($callInit) {
        $htmlField .= $this->initJS();
        $callInit = false;
      }
    }

    return $htmlField;
  }

  private function initJS()
  {
    return '';
  }

  private function _getArea($datamodulWert, $pPrintversion, $field, $pEditable)
  {
    $fieldname = $this->_getFieldname($field);

    
    $dokumente = $datamodulWert->dokumente;
    $dokumente4JS = array_map(function($dokument) {
      $dok = new \stdClass();
      $dok->id = $dokument->id;
      $dok->beschreibung = $dokument->beschreibung;
      $dok->bemerkung = $dokument->bemerkung;
      $dok->dateiname = $dokument->dateiname;
      $dok->dateiinhalt = $dokument->dokument;

      return $dok;
    }, $dokumente);

    $tablespaceproperties = ($pPrintversion) ? 'cellpadding="2" cellspacing="2"' : '';

    $readonly = (!$pEditable) ? 'read_only' : '';
    $str = '<div id="' . $fieldname . '" class="recht_' . $field->privileg_key . ' ' . $fieldname . ' ' . $readonly . ' form-control" style="min-height: 177px; overflow: auto;">' . "\n"
              . '<table class="mnlisttable" border="1" ' . $tablespaceproperties . ' rules="all" style="font-size:12px; width: 95%;">' . "\n"
                . '<thead></thead>' . "\n"
                . '<tbody>' . "\n";
    $str .= '</tbody>' . "\n"
           . '</table>' . "\n"
           . '<input id="' . $fieldname . '_hidden" class="mnlist_hidden" name="' . $fieldname . '_hidden" type="hidden" value="' . \JsonOperations::encodeBase64($dokumente4JS) . '">' . "\n"
        . '</div>' . "\n";

    return $str;
  }
}