<?php

class PageNetworkHostList extends Page
{
  function __construct(System $System)
  {
    parent::__construct($System);
    $this->Title = 'Registrované počítače';
    $this->ParentClass = 'PageNetwork';
  }

  function Show(): string
  {
    if (ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id'] == '') return $this->SystemMessage('Nepovolený přístup', 'Nemáte oprávnění pro tuto operaci');
    $Output = '<div align="center" style="font-size: small;"><table class="WideTable">';
    $Output .= '<tr><th>Jméno počítače</th><th>Místní adresa</th><th>Veřejná adresa</th><th>Fyzická adresa</th><th>Typ</th><th>Naposledy online</th></tr>';
    $DbResult = $this->Database->query('SELECT NetworkDevice.*, NetworkDeviceType.Name AS HostType FROM NetworkDevice '.
      'LEFT JOIN NetworkDeviceType ON NetworkDeviceType.Id = NetworkDevice.Type '.
      'WHERE NetworkDevice.Used = 1 AND NetworkDevice.Member = (SELECT Customer FROM UserCustomerRel WHERE User='.ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id'].') ORDER BY NetworkDevice.Name');
    while ($Device = $DbResult->fetch_assoc())
    {
      if ($Device['Online'] == 1) $Style = 'color: blue;'; else $Style = '';
      $Output .= '<tr><td colspan="4" style="text-align: left; font-weight: bold; '.
        $Style.'">'.$Device['Name'].'</td><td>'.$Device['HostType'].'</td><td style="text-align: right;">'.HumanDate($Device['LastOnline']).'</td></tr>';
      $DbResult2 = $this->Database->query('SELECT * FROM NetworkInterface WHERE Device = '.$Device['Id']);
      while ($Interface = $DbResult2->fetch_assoc())
      {
        if ($Interface['Online'] == 1) $Style = 'font-weight: bold; color: blue;'; else $Style = '';
        $InterfaceName = $Device['Name'];
        if ($Interface['Name'] != '') $InterfaceName .= '-'.$Interface['Name'];
        $Output .= '<tr><td style="text-align: left; '.$Style.'">&nbsp;&nbsp;'.
          $InterfaceName.'</td><td>'.NotBlank($Interface['LocalIP']).'</td><td>'.
          NotBlank($Interface['ExternalIP']).'</td><td>'.
          NotBlank($Interface['MAC']).'</td><td>&nbsp;</td><td>&nbsp;</td></tr>';
      }
    }
    $Output .= '</table></div>';
    return $Output;
  }
}
