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

use MGProvision\Proxmox\v2\repository\TaskRepository;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Vps\ProductService;
use ModulesGarden\Servers\ProxmoxVps\App\UI\TaskHistory\Buttons\InfoButton;
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 TaskHistoryDataTable extends DataTable implements ClientArea
{
    use ProductService;
    use ApiService;
    protected $id = 'taskHistoryDataTable';
    protected $title = 'taskHistoryDataTable';

    public function initContent()
    {
        //Info
        $this->addActionButton(new InfoButton());
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('starttime'))->setSearchable(true, Column::TYPE_DATE)->setOrderable('DESC'))
            ->addColumn((new Column('endtime'))->setSearchable(true, Column::TYPE_DATE)->setOrderable())
            ->addColumn((new Column('type'))->setSearchable(true))
            ->addColumn((new Column('status'))->setSearchable(true));
    }

    public function formatRowStarttime($key, $row)
    {
        return date('Y-m-d H:i:s', $row[$key]);
    }

    public function formatRowEndtime($key, $row)
    {
        return date('Y-m-d H:i:s', $row[$key]);
    }

    public function formatRowStatus($key,$row)
    {
        if ($row['status'] == "OK")
        {
            return '<span class="lu-label lu-label--success lu-label--status">' . sl('lang')->tr("OK") . '</span>';
        }
        return '<span class="lu-label lu-label--danger lu-label--status">' . sl('lang')->tr("Error") . '</span>';
    }

    public function formatRowSnaptime($key, $row)
    {
        return date('Y-m-d H:i:s', $row['snaptime']);
    }

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

    protected function loadData()
    {
        $repository = new TaskRepository();
        $repository->setApi($this->api());
        $repository->findByVm($this->vm())
            ->fromStartTime($this->getWhmcsParamByKey('model')->registrationDate->getTimestamp());
        $repository->limit(3000);
        $data = [];
        foreach ($repository->fetch() as $entity)
        {
            $row             = $entity->getAttributes();
            $row['entityId'] = $row['id'];
            $row['satusRaw'] = $row['status'];
            $row['id'] =  base64_encode(json_encode($row));
            foreach($row as  $k => &$entery){
                $method = 'formatRow'.ucfirst($k) ;
                if(method_exists($this, $method)){
                    $entery = $this->$method($k,$row);
                }
            }
            $data[] = $row;
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting("snaptime", 'DESC');
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


}