<?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\ProxmoxCloudVps\App\UI\VirtualNetwork\Pages;

use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualNetwork\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualNetwork\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualNetwork\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualNetwork\Buttons\InfoButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualNetwork\Buttons\MailtoSwitchButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualNetwork\Buttons\RestoreButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualNetwork\Buttons\UpdateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\Providers\QueryDataProvider;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataTable;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class VirtualNetworkDataTable extends DataTable implements ClientArea
{
    use ProductService;
    use ApiService;

    public function initContent()
    {
        $this->initIds('virtualNetworkDataTable');
        $this->addClass(' lu-text-left');
        $createButton    = new CreateButton();
        $resourceManager = new ResourceManager();
        $createButton->addClass("pmCreateVirtualNetworkButton");
        if (!$resourceManager->virtualNetworks()->hasFreeTotal())
        {
            $createButton->addClass("disabled");
        }
        //Create
        $this->addButton($createButton);
        //Update
        $this->addActionButton(new UpdateButton());
        //Delete
        $this->addActionButton(new DeleteButton());
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('name'))->setSearchable(true)->setOrderable()->setOrderable('ASC'))
            ->addColumn((new Column('pool'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('cidr'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('gateway'))->setSearchable(true)->setOrderable());
    }

    public function replaceFieldGateway($key, $row)
    {
        if ($row->gateway)
        {
            return $row->gateway;
        }
        return "-";
    }

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


}