<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (27.03.19)
* *
*
*  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\Servers\ProxmoxCloudVps\App\UI\Snapshot\Pages;
use MGProvision\Proxmox\v2\models\Kvm;
use ModulesGarden\ProxmoxAddon\App\Models\SnapshotJob;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\Enum\JobPeriod;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\CreateJobButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\DeleteJobButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\DeleteJobMassButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Snapshot\Buttons\UpdateJobButton;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\AdminArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataProviders\Providers\QueryDataProvider;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\RawDataTable\RawDataTable;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class JobRawDataTable extends RawDataTable implements ClientArea, AdminArea
{
    use ApiService;
    use ProductService;

    protected $id = 'jobDataTable';
    protected $title = 'jobDataTable';

    public function initContent()
    {
        //Create
        $createButton = new CreateJobButton();
        $createButton->addClass("pmCreateJobButton");
        if (!$this->resourceGuard()->hasSnapshotJobLimit())
        {
            $createButton->addClass("hidden");
        }
        $this->addButton($createButton);
        //Update
        $this->addActionButton(new UpdateJobButton());
        //Delete
        $this->addActionButton(new DeleteJobButton());
        //Mass delete
        $this->addMassActionButton(new DeleteJobMassButton());
    }

    protected function loadHtml()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $this->addColumn((new Column('name'))->setSearchable(true, "string")->setOrderable());
        if ($vm instanceof Kvm)
        {
            $this->addColumn((new Column('vmstate'))->setSearchable(true)->setOrderable());
        }
        $this->addColumn((new Column('description'))->setSearchable(true)->setOrderable());
        $this->addColumn((new Column('days'))->setSearchable(true))
             ->addColumn((new Column('created_at'))->setSearchable(true, "date")->setOrderable('DESC'));

    }


    public function replaceFieldVmstate($key, $row)
    {
        if ($row->vmstate == "1")
        {
            return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr("Yes") . '</span>';
        }
        return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr("No") . '</span>';
    }

    public function replaceFieldDescription($key, $row)
    {
        if ($row->description)
        {
            return $row->description;
        }
        return '-';
    }

    public function replaceFieldDays($key, $row)
    {

        if($row->period == JobPeriod::DAILY && $row->days){
            $row = json_decode($row->days, true);
            foreach ($row as &$day){
                $day = sl("lang")->tr($day);
            }
            return implode(", ", $row);
        }elseif ($row->period == JobPeriod::HOURLY && $row->run_every &&  $row->run_every == 1){
            sl('lang')->addReplacementConstant('run_every', $row->run_every);
            return sl("lang")->tr("Every :run_every: hour");
        }elseif ($row->period == JobPeriod::HOURLY && $row->run_every &&  $row->run_every > 1){
            sl('lang')->addReplacementConstant('run_every', $row->run_every);
            return sl("lang")->tr("Every :run_every: hours");
        }
        return '-';
    }

    protected function loadData()
    {
        $query    = (new SnapshotJob())
            ->query()
            ->getQuery()
            ->select('id','name','description','vmstate','period', 'run_every','days','start_time','updated_at','created_at')
            ->where("hosting_id", $this->getWhmcsParamByKey('serviceid'));
        $dataProv = new QueryDataProvider();
        $dataProv->setData($query);
        $dataProv->setDefaultSorting("created_at", 'DESC');
        $this->setDataProvider($dataProv);
    }

}
