<?php

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

  function Start(): void
  {
    $this->System->RegisterPage(['tance'], 'PageDanceList');
    $this->System->RegisterPage(['tanec'], 'PageDance');
    $this->System->RegisterPage(['figura'], 'PageDanceFigure');
    $this->System->RegisterPage(['figury'], 'PageDanceFigureList');
    Core::Cast($this->System)->RegisterMenuItem('/tance', 'Tance');
    Core::Cast($this->System)->RegisterMenuItem('/figury', 'Figury');
  }
}

class PageDance extends Page
{
  function __construct($System)
  {
    parent::__construct($System);
    $this->Title = 'Tanec';
  }

  function Show(): string
  {
    $Output = '';
    if (count($this->System->PathItems) > 1)
    {
      $id = $this->System->PathItems[1] * 1;
    } else return 'Položka nenalezena';
    $Output .= '<div class="title">Tanec</div>';
    $DbResult = $this->Database->select('Dance', '*, (SELECT DanceGroup.Name FROM DanceGroup WHERE DanceGroup.Id=Dance.Group) AS DanceGroupName', 'Id='.$id);
    if ($DbResult->num_rows > 0)
    {
      $DanceItem = $DbResult->fetch_assoc();
      $Output .= '<table class="ItemTable">'.
        '<tr><th>Jméno</th><td>'.$DanceItem['Name'].'</td></tr>'.
        '<tr><th>Skupina</th><td>'.$DanceItem['DanceGroupName'].'</td></tr>';
      $Output .= '</table>';
    } else $Output .= 'Položka nenalezena';

    $Output .= '<br/><div class="title">Taneční figury</div>';
    $Output .= '<table class="WideTable">';
    $Output .= '<tr><th>Český název</th><th>Anglický název</th><th>Detail</th></tr>';
    $DbResult = $this->Database->select('DanceFigure', '*', '(`Dance`='.$id.')');
    while ($DanceFigure = $DbResult->fetch_assoc())
    {
      $Output .= '<tr>'.
        '<td>'.$DanceFigure['NameCz'].'</td>'.
        '<td>'.$DanceFigure['NameEn'].'</td>'.
        '<td><a href="'.$this->System->Link('/figura/'.$DanceFigure['Id'].'/').'">Ukázat</a></td>';
        '</tr>';
    }
    $Output .= '</table>';

    $Output .= '<br/><div class="title">Výuková videa</div>';
    $Output .= '<table class="WideTable">';
    $Output .= '<tr><th>Název</th><th>Skupina</th><th>Odkaz</th></tr>';
    $DbResult = $this->Database->select('Resource', '*, (SELECT Name FROM ResourceGroup WHERE ResourceGroup.Id=Resource.Group) AS GroupName', '(`Dance`='.$id.')');
    while ($DbRow = $DbResult->fetch_assoc())
    {
      $Output .= '<tr>'.
        '<td>'.$DbRow['Name'].'</td>'.
        '<td>'.$DbRow['GroupName'].'</td>'.
        '<td><a href="'.$DbRow['URL'].'">'.$DbRow['URL'].'</a></td>'.
        '</tr>';
    }
    $Output .= '</table>';

      /*
          $DbResult2 = $this->Database->select('ResourceGroup', '*');
    while ($ResourceGroup = $DbResult2->fetch_assoc())
    {
      $Output .= '<th>'.$ResourceGroup['Name'].'</th>';
    }

      $DbResult2 = $this->Database->select('ResourceGroup', '*');
      while ($ResourceGroup = $DbResult2->fetch_assoc())
      {
        $Output .= '<td>';
        while ($Resource = $DbResult3->fetch_assoc())
        {
          $Output .= '<a href="'.$Resource['URL'].'">'.$Resource['Name'].'</a> ';
        }
        $Output .= '</td>';
      }*/

    return $Output;
  }
}

class PageDanceList extends Page
{
  function __construct($System)
  {
    parent::__construct($System);
    $this->Title = 'Tance';
  }

  function Show(): string
  {
    $Output = '<div class="title">Tance</div>';
    $Output .= '<table class="WideTable">';
    $Output .= '<tr><th>Název</th><th>Skupina</th><th>Detail</th></tr>';
    $DbResult = $this->Database->select('Dance', '*, (SELECT Name FROM DanceGroup WHERE DanceGroup.Id=Dance.Group) AS GroupName', '1 ORDER BY `Name`');
    while ($Dance = $DbResult->fetch_assoc())
    {
      $Output .= '<tr>'.
        '<td>'.$Dance['Name'].'</td>'.
        '<td>'.$Dance['GroupName'].'</td>'.
        '<td><a href="'.$this->System->Link('/tanec/'.$Dance['Id'].'/').'">Ukázat</a></td>';
      $Output .= '</tr>';
    }
    $Output .= '</table>';

    return $Output;
  }
}

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

  function Show(): string
  {
    $Output = '';
    if (count($this->System->PathItems) > 1)
    {
      $id = $this->System->PathItems[1] * 1;
    } else return 'Položka nenalezena';
    $Output .= '<div class="title">Taneční figura</div>';
    $DbResult = $this->Database->select('DanceFigure', '*, (SELECT Dance.Name FROM Dance WHERE Dance.Id=DanceFigure.Dance) AS DanceName', 'Id='.$id);
    if ($DbResult->num_rows > 0)
    {
      $DbRow = $DbResult->fetch_assoc();
      $Output .= '<table class="ItemTable">'.
        '<tr><th>České jméno</th><td>'.$DbRow['NameCz'].'</td></tr>'.
        '<tr><th>Anglické jméno</th><td>'.$DbRow['NameEn'].'</td></tr>'.
        '<tr><th>Tanec</th><td>'.$DbRow['DanceName'].'</td></tr>';
      $Output .= '</table>';
    } else $Output .= 'Položka nenalezena';

    /*$Output .= '<br/><div class="title">Videa</div>';
    $Output .= '<table class="WideTable">';
    $Output .= '<tr><th>Skupina videí</th><th></th><th>Detail</th></tr>';
    $DbResult = $this->Database->select('DanceFigure', '*', '(`Dance`='.$id.')');
    while ($DanceFigure = $DbResult->fetch_assoc())
    {
      $Output .= '<tr>'.
        '<td>'.$DanceFigure['NameCz'].'</td>'.
        '<td>'.$DanceFigure['NameEn'].'</td>'.
        '<td><a href="'.$this->System->Link('/tance/figura/'.$DanceFigure['Id'].'/').'">Ukázat</a></td>';
        '</tr>';
    }
    $Output .= '</table>';
    */
      /*
          $DbResult2 = $this->Database->select('ResourceGroup', '*');
    while ($ResourceGroup = $DbResult2->fetch_assoc())
    {
      $Output .= '<th>'.$ResourceGroup['Name'].'</th>';
    }

      $DbResult2 = $this->Database->select('ResourceGroup', '*');
      while ($ResourceGroup = $DbResult2->fetch_assoc())
      {
        $Output .= '<td>';
        while ($Resource = $DbResult3->fetch_assoc())
        {
          $Output .= '<a href="'.$Resource['URL'].'">'.$Resource['Name'].'</a> ';
        }
        $Output .= '</td>';
      }*/

    return $Output;
  }
}

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

  function Show(): string
  {
    $Output = '<div class="title">Taneční figury</div>';
    $Output .= '<table class="WideTable">';
    $Output .= '<tr><th>Název</th><th>Skupina</th><th>Tanec</th><th>Detail</th></tr>';
    $DbResult = $this->Database->select('DanceFigure', '*, (SELECT Dance.Name FROM Dance WHERE Dance.Id=DanceFigure.Dance) AS DanceName', '1 ORDER BY `NameCz`');
    while ($DbRow = $DbResult->fetch_assoc())
    {
      $Output .= '<tr>'.
        '<td>'.$DbRow['NameCz'].'</td>'.
        '<td>'.$DbRow['NameEn'].'</td>'.
        '<td>'.$DbRow['DanceName'].'</td>'.
        '<td><a href="'.$this->System->Link('/figura/'.$DbRow['Id'].'/').'">Ukázat</a></td>';
      $Output .= '</tr>';
    }
    $Output .= '</table>';

    return $Output;
  }
}
