<?php
/* * ********************************************************************
*  ProxmoxVPS Product developed. (26.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\ProxmoxVps\App\UI\BackupJob\Pages;

use MGProvision\Proxmox\v2\repository\BackupScheduleRepository;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Buttons\MailtoSwitchButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Buttons\RestoreButton;
use ModulesGarden\Servers\ProxmoxVps\App\UI\BackupJob\Buttons\UpdateButton;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\ProxmoxVps\Core\UI\Widget\DataTable\DataTable;
use function ModulesGarden\Servers\ProxmoxVps\Core\Helper\sl;

class BackupJobDataTable extends DataTable implements ClientArea
{
    use ProductService, ApiService;

    protected $id = 'dackupJobDataTable';
    protected $title = 'dackupJobDataTable';

    public function initContent()
    {
        //Create
        $createButton = new CreateButton();
        if (!$this->resourceGuard()->hasBackupJobLimit())
        {
            $createButton->addClass("hidden");
        }
        $this->addButton($createButton );
        //Update
        $this->addActionButton(new UpdateButton());
        //Delete
        $this->addActionButton(new DeleteButton());
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('displayId'))->setSearchable(true, "int")->setOrderable('DESC'))
            ->addColumn((new Column('starttime'))->setSearchable(true, "date")->setOrderable())
            ->addColumn((new Column('dow'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('compress'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('mode'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('mailto')));
    }


    public function replaceFieldDow($key, $row)
    {
        $down = explode(",", $row['dow']);
        $days = [];
        foreach ($down as $day)
        {
            $days[] = sl('lang')->tr($day);
        }
        return implode(", ", $days);
    }

    public function replaceFieldCompress($key, $row)
    {
        return sl('lang')->tr($row['compress']);
    }

    public function replaceFieldMode($key, $row)
    {
        return sl('lang')->tr($row['mode']);
    }

    public function customColumnHtmlMailto()
    {
        return (new MailtoSwitchButton())->getHtml();
    }

    public function replaceFieldMailto($key, $row)
    {
        return $row['mailto'] ? "on" : "off";

    }

    protected function loadData()
    {
        $scheduleRepository = new BackupScheduleRepository();
        $scheduleRepository->setApi($this->api());
        $scheduleRepository->findByVm($this->vm());
        $data = [];
        foreach ($scheduleRepository->fetch() as $job)
        {
            $row                     = $job->getAttributes();
            $row['backupScheduleId'] = $row['id'];
            unset($row['id'], $row['days']);
            $data[] = array_merge(['id' => base64_encode(json_encode($row))], $row);
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting("displayId", 'DESC');
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


}