<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (26.03.19)
* *
*
*  CREATED BY MODULESGARDEN       ->       http://modulesgarden.com
*  CONTACT                        ->       contact@modulesgarden.com
*
*
* This software is furnished under a license and may be used and copied
* only  in  accordance  with  the  terms  of such  license and with the
* inclusion of the above copyright notice.  This software  or any other
* copies thereof may not be provided or otherwise made available to any
* other person.  No title to and  ownership of the  software is  hereby
* transferred.
*
*
* ******************************************************************** */

namespace ModulesGarden\Servers\ProxmoxVps\App\UI\IpAddress\Pages;

use ModulesGarden\ProxmoxAddon\App\Models\VmIpAddress;
use ModulesGarden\Servers\ProxmoxVps\App\UI\IpAddress\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\IpAddress\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataProviders\Providers\QueryDataProvider;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataTable;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\isAdmin;

class IpAddressDataTable extends DataTable implements ClientArea, AdminArea
{

    protected $id = 'ipAddressDataTable';
    protected $title = 'ipAddressDataTable';
    protected $searchable = false;
    protected $tableLength = "100";

    public function initContent()
    {
        $this->addClass('lu-text-left');

        if (isAdmin())
        {
            $this->addButton(new CreateButton());
            $this->addActionButton(new DeleteButton());
        }
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('ip')))
            ->addColumn((new Column('mac_address')))
            ->addColumn((new Column('subnet_mask')))
            ->addColumn((new Column('gateway')));
    }

    public function replaceFieldMac_address($key, $row)
    {
        return $row->{$key} ? $row->{$key} : '-';
    }

    public function replaceFieldSubnet_mask($key, $row)
    {
        return $row->{$key} ? $row->{$key} : '-';
    }

    public function replaceFieldGateway($key, $row)
    {
        return $row->{$key} ? $row->{$key} : '-';
    }

    protected function loadData()
    {
        $query    = VmIpAddress::select("id", "ip", "mac_address", "subnet_mask", "gateway")
            ->ofHostingId($this->getWhmcsParamByKey('serviceid'))
            ->getQuery();
        $dataProv = new QueryDataProvider();
        $dataProv->setDefaultSorting("ip", 'ASC');
        $dataProv->setData($query);
        $this->setDataProvider($dataProv);
    }

    public function isViewFooter()
    {
        return false;
    }

    public function isViewTopBody()
    {
        return isAdmin();
    }

}