<?php

/* * ********************************************************************
 * Proxmox Addon product developed. (Oct 12, 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\Jobs\Pages;

use http\Client;
use ModulesGarden\ProxmoxAddon\App\Models\Job;
use ModulesGarden\ProxmoxAddon\App\Models\Whmcs\Product;
use ModulesGarden\ProxmoxAddon\App\UI\Jobs\Buttons\DeleteButton;
use ModulesGarden\ProxmoxAddon\App\UI\Jobs\Buttons\InfoButton;
use ModulesGarden\ProxmoxAddon\App\UI\Jobs\Buttons\MassDeleteButton;
use ModulesGarden\ProxmoxAddon\App\UI\Jobs\Buttons\RunButton;
use ModulesGarden\ProxmoxAddon\App\UI\Jobs\Others\StatusLabel;
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;
use function ModulesGarden\ProxmoxAddon\Core\Helper\sl;

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

    protected function loadHtml()
    {
        $this->initIds('jobsDataTable');
        $this->title = null;
        $j           = (new Job())->getTable();
        $h        = (new Hosting)->getTable();
        $p        = (new Product())->getTable();
        $this->addColumn((new Column('id'))->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('job', $j))->setSearchable(true, 'string'))
            ->addColumn((new Column('status', $j))->setSearchable(true, 'string'))
            ->addColumn((new Column('updated_at', $j))->setSearchable(true, 'date')->setOrderable())
            ->addColumn((new Column('created_at', $j))->setSearchable(true, 'date')->setOrderable());
    }

    public function initContent()
    {
        $this->addActionButton(new InfoButton());
        $this->addActionButton(new RunButton());
        //delete
        $this->addActionButton(new DeleteButton());
        //mass delete
        $this->addMassActionButton(new MassDeleteButton());
    }

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

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

    public function replaceFieldid_($key, $row)
    {
        return $row->id;
    }

    public function replaceFieldJob($key, $row)
    {
        return sl('lang')->tr($row->job);
    }

    public function replaceFieldStatus($key, $row)
    {
        if (!$row->status)
        {
            $row->status = 'pending';
        }
        $label = new StatusLabel();
        $label->setStatus($row->status);
        return $label->getHtml();
    }

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

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

    protected function loadData()
    {
        $j        = (new Job)->getTable();
        $h        = (new Hosting)->getTable();
        $p        = (new Product())->getTable();
        $query    = (new  Job)
            ->query()
            ->getQuery()
            ->leftJoin($h, "{$j}.rel_id", '=', "{$h}.id")
            ->leftJoin($p, "{$h}.packageid", '=', "{$p}.id")
            ->select("{$j}.id", "{$j}.job", "{$j}.status", "{$j}.status AS statusRaw", "{$j}.created_at", "{$j}.updated_at",
                     "{$h}.domain",  "{$h}.userid", "{$h}.server", "{$h}.packageid", "{$h}.id AS hostingId",
                     "{$p}.name"
            );
        $dataProv = new QueryDataProvider();
        $dataProv->setDefaultSorting("id", 'desc');
        $dataProv->setData($query);
        $this->setDataProvider($dataProv);
    }
}
