<?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\VirtualInterface\Pages;

use ModulesGarden\ProxmoxAddon\App\Models\VirtualInterface;
use ModulesGarden\ProxmoxAddon\App\Models\VirtualNetwork;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\Buttons\InfoButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\Buttons\MailtoSwitchButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\Buttons\RestoreButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\VirtualInterface\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 VirtualInterfaceDataTable extends DataTable implements ClientArea
{
    use ProductService;
    use ApiService;

    protected $id = 'virtualInterfaceDataTable';
    protected $title = 'virtualInterfaceDataTable';

    public function initContent()
    {
        $createButton = new CreateButton();
        $createButton->addClass("pmVnCreateButton");
        /**
         * @todo validate limit
         */
        if (!$this->configuration()->isPermissionVirtualNetwork())
        {
            $createButton->addClass("hidden");
        }
        $this->unsetShowTitle();

        //Create
        $this->addButton($createButton);
        //Delete
        $this->addActionButton(new DeleteButton());
    }

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


    public function replaceFieldName($key, $row)
    {
        if ($row->vn_id==0)
        {
            return sl('lang')->abtr("Public");
        }
        return $row->name;

    }
    protected function loadData()
    {
        $vmId = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVmModel()->id;
        $vn = (new VirtualNetwork())->getTable();
        $vi = (new VirtualInterface())->getTable();
        $query    = VirtualInterface::select( "{$vn}.name","{$vi}.vn_id", "{$vi}.ip", "{$vi}.id")
                           ->leftJoin($vn, "{$vn}.id", "=" ,"{$vi}.vn_id")
                           ->where("{$vi}.vm_id", $vmId)
                           ->getQuery();
        $dataProv = new QueryDataProvider();
        $dataProv->setDefaultSorting("name", 'ASC');
        $dataProv->setData($query);
        $this->setDataProvider($dataProv);
    }


}