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

use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\App\UI\RecoveryVms\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;

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

    public function initContent()
    {
        $this->initIds('recoveryVms');
        $this->title = null;
        $this->addActionButton(new DetailButton);
        $rd = new main\Core\UI\Widget\Buttons\ButtonRedirect();
        $rd->setRawUrl('addonmodules.php?module=proxmoxAddon&mg-page=home&mg-action=recoveryToFile');
        $rd->setIcon('lu-btn__icon lu-zmdi lu-zmdi-download');
        $rd->setTitle("export");
        $rd->replaceClasses(['lu-btn lu-btn--primary']);
        $rd->setShowTitle();
        $rd->deleteHtmlAttribute('data-toggle');
        $this->addButton($rd);
    }

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

    public function replaceFieldDomain($key, $row)
    {

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

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

    public function replaceFieldVirtualization($key, $row)
    {
        return main\Core\ServiceLocator::call('lang')->absoluteT($row->virtualization);
    }

    public function replaceFieldlast_update($key, $row)
    {
        return fromMySQLDate($row->last_update, true);
    }

    protected function loadHtml()
    {
        $r = (new main\App\Models\RecoveryVm)->getTable();
        $c = (new main\Core\Models\Whmcs\Client)->getTable();
        $h = (new Hosting)->getTable();
        $s = (new Server)->getTable();
        $this->addColumn((new Column('id', $r))->setOrderable("DESC"))
            ->addColumn((new Column('firstname', $c))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('domain', $h))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('server')))
            ->addColumn((new Column('node', $r))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('vmid', $r))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('virtualization', $r))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('last_update', $r))->setSearchable(true, Column::TYPE_DATE)->setOrderable());
    }

    protected function loadData()
    {
        $r        = (new main\App\Models\RecoveryVm)->getTable();
        $c        = (new main\Core\Models\Whmcs\Client)->getTable();
        $h        = (new Hosting)->getTable();
        $s        = (new Server)->getTable();
        $p        = (new main\Core\Models\Whmcs\Product)->getTable();
        $query    = (new main\App\Models\RecoveryVm)
            ->query()
            ->getQuery()
            ->leftJoin($c, "{$r}.client_id", '=', "{$c}.id")
            ->leftJoin($s, "{$r}.server_id", '=', "{$s}.id")
            ->leftJoin($h, "{$r}.service_id", '=', "{$h}.id")
            ->leftJoin($p, "{$h}.packageid", '=', "{$p}.id")
            ->select("{$r}.*", "{$h}.domain", "{$h}.domainstatus", "{$h}.server", "{$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);
    }
}
