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

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

    public function initContent()
    {
        $this->initIds('taskHistory');
        $this->title = null;
        $this->addActionButton(new main\App\UI\TaskHistory\Buttons\DeleteButton());
        $this->addMassActionButton(new DeleteMassButton);
    }

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

    public function replaceFieldStatus($key, $row)
    {
        $backgroundColor = [
            "0" => "f89406",
            "1" => "46a546",
            "2" => "d9534f",
        ];
        $label           = new main\Core\UI\Widget\Others\Label();
        $lang            = main\Core\ServiceLocator::call('lang');
        $label->setMessage($lang->absoluteT('status', $row->status))
            ->setTitle($lang->absoluteT('status', $row->status))
            ->setColor('fcffff')
            ->setBackgroundColor($backgroundColor[$row->status]);
        return $label->getHtml();
    }

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

    protected function loadHtml()
    {
        $t = (new main\App\Models\TaskHistory)->getTable();
        $h = (new Hosting)->getTable();
        $this->addColumn((new Column('id', $t))->setSearchable(true, Column::TYPE_INT)->setOrderable('DESC'))
            ->addColumn((new Column('domain', $h))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('node', $t))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('vmid', $t))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('status', $t))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('name', $t))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('description', $t))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('start_time', $t))->setSearchable(true, Column::TYPE_DATE)->setOrderable());
    }

    protected function loadData()
    {
        $t        = (new main\App\Models\TaskHistory)->getTable();
        $h        = (new Hosting)->getTable();
        $p        = (new main\Core\Models\Whmcs\Product)->getTable();
        $query    = (new main\App\Models\TaskHistory)
            ->query()
            ->getQuery()
            ->leftJoin($h, "{$t}.hosting_id", '=', "{$h}.id")
            ->select("{$t}.*", "{$h}.domain", "{$h}.userid");
        $dataProv = new QueryDataProvider();
        $dataProv->setDefaultSorting("id", 'DESC');
        $dataProv->setData($query);
        $this->setDataProvider($dataProv);
    }
}
