<?php

/* * ********************************************************************
 * WordPress Manager product developed. (Feb 5, 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\VmCleaner\Pages;

use MGProvision\Proxmox\v2 as proxmox;
use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\App\UI\VmCleaner\Buttons\DeleteButton;
use ModulesGarden\ProxmoxAddon\App\UI\VmCleaner\Buttons\DeleteMassButton;
use ModulesGarden\ProxmoxAddon\Core\Helper;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Column;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\RawDataTable\RawDataTable;
use WHMCS\Database\Capsule;
use WHMCS\Service\Service;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;
use Illuminate\Database\Capsule\Manager as DB;

/**
 * Description of PluginInstalled
 *
 * @author Pawel Kopec <pawelk@modulesgardne.com>
 */
class VmCleanerDataTable extends RawDataTable implements AdminArea
{

    use main\App\Services\BaseService;
    use main\App\Helper\VmidHelper;
    protected $id = 'vmCleaner';
    protected $name = 'vmCleaner';
    protected $title = 'vmCleanerTitle';
    private $proxmoxCloudTable;


    public function isRawTitle()
    {
        return false;
    }

    public function initContent()
    {
        $this->addActionButton(new DeleteButton);
        $this->addMassActionButton(new DeleteMassButton);
        try
        {
            $this->setServerId($this->getRequestValue('id'))->getApi();
        }
        catch (\Exception $ex)
        {
            $this->setInternalAlertMessage($ex->getMessage())
                ->setInternalAlertMessageType('danger');
        }
    }

    public function replaceFieldVirtualization($key, $row)
    {
        return Helper\sl('lang')->absoluteT($row['type']);
    }

    public function replaceFieldStatus($key, $row)
    {
        switch ($row['status'])
        {
            case 'stopped':
                return '<span class="lu-label lu-label--default lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
            case 'running':
                return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
                break;
            case 'unknown':
                return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
                break;
            case 'io-error':
                return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
                break;
            default:
                return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr($row['status']) . '</span>';
        }
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('node'))->setSearchable(true, Column::TYPE_STRING)->setOrderable('ASC'))
            ->addColumn((new Column('vmid'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('name'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('virtualization'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('status'))->setSearchable(true, Column::TYPE_STRING)->setOrderable());
    }

    protected function loadData()
    {
        session_write_close();
        $this->setServerId($this->getRequestValue('id'))->getApi()->setInstance();
        $dataProv          = new main\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider();
        $data              = [];
        $clusterRepository = new proxmox\repository\ClusterResourcesRepository();
        foreach ($clusterRepository->fetch() as $resurce)
        {
            if (!in_array($resurce->getType(), ['qemu', 'lxc']) || $resurce->getTemplate() == '1' || $resurce->getStatus() == "unknown" ||
                $this->vmidExistInWhmcs( $resurce->getVmid()))
            {
                continue;
            }
            $data[] = array_merge($resurce->toArray(), ["id" => base64_encode(json_encode($resurce->toArray()))]);
        }
        $dataProv->setDefaultSorting("node", 'ASC');
        $dataProv->setData((array)$data);
        $this->setDataProvider($dataProv);
    }
}
