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

namespace Kufer\Web\KuferTools\Veranstalter\KursVorschlag\HandleKursortDataTable;

use Kufer\Web\KuferTools\Utils\DatamodulHelper\Kursorte\KursorteHelper;
use Kufer\Web\KuferTools\Utils\FilterSearch\KursorteSearchFilter;

class HandleKursortDataTableUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  private $kursorthelper;

  public function __construct($request)
  {
    parent::__construct($request, new HandleKursortDataTableResponse());
    $this->toolSettings->sorttn = 'mktext';
    $this->kursorthelper = KursorteHelper::createVeranstalterKursvorschlagKursorte($this->getUser(), $this->toolSettings);
    $searchAPI = new KursorteSearchFilter();
    $searchAPI->setSearchMapToSession($searchAPI->getSearchMapFromRequest(), 'tool' . $this->getToolId());
  }

  public static function getToolSectionId()
  {
    return 738;
  }

  public static function getToolId()
  {
    return \ToolsAPI::TOOL_KURSVORSCHLAG_VA;
  }

  public function action()
  {
    $json = $this->getJSON4TnDataTable();

    return $this->getResponse($json);
  }

  private function getExtraColumn()
  {
    $columns = $this->getColumns($this->toolSettings);
    $extraColumn = new \Column();
    $extraColumn->fieldname = 'id';
    $extraColumn->datamodul = 'kursort';
    $extraColumn->title = '';
    $extraColumn->width = '20';
    array_unshift($columns, $extraColumn);

    return $columns;
  }

  
  private function getJSON4TnDataTable()
  {
    
    $kursorteProSeite = (isset($this->toolSettings->kursorteproseite) && $this->toolSettings->kursorteproseite > 0) ? $this->toolSettings->kursorteproseite : 6;
    $iDisplayStart = \RequestOperations::getVarValue('iDisplayStart', 0);
    $currentPage = $iDisplayStart / $kursorteProSeite + 1;

    
    $alleKursortIds = $this->kursorthelper->getIdsMitFilter($this->getToolId());
    
    $this->setTotalData($alleKursortIds);
    $tnAnzahl = count($alleKursortIds);

    $_REQUEST['currentIndex'] = $currentPage;
    $tnNummernDerSeite = \ToolsAPI::getDataForCurrentPage($alleKursortIds, $kursorteProSeite);

    $data = $this->kursorthelper->getDatamoduleByIds($tnNummernDerSeite);

    $columns = $this->getExtraColumn();
    
    $aaData = $this->datamodulToDataTable($data, $columns);

    
    $tnAnzahlSeite = count($data);
    for ($i = 0; $i < $tnAnzahlSeite; $i++) {
      $tmpKursort = new \stdClass();
      $tmpKursort->mktext = $data[$i]->mktext;
      $tmpKursort->bez = $data[$i]->bezeichnung;
      $tmpKursort->strasse = $data[$i]->strasse;
      $tmpKursort->plz = $data[$i]->getVarValue('nurplz');
      $tmpKursort->ort = $data[$i]->getVarValue('nurort');
      $tmpKursort->gemeinde = $data[$i]->gemeinde;
      $tmpKursort->stadtteil = $data[$i]->stadtteil;
      $tmpKursort->bezirk = $data[$i]->bezirk;
      $tmpKursort->bundesland = $data[$i]->bundesland;
      $tmpKursort->internet = $data[$i]->internet;

      $base64Kursort = \JsonOperations::encodeBase64($tmpKursort);

      
      $aaData[$i][0] = '<button type="button" class="btn btn-success choose_kursort" title="Buchen" data-kursort="' . $base64Kursort . '" >'
       . '<i class="fa fa-download" aria-hidden="true"></i>'
              . '</button>';
    }

    
    $output = array(
      'sEcho' => intval(\RequestOperations::getVarValue('sEcho')),
      'iTotalRecords' => $tnAnzahl,
      'iTotalDisplayRecords' => $tnAnzahl,
      'aaData' => $aaData
    );

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

  
  private function datamodulToDataTable($pDatamodulAry, $pColumns)
  {
    $wssFactory = new \Kufer\Web\KuferTools\Utils\WssField\WssFactory();

    $data4Datatable = array();
    foreach ($pDatamodulAry as $datamodul) {
      $row = array();
      foreach ($pColumns as $column) {
        $wss = $wssFactory->getWss($this->toToolSection(), $this->toolSettings, $column);
        $row[] = $wss->getHtmlEncodedValue($datamodul, false);
      }
      $data4Datatable[] = $row;
    }

    return $data4Datatable;
  }

  
  protected function setTotalData($pData)
  {
    \ToolsAPI::setTotalData($pData, $this->getToolId());
  }

  protected function getResponse($json)
  {
    $this->response->json = $json;

    return $this->response;
  }
}

class HandleKursortDataTableRequest
{
  public $mehrfachAnm;
  public $getTn;
  public $tnr;
}

class HandleKursortDataTableResponse extends \Kufer\Web\KuferTools\Basis\UseCase\ToolResponse
{
  public $json;
}