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

use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\App\UI\Vms\Buttons\DetailButton;
use ModulesGarden\ProxmoxAddon\Core\Models\Whmcs\Hosting;
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 VmsDataTable extends DataTable implements AdminArea
{

    public function initContent()
    {
        $this->initIds('vms');
        $this->title = null;
        $this->addActionButton(new DetailButton);
    }

    public function replaceFieldName($key, $row)
    {
        return sprintf('<a href="configproducts.php?action=edit&id=%s">%s</a>', $row->packageid, $row->productName);
    }

    public function replaceFieldDomain($key, $row)
    {
        if (!$row->domain)
        {
            return sprintf('<a href="clientsservices.php?userid=%s&id=%s">%s</a>', $row->userid, $row->id, '-');
        }
        return sprintf('<a href="clientsservices.php?userid=%s&id=%s">%s</a>', $row->userid, $row->id, $row->domain);
    }

    public function replaceFieldDomainstatus($key, $row)
    {
        switch ($row->domainstatus)
        {
            case 'Pending':
                return '<span class="lu-label lu-label--warning lu-label--status">' . sl('lang')->tr($row->domainstatus) . '</span>';
            case 'Active':
            case 'Completed':
                return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr($row->domainstatus) . '</span>';
                break;
            case 'Suspended':
                return '<span class="lu-label lu-label--default lu-label--status">' . sl('lang')->tr($row->domainstatus) . '</span>';
                break;
            case 'Cancelled':
                return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row->domainstatus) . '</span>';
                break;
            case 'Fraud':
                return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row->domainstatus) . '</span>';
            case 'Terminated':
                return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row->domainstatus) . '</span>';
            default:
                return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row->domainstatus) . '</span>';

        }
    }

    public function replaceFieldDedicatedip($key, $row)
    {
        return $row->dedicatedip ? $row->dedicatedip : "-";
    }

    public function replaceFieldAssignedips($key, $row)
    {
        if($row->assignedips){
            $ips = explode("\n", $row->assignedips);
            $ips = array_filter($ips);
            $ips = array_unique($ips);
            return implode(", ", $ips);
        }
        return  "-";
    }

    public function replaceFieldFirstname($key, $row)
    {
        return sprintf('<a href="clientssummary.php?userid=%s">%s %s</a>', $row->userid, $row->firstname, $row->lastname);
    }

    public function replaceFieldServer($key, $row)
    {
        return sprintf('<a href="configservers.php?action=manage&id=%s">%s</a>', $row->server, $row->serverName);
    }

    public function replaceFieldBwusage($key, $row)
    {
        if ($row->bwusage == 0)
        {
            return '-';
        }
        $bytes = $row->bwusage * pow(1024,2);
        return main\App\Libs\Format::convertBytes($bytes);
    }

    protected function loadHtml()
    {
        $h = (new Hosting)->getTable();
        $p = (new main\Core\Models\Whmcs\Product)->getTable();
        $s = (new Server)->getTable();
        $c = (new main\Core\Models\Whmcs\Client)->getTable();
        $this->addColumn((new Column('id', $h))->setSearchable(true, Column::TYPE_INT)->setOrderable("DESC"))
            ->addColumn((new Column('name', $p))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('domain', $h))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('domainstatus', $h))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('bwusage', $h))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('dedicatedip', $h))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('assignedips', $h))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('firstname', $c))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('server'))
            );
    }

    protected function loadData()
    {
        $h        = (new Hosting)->getTable();
        $p        = (new main\Core\Models\Whmcs\Product)->getTable();
        $s        = (new Server)->getTable();
        $c        = (new main\Core\Models\Whmcs\Client)->getTable();
        $query    = (new Hosting)
            ->query()
            ->getQuery()
            ->leftJoin($p, "{$h}.packageid", '=', "{$p}.id")
            ->leftJoin($c, "{$h}.userid", '=', "{$c}.id")
            ->leftJoin($s, "{$h}.server", '=', "{$s}.id")
            ->select("{$h}.id", "{$h}.domain", "{$h}.domainstatus", "{$h}.dedicatedip", "{$h}.assignedips", "{$h}.userid", "{$h}.server", "{$h}.packageid", "{$h}.bwusage",
                "{$c}.firstname", "{$c}.lastname", "{$s}.name AS serverName", "{$p}.name AS productName")
            ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps"]);
        $dataProv = new QueryDataProvider();
        $dataProv->setDefaultSorting("id", 'DESC');
        $dataProv->setData($query);
        $this->setDataProvider($dataProv);
    }
}
