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

use Kufer\KuferTools\Utils\Hinterlegungen\HinterlegungJsHelper;


class HtmlFreiWaehlbaresEingabefeld
{
  
  public function getBezeichnungLabel($pFreiWaehlbaresEingabefeld, $pFor)
  {
    $bezeichnung = $pFreiWaehlbaresEingabefeld->bezeichnung;

    return '<label for="' . CleanOperations::cleanStringForHtml($pFor) . '">' . CleanOperations::cleanStringForHtml($bezeichnung) . '</label>';
  }

  
  public function getInputFeldReadOnly($pFreiWaehlbaresEingabefeld, $pId = '', $pClass = '', $pHtmlStyle = null)
  {
    if (is_null($pHtmlStyle)) {
      $pHtmlStyle = new HtmlStyle();
    }

    $typ = StringOperations::substr($pFreiWaehlbaresEingabefeld->typ, 0, 1);
    switch ($typ) {
      case 'C': 
        return $this->_getInputAsCheckbox($pFreiWaehlbaresEingabefeld, '', $pId, $pClass, true, $pHtmlStyle);
      case 'D': 
      case 'T': 
      case 'Z': 
      default:
        return $this->_getValueAsSpan($pFreiWaehlbaresEingabefeld, $pId, $pClass);
    }
  }

  
  public function getInputFeld($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly = false, $pHtmlStyle = null, $dialogBox = false)
  {
    $typ = StringOperations::substr($pFreiWaehlbaresEingabefeld->typ, 0, 1);
    switch ($typ) {
      case 'C': 
        return $this->_getInputAsCheckbox($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly, $pHtmlStyle);
      default:
      case 'T': 
        return $this->_getInput4Text($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly, $pHtmlStyle, $dialogBox);
      case 'D': 
        return $this->_getInput4Date($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly, $pHtmlStyle);
      case 'Z': 
        return $this->_getInput4Numbers($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly, $pHtmlStyle);
    }
  }

  
  private function _getValueAsSpan($pFreiWaehlbaresEingabefeld, $pId, $pClass)
  {
    $value = $pFreiWaehlbaresEingabefeld->wert;

    return '<span id="' . CleanOperations::cleanStringForHtml($pId) . '" class="' . CleanOperations::cleanStringForHtml($pClass) . '">' . CleanOperations::cleanStringForHtml($value) . '</span>';
  }

  
  private function _getInputAsCheckbox($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly = false, $pHtmlStyle = null)
  {
    $value = 'W';
    $typ = 'checkbox';
    $checked = $pFreiWaehlbaresEingabefeld->wert == 'W';
    $class = $pClass . ' ' . $this->getClass4Checkbox();
    $style = '';
    if (!is_null($pHtmlStyle)) {
      unset($pHtmlStyle->width);
      $style = 'style="' . CleanOperations::cleanStringForHtml($pHtmlStyle->getStyle()) . '"';
    }

    return HTMLOperations::CreateInputTag($pName, $typ, $pId, $class, $value, $style, $checked, $pReadOnly);
  }

  protected function getClass4Checkbox()
  {
    return 'freifeld_checkbox';
  }

  protected function getFormControlClass()
  {
    return '';
  }

  
  private function _getInput4Numbers($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly = false, $pHtmlStyle = null)
  {
    $value = $pFreiWaehlbaresEingabefeld->wert;
    $typ = 'text';
    $class = $pClass . ' freifeld_zahl ' . $this->getFormControlClass();

    
    if ($pFreiWaehlbaresEingabefeld->typ[1] === 'W') {
      $class .= ' val_float';
    } else {
      $class .= ' val_int';
    }
    $style = '';
    if (!is_null($pHtmlStyle)) {
      $style = 'style="' . CleanOperations::cleanStringForHtml($pHtmlStyle->getStyle()) . '"';
    }
    $maxLength = 15;

    return HTMLOperations::CreateInputTag($pName, $typ, $pId, $class, $value, $style, '', $pReadOnly, $maxLength);
  }

  
  private function _getInput4Date($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly = false, $pHtmlStyle = null)
  {
    $value = $pFreiWaehlbaresEingabefeld->wert;
    $typ = 'text';
    $class = $pClass . ' datepicker date val_date ' . $this->getFormControlClass();
    $style = '';
    if (!is_null($pHtmlStyle)) {
      $style = 'style="' . CleanOperations::cleanStringForHtml($pHtmlStyle->getStyle()) . '"';
    }
    $maxLength = 10;

    return HTMLOperations::CreateInputTag($pName, $typ, $pId, $class, $value, $style, '', $pReadOnly, $maxLength);
  }

  
  protected function _getInput4Text($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly = false, $pHtmlStyle = null, $dialogBox = false)
  {
    $value = $pFreiWaehlbaresEingabefeld->wert;
    $typ = 'text';
    $class = $pClass . ' freifeld_text';
    $maxLength = $this->getMaxlengthFromTypText($pFreiWaehlbaresEingabefeld->typ);

    $button = '';
    if (!$pReadOnly) {
      $button = $this->_getHinterlegungsButton($pFreiWaehlbaresEingabefeld, $pId, $dialogBox);
    }

    $posStyleAttr = $inputStyleAttr = '';
    if (!is_null($pHtmlStyle)) {
      
      $positionStyle = new HtmlStyle();
      $positionStyle->position = $pHtmlStyle->position;
      unset($pHtmlStyle->position);
      $positionStyle->left = $pHtmlStyle->left;
      unset($pHtmlStyle->left);
      $positionStyle->top = $pHtmlStyle->top;
      unset($pHtmlStyle->top);
      $posStyleAttr = 'style="' . CleanOperations::cleanStringForHtml($positionStyle->getStyle()) . '"';

      
      if (trim($button) != '' && isset($pHtmlStyle->width)) {
        $width = (int)$pHtmlStyle->width;
        $pHtmlStyle->width = ($width - 23) . 'px';
      }
      $inputStyleAttr = 'style="' . CleanOperations::cleanStringForHtml($pHtmlStyle->getStyle()) . '"';
    }

    return '<span ' . $posStyleAttr . '>' . HTMLOperations::CreateInputTag($pName, $typ, $pId, $class, $value, $inputStyleAttr, '', $pReadOnly, $maxLength) . $button . '</span>';
    
  }

  
  protected function _getHinterlegungsButton($pFreiWaehlbaresEingabefeld, $pId, $dialogBox = false)
  {
    if (is_array($pFreiWaehlbaresEingabefeld->hinterlegung) && count($pFreiWaehlbaresEingabefeld->hinterlegung) > 0) {
      $hinterlegung4Js = array_map(function($hinterlegung) use ($pFreiWaehlbaresEingabefeld) {
        $std = new stdClass();
        $std->value = $hinterlegung->text;
        $std->{$pFreiWaehlbaresEingabefeld->bezeichnung} = $hinterlegung->text;

        return $std;
      }, $pFreiWaehlbaresEingabefeld->hinterlegung);
      $hlJson = JsonOperations::encode($hinterlegung4Js);

      $buttonId = 'hlButton_' . $pId;

      
      
      
      
      
      $script = '<script language="javascript" type="text/javascript">
                 $(function(){
                   $(document).off("click", "#' . CleanOperations::cleanStringForJs($buttonId) . '");
                   $(document).on("click","#' . CleanOperations::cleanStringForJs($buttonId) . '",function(){                   
                     KuferTools.ShowHinterlegungsDialog(' . $hlJson . ', "' . CleanOperations::cleanStringForJs($pId) . '");
                  });
                 });
                 </script>';

      return HTMLOperations::CreateImageTag('layout/button_hinterlegung.gif', 'Hinterlegung für das Eingabefeld öffnen', $buttonId, 'freifeldHlButton') . $script;
    }

    return '';
  }

  protected function getMaxlengthFromTypText($typ)
  {
    $maxLength = (int)StringOperations::substr($typ, 1);

    if ($maxLength <= 0) {
      
      
      return '';
    } else {
      return $maxLength;
    }
  }
}

class HtmlFreiWaehlbaresEingabefeldResponsive extends HtmlFreiWaehlbaresEingabefeld
{
  
  protected function _getInput4Text($pFreiWaehlbaresEingabefeld, $pName, $pId, $pClass, $pReadOnly = false, $pHtmlStyle = null, $dialogBox = false)
  {
    $value = $pFreiWaehlbaresEingabefeld->wert;
    $typ = 'text';
    $class = $pClass . ' freifeld_text form-control';
    $maxLength = $this->getMaxlengthFromTypText($pFreiWaehlbaresEingabefeld->typ);

    $button = '';
    if (!$pReadOnly) {
      $button = $this->_getHinterlegungsButton($pFreiWaehlbaresEingabefeld, $pId, $dialogBox);
    }

    $posStyleAttr = $inputStyleAttr = '';
    if (!is_null($pHtmlStyle)) {
      
      $positionStyle = new HtmlStyle();
      $positionStyle->position = $pHtmlStyle->position;
      unset($pHtmlStyle->position);
      $positionStyle->left = $pHtmlStyle->left;
      unset($pHtmlStyle->left);
      $positionStyle->top = $pHtmlStyle->top;
      unset($pHtmlStyle->top);
      $posStyleAttr = 'style="' . $positionStyle->getStyle() . '"';

      
      if (trim($button) != '' && isset($pHtmlStyle->width)) {
        $width = (int)$pHtmlStyle->width;
        $pHtmlStyle->width = ($width - 23) . 'px';
      }
      $inputStyleAttr = 'style="' . $pHtmlStyle->getStyle() . '"';
    }

    $inputgroupOpeningTag = '';
    $inputgroupClosingTag = '';
    if (trim($button) != '') {
      $inputgroupOpeningTag = '<div class="input-group">';
      $inputgroupClosingTag = '</div>';
    }

    return '<span ' . $posStyleAttr . '>' . $inputgroupOpeningTag . HTMLOperations::CreateInputTag($pName, $typ, $pId, $class, $value, $inputStyleAttr, '', $pReadOnly, $maxLength) . $button . $inputgroupClosingTag . '</span>';
    
  }

  
  protected function _getHinterlegungsButton($pFreiWaehlbaresEingabefeld, $pId, $dialogBox = false)
  {
    if (is_array($pFreiWaehlbaresEingabefeld->hinterlegung) && count($pFreiWaehlbaresEingabefeld->hinterlegung) > 0) {
      if ($dialogBox) {
        $hinterlegung4Js = array_map(function($hinterlegung) use ($pFreiWaehlbaresEingabefeld) {
          $std = new stdClass();
          $std->value = $hinterlegung->text;
          $std->{$pFreiWaehlbaresEingabefeld->bezeichnung} = $hinterlegung->text;

          return $std;
        }, $pFreiWaehlbaresEingabefeld->hinterlegung);
        $hlJson = JsonOperations::encode($hinterlegung4Js);
        $targetIdAry = array(CleanOperations::cleanStringForJs($pId));
        $buttonId = 'hlButton_' . $pId;
        $titel = '';
        $showHeader = false;

        $script = HinterlegungJsHelper::getHlDialogSingleColumnScript($hlJson, $targetIdAry, $buttonId, $titel, $showHeader);

        $result = '<div class="input-group-btn">'
                  . '<button type="button" id="' . $buttonId . '" class="btn btn-secondary dropdown-toggle"
                      title="Hinterlegung" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                        <i class="fa fa-hand-o-down" aria-hidden="true"></i>
                    </button>';
        $result .= $script;
        $result .= '</div>';

        return $result;
      }
      $result = '<div class="input-group-btn"><button type="button" class="btn btn-secondary dropdown-toggle"
                  title="Hinterlegung" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                  <i class="fa fa-hand-o-down" aria-hidden="true"></i>
                  </button>
                  <div class="dropdown-menu dropdown-menu-right">
                  <div class="dropdown-item"><strong>' . $pFreiWaehlbaresEingabefeld->bezeichnung . '</strong></div>
                  <div role="separator" class="dropdown-divider"></div>';

      foreach ($pFreiWaehlbaresEingabefeld->hinterlegung as $hinterlegung) {
        $result .= '<a class="dropdown-item hinterlegung_freifeld" href="#/">' . $hinterlegung->text . '</a>';
      }
      $result .= '</div></div>';

      return $result;
    }

    return '';
  }

  protected function getClass4Checkbox()
  {
    return 'checkbox freifeld_checkbox form-control formular_checkbox ';
  }

  protected function getFormControlClass()
  {
    return 'form-control';
  }
}