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

use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataTable;

class NetworkInterfacesDataTable extends DataTable implements AdminArea
{
    use ProductService;
    use ApiService;


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

    protected function loadHtml()
    {
        $this->addColumn((new Column('name'))->setTitle("interfaceName"))
            ->addColumn((new Column('hardwareAddress')))
            ->addColumn((new Column('ipAddresses')));
    }


    public function replaceFieldHardwareAddress($key, $row)
    {

        if($row[$key]){
            return $row[$key];
        }
        return '-';
    }


    protected function loadData()
    {
        $data  = [];
        $agent = $this->vm()->agent();
        foreach ($agent->networkGetInterfaces()['result'] as $net)
        {
            $ips = [];
            foreach ($net['ip-addresses'] as $ipAddress)
            {
                $ips[] = "{$ipAddress['ip-address']} / {$ipAddress['prefix']}";
            }
            $data[] = [
                "name"            => $net['name'],
                "hardwareAddress" => $net['hardware-address'],
                "ipAddresses"     => implode("<br/>", $ips)
            ];
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


    public function initContent()
    {

    }

    public function isViewFooter()
    {
        return false;
    }

    public function isViewTopBody()
    {
        return false;
    }

}