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

namespace Kufer\Web\KuferTools\Basis\Anmeldung\HandleKursDataTable;

abstract class HandleKursDataTableUseCase extends \Kufer\Web\KuferTools\Basis\UseCase\ToolUseCase
{
  public function __construct($request)
  {
    parent::__construct($request, new HandleKursDataTableResponse());
    
    $searchAPI = new \KurssuchAPIwSearch(isset($this->toolSettings->teilnehmerSuche) && $this->toolSettings->teilnehmerSuche);
    $searchAPI->getSearchParamsFromReqToSession('tool' . $this->getToolId());
  }

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

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

  protected function getExtraColumns()
  {
    $columns = $this->getColumns($this->toolSettings);

    $extraColumn = new \Column();
    $extraColumn->fieldname = 'id';
    $extraColumn->datamodul = 'kurse';
    $extraColumn->title = '';
    $extraColumn->width = '30';
    array_unshift($columns, $extraColumn);

    return $columns;
  }

  abstract protected function getKursHelper(): \Kufer\Web\KuferTools\Utils\DatamodulHelper\Kurse\KursHelper;

  
  private function getJSON4KursDataTable($pAction)
  {
    $kurshelper = $this->getKursHelper();
    $alleKursnummern = $kurshelper->getIdsMitFilter($this->getToolId());
    $this->setTotalData($alleKursnummern);
    $kursanzahl = \count($alleKursnummern);
    $kurseProSeite = ($this->toolSettings->kurseproseite > 0) ? $this->toolSettings->kurseproseite : 20;
    $iDisplayStart = \RequestOperations::getVarValue('iDisplayStart', 0);
    $currentPage = $iDisplayStart / $kurseProSeite + 1;

    $_REQUEST['currentIndex'] = $currentPage;
    $kursnummernDerSeite = \ToolsAPI::getDataForCurrentPage($alleKursnummern, $kurseProSeite);
    
    $data = $kurshelper->getDatamoduleByIds($kursnummernDerSeite);

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

    $isUmelden = $pAction === 'ummelden';
    $isWeitermelden = $pAction === 'Weitermelden';
    $toolId = $this->getToolId();

    
    foreach ($aaData as &$row) {
      
      
      if ($isUmelden || $isWeitermelden) {
        $class = $isUmelden ? 'ummeldeKurs' : 'weitermeldeKurs';
        $row[0] = '<button type="button" id="' . \CleanOperations::cleanStringForHtml($row[0]) . '" class="btn btn-success ' . $class . '" title="Buchen" >'
                    . '<i class="fa fa-download" aria-hidden="true"></i>'
                  . '</button>';
      } else { 
        $row[0] = '<a href="index.php?tool_id=' . $toolId . '&knr=' . \CleanOperations::cleanStringForHtml($row[0]) . '">'
                . '<button type="button" class="btn btn-success" title="Buchen" >'
                  . '<i class="fa fa-download" aria-hidden="true"></i>'
                . '</button></a>';
      }
    } 

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

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

  
  private function datamodulToDataTable($pDatamodulAry, $pColumns)
  {
    return \Kufer\Web\KuferTools\Utils\Datatable\DatatableUtils::datamodulToDataTable(
      $this->toToolSection(),
      $this->toolSettings,
      $pDatamodulAry,
      $pColumns
    );
  }

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

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

    return $this->response;
  }
}

class HandleKursDataTableRequest
{
  public $action;
}

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