<?php

/* * ********************************************************************
 * ProxmoxAddon product developed. (Aug 22, 2018)
 * *
 *
 *  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\ProxmoxAddon\App\UI\IpManagement\Pages;

use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\App\UI\IpManagement\Buttons\CreateButton;
use ModulesGarden\ProxmoxAddon\App\UI\IpManagement\Buttons\DeleteButton;
use ModulesGarden\ProxmoxAddon\App\UI\IpManagement\Buttons\DeleteMassButton;
use ModulesGarden\ProxmoxAddon\App\UI\IpManagement\Buttons\UpdateButton;
use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Server;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Column;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\DataProviders\Providers\QueryDataProvider;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\DataTable;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

/**
 * Description of ServersDataTable
 *
 * @author Pawel Kopec <pawelk@modulesgardne.com>
 */
class IpManagementDataTable extends DataTable implements AdminArea
{

    public function initContent()
    {
        $this->initIds('ipManagement');
        $this->title = null;
        $this->addButton(new CreateButton);
        $this->addActionButton(new UpdateButton);
        $this->addActionButton(new DeleteButton);
        $this->addMassActionButton(new DeleteMassButton);
    }

    public function replaceFieldMac_address($key, $row)
    {
        if ($row->mac_address && $row->mac_address != 'auto')
        {
            return $row->mac_address;
        }
        return '-';
    }

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

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

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

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

    public function replaceFieldName($key, $row)
    {
        if ($row->sid == "0" && !$row->sid)
        {
            return '-';
        }
        return sprintf('<a href="configservers.php?action=manage&id=%s">%s</a>', $row->sid, $row->name);;
    }

    public function replaceFieldNode($key, $row)
    {
        if ($row->node == "0" && !$row->node)
        {
            return '-';
        }
        return $row->node;
    }

    public function replaceFieldVisualization($key, $row)
    {
        if ($row->visualization == "Auto")
        {
            return '-';
        }
        return $row->visualization;
    }

    public function replaceFieldPrivate($key, $row)
    {
        if ($row->private == '1')
        {
            return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr('Yes') . '</span>';
        }
        return '<span class="lu-label lu-label--default lu-label--status">' . sl('lang')->tr('No') . '</span>';

        return $row->private == '1' ? '<span class="glyphicon glyphicon-ok-sign"></span>' : '<span class="glyphicon glyphicon-remove-sign"></span>';
    }

    protected function loadHtml()
    {
        $i = (new main\App\Models\IpAddress())->getTable();
        $s = (new Server)->getTable();
        $this->addColumn((new Column('ip', $i))->setSearchable(true, Column::TYPE_STRING)->setOrderable('ASC'))
            ->addColumn((new Column('mac_address', $i))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('trunks', $i))->setSearchable(true, 'int')->setOrderable())
            ->addColumn((new Column('tag', $i))->setSearchable(true, 'int')->setOrderable())
            ->addColumn((new Column('subnet_mask', $i))->setSearchable(true, 'string')->setOrderable())
            ->addColumn((new Column('gateway', $i))->setSearchable(true, 'string')->setOrderable())
            ->addColumn((new Column('cidr', $i))->setSearchable(true, 'string')->setOrderable())
            ->addColumn((new Column('name', $s))->setSearchable(true, 'string')->setOrderable())
            ->addColumn((new Column('node', $i))->setSearchable(true, 'string')->setOrderable())
            ->addColumn((new Column('visualization', $i))->setSearchable(true, 'string')->setOrderable())
            ->addColumn((new Column('private', $i)))
            ->addColumn((new Column('bridge', $i))->setSearchable(true, 'string')->setOrderable());

    }

    protected function loadData()
    {
        $i        = (new main\App\Models\IpAddress())->getTable();
        $s        = (new Server)->getTable();
        $query    = (new main\App\Models\IpAddress)
            ->query()
            ->getQuery()
            ->leftJoin($s, "{$s}.id", '=', "{$i}.sid")
            ->select("{$i}.*", "{$s}.name")
            ->where("{$i}.hosting_id", '0');
        $dataProv = new QueryDataProvider();
        $dataProv->setDefaultSorting("ip", 'ASC');
        $dataProv->setData($query);
        $this->setDataProvider($dataProv);
    }
}
