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

use ModulesGarden\ProxmoxAddon\App\Models\RangeVm;
use ModulesGarden\ProxmoxAddon\App\UI\Servers\Buttons\UpdateButton;
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\Buttons\ButtonRedirect;
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 ServersDataTable extends DataTable implements AdminArea
{

    public function initContent()
    {
        $this->initIds('serverList');
        $this->title = null;
        //Manage
        $rd = new ButtonRedirect;
        $rd->setRawUrl('configservers.php?action=manage')
            ->setRedirectParams(['id' => ':id']);
        $rd->replaceClasses(['lu-btn lu-btn--sm lu-btn--link lu-btn--icon lu-btn--plain lu-btn--default lu-tooltip drop-target drop-element-attached-bottom drop-element-attached-center drop-target-attached-top drop-target-attached-center']);
        $rd->setIcon('lu-btn-icon lu-zmdi lu-zmdi-edit');
        $this->addActionButton($rd);
        //Server details
        $rd = new ButtonRedirect('serverDetail');
        $rd->setRawUrl('addonmodules.php?module=proxmoxAddon&mg-page=home&mg-action=serverDetail')
            ->setRedirectParams(['id' => ':id']);
        $rd->replaceClasses(['lu-btn lu-btn--sm lu-btn--link lu-btn--icon lu-btn--plain lu-btn--default lu-tooltip drop-target drop-element-attached-bottom drop-element-attached-center drop-target-attached-top drop-target-attached-center']);
        $rd->setIcon('lu-btn__icon lu-zmdi lu-zmdi-info-outline');
        $this->addActionButton($rd);
    }

    public function replaceFieldResources($key, $row)
    {
        $resources = new ResourcesRow("resourcesRow" . $row->id);
        $resources->setIndex($row->id);
        return $resources->getHtml();
    }

    public function replaceFieldSuspended($key, $row)
    {
        $ajax = new SuspendedRow("suspendedRow" . $row->id);
        $ajax->setIndex($row->id);
        return $ajax->getHtml();
    }

    public function replaceFieldActiveAccounts($key, $row)
    {
        $active = Hosting::where('server', $row->id)->where("domainstatus", "Active")->count();
        return sprintf("%s / %s", $active, $row->maxaccounts);
    }

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

    public function replaceFieldRange($key, $row)
    {
        if ($row->vmid_from && $row->vmid_to)
        {
            return sprintf("%s - %s", $row->vmid_from, $row->vmid_to);
        }
        return '-';
    }

    protected function loadHtml()
    {
        $s = (new Server)->getTable();
        $r = (new RangeVm)->getTable();
        $this->addColumn((new Column('name', $s))->setSearchable(true, Column::TYPE_STRING)->setOrderable('ASC'))
            ->addColumn((new Column('ipaddress', $s))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('activeAccounts')))
            ->addColumn((new Column('resources')))
            ->addColumn((new Column('suspended')))
            ->addColumn((new Column('disabled', $s)))
            ->addColumn((new Column('range', $r))
            );
    }

    protected function loadData()
    {
        $s        = (new Server)->getTable();
        $r        = (new RangeVm)->getTable();
        $query    = (new Server)
            ->query()
            ->getQuery()
            ->leftJoin($r, "{$s}.id", '=', "{$r}.server_id")
            ->select("{$s}.id", "{$s}.name", "{$s}.ipaddress", "{$s}.hostname", "{$s}.active", "{$s}.maxaccounts", "{$s}.disabled", "{$r}.vmid_from", "{$r}.vmid_to")
            ->whereIn("{$s}.type", ["proxmoxVPS", "ProxmoxCloudVps"]);
        $dataProv = new QueryDataProvider();
        $dataProv->setDefaultSorting("name", 'ASC');
        $dataProv->setData($query);
        $this->setDataProvider($dataProv);
    }
}
