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


namespace Kufer\Web\KuferTools\Utils\Hinterlegungen;

class HinterlegungJsHelper
{
  
  private static function getScriptContainerForHlDialog($pHlButtonId, $pScriptPartDialog)
  {
    $script = '<script language="javascript" type="text/javascript">' . "\n"
              . '  docReady(function(){' . "\n"
              . '    $("#' . $pHlButtonId . '").off("click");' . "\n"
              . '    $("#' . $pHlButtonId . '").on("click",function(){' . "\n";

    $script .= $pScriptPartDialog . "\n";

    $script
              .= '    });' . "\n"
              . '  });' . "\n"
              . '</script>' . "\n";

    return $script;
  }

  
  public static function getHlDialogSingleColumnScript($pHlJson, $pTargetIdAry, $pHlButtonId, $pTitel = '', $pShowHeader = true, $pWidth = 'auto', $pHeight = 'auto')
  {
    $triggerChange = 'true';
    $showHeader = $pShowHeader ? 'true' : 'false';
    $target = $pTargetIdAry[0];
    $scriptPartDialog = 'KuferTools.DisplayHinterlegungsDialogAsList(' . $pHlJson . ', "' . $target . '", "' . $pTitel . '", "' . $pWidth . '", "' . $pHeight . '", ' . $triggerChange . ', ' . $showHeader . ');';

    return self::getScriptContainerForHlDialog($pHlButtonId, $scriptPartDialog);
  }

  
  public static function getHlDialogMultiColumnScript($pHlJson, $pTargetIdAry, $pHlButtonId, $pTitel = '', $pZusatzButton = 'undefined', $pZusatzButtonBeschriftung = '')
  {
    $triggerChange = 'true';
    
    $targets = \JsonOperations::encode($pTargetIdAry);
    $scriptPartDialog = 'KuferTools.DisplayHinterlegungsDialogMultiCol(' . $pHlJson . ', ' . $targets . ', "' . $pTitel . '", ' . $triggerChange . ', ' . $pZusatzButton . ', "' . $pZusatzButtonBeschriftung . '");';

    return self::getScriptContainerForHlDialog($pHlButtonId, $scriptPartDialog);
  }

  
  public static function getHlDialogMultiColumnFilteredScript($pHlJson, $pTargetIdAry, $pTargetIdFilterAry, $pHlButtonId, $pTitel = '', $pZusatzButton = 'undefined', $pZusatzButtonBeschriftung = '')
  {
    $triggerChange = 'true';
    
    $targets = \JsonOperations::encode($pTargetIdAry);
    $targetsFilter = \JsonOperations::encode($pTargetIdFilterAry);
    $scriptPartDialog = 'KuferTools.DisplayHinterlegungsDialogMultiColFiltered(' . $pHlJson . ', ' . $targets . ', ' . $targetsFilter . ', "' . $pTitel . '", ' . $triggerChange . ', ' . $pZusatzButton . ', "' . $pZusatzButtonBeschriftung . '");';

    return self::getScriptContainerForHlDialog($pHlButtonId, $scriptPartDialog);
  }

  
  public static function getHinterlegungSingleCol4JS($pHlObj)
  {
    $rowCount = $pHlObj->GetRowCount();
    $columnCount = $pHlObj->GetColumnCount();
    $hinterlegung4Js = array();
    for ($row = 0; $row < $rowCount; $row++) {
      for ($col = 0; $col < $columnCount; $col++) {
        $colTitle = $pHlObj->GetColumnTitle($col);
        $cellValue = $pHlObj->GetValueAt($row, $col);
        if ($cellValue === '') {
          $cellValue = '&nbsp;';
        }
        $std = new \stdClass();
        $std->value = $cellValue;
        $std->{$colTitle} = $cellValue;
        $hinterlegung4Js[] = $std;
      }
    }

    return \JsonOperations::encode($hinterlegung4Js);
  }

  
  public static function getHinterlegungMultiCol4JS($pHlObj)
  {
    $rowCount = $pHlObj->GetRowCount();
    $columnCount = $pHlObj->GetColumnCount();
    $columns = array();
    $columnWidth = array();
    $rows = array();

    for ($row = 0; $row < $rowCount; $row++) {
      $cellValues = array();
      for ($col = 0; $col < $columnCount; $col++) {
        $columns[$col] = $pHlObj->GetColumnTitle($col);
        
        
        $width = $pHlObj->GetColumnWidth($col);
        if (!empty($width)) {
          $columnWidth[$col] = $width;
        }
        $cellValues[] = $pHlObj->GetValueAt($row, $col);
      }
      $rows[] = $cellValues;
    }
    $hinterlegungen4Js = new \stdClass();
    $hinterlegungen4Js->columns = $columns;

    if (count($columnWidth) > 0) {
      $hinterlegungen4Js->colwidth = $columnWidth;
    }
    $hinterlegungen4Js->rows = $rows;
    $hinterlegungen4Js->idColumnsIndices = $pHlObj->GetIdColumnIndices();
    $hinterlegungen4Js->columnsToHide = $pHlObj->GetColumnsToHide();

    return \JsonOperations::encode($hinterlegungen4Js);
  }

  
  public static function getHinterlegungMultiColFiltered4JS($pHlObj)
  {
    $rowCount = $pHlObj->GetRowCount();
    $columnCount = $pHlObj->GetColumnCount();
    $columns = array();
    $columnWidth = array();
    $rows = array();
    $filterValues = array();

    for ($row = 0; $row < $rowCount; $row++) {
      $cellValues = array();
      for ($col = 0; $col < $columnCount; $col++) {
        $columns[$col] = $pHlObj->GetColumnTitle($col);
        
        
        $width = $pHlObj->GetColumnWidth($col);
        if (!empty($width)) {
          $columnWidth[$col] = $width;
        }
        $cellValues[] = $pHlObj->GetValueAt($row, $col);
      }
      $rows[] = $cellValues;
      $filterValues[] = $pHlObj->GetFilterValues($row);
    }
    $hinterlegungen4Js = new \stdClass();
    $hinterlegungen4Js->columns = $columns;

    if (count($columnWidth) > 0) {
      $hinterlegungen4Js->colwidth = $columnWidth;
    }
    $hinterlegungen4Js->rows = $rows;
    $hinterlegungen4Js->filterValues = $filterValues;
    $hinterlegungen4Js->idColumnsIndices = $pHlObj->GetIdColumnIndices();
    $hinterlegungen4Js->columnsToHide = $pHlObj->GetColumnsToHide();

    return \JsonOperations::encode($hinterlegungen4Js);
  }
}