<?php

class ModuleMovie extends Module
{
  function __construct($System)
  {
    parent::__construct($System);
    $this->Name = 'Movie';
    $this->Version = '1.0';
    $this->Creator = 'Chronos';
    $this->License = 'GNU/GPL';
    $this->Description = 'List of dance movies';
    $this->Dependencies = array();
    $this->RSSChannels = array();
  }

  function Start(): void
  {
    $this->System->RegisterPage(['filmy'], 'PageMovieList');
    Core::Cast($this->System)->RegisterMenuItem('/filmy', 'Filmy');
  }
}

class PageMovieList extends Page
{
  function __construct($System)
  {
    parent::__construct($System);
    $this->Title = 'Filmy';
    $this->Description = 'Taneční filmy';
  }

  function Show(): string
  {
    $Output = '';
    if (array_key_exists('lvm', $_GET) and ($_GET['lvm'] == 'seznam'))
      $this->RawPage = true;
      else $Output .= '<div class="title">Filmy</div>';

    $Filter = new Filter();
    $Filter->Items = array(
      array('Name' => 'year', 'Type' => 'Integer', 'DbName' => 'Year', 'Title' => 'Rok'),
      array('Name' => 'namecz', 'Type' => 'String', 'DbName' => 'NameCz', 'Title' => 'Český název'),
      array('Name' => 'nameen', 'Type' => 'String', 'DbName' => 'NameEn', 'Title' => 'Anglický název'),
    );
    $Output .= $Filter->GetOutput($this->System->Link('/filmy/'));
    $Where = $Filter->GetWhere($this->Database);

    $DbResult = $this->Database->query('SELECT COUNT(*) FROM Movie WHERE '.$Where);
    $DbRow = $DbResult->fetch_row();
    $PageList = GetPageList($DbRow[0]);
    $Output .= $PageList['Output'];
    $TableColumns = array(
      array('Name' => 'Year', 'Title' => 'Rok'),
      array('Name' => 'NameCz', 'Title' => 'Český název'),
      array('Name' => 'NameEn', 'Title' => 'Anglický název'),
      array('Name' => 'Imdb', 'Title' => 'IMDB'),
      array('Name' => 'Csfd', 'Title' => 'ČSFD'),
    );
    $Order = GetOrderTableHeader($TableColumns, 'Year', 1);

    $Output .= '<div id="list_content">';
    $Output .= '<table class="WideTable">';
    $Output .= $Order['Output'];
    $Output .= '</tr>';
    $DbResult = $this->Database->select('Movie', '*', $Where.$Order['SQL'].$PageList['SQLLimit']);
    while ($Movie = $DbResult->fetch_assoc())
    {
      $Output .= '<tr>'.
        '<td>'.$Movie['Year'].'</td>'.
        '<td>'.$Movie['NameCz'].'</td>'.
        '<td>'.$Movie['NameEn'].'</td>'.
        '<td><a href="'.$Movie['Imdb'].'">Otevřít</a></td>'.
        '<td><a href="'.$Movie['Csfd'].'">Otevřít</a></td>'.
        '</tr>';
    }
    $Output .= '</table>';
    $Output .= '</div>';
    $Output .= $PageList['Output'];

    return $Output;
  }
}
