<?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\ProxmoxCloudVps\App\UI\Backup\Pages;

use MGProvision\Proxmox\v2\repository\FileRepository;
use ModulesGarden\ProxmoxAddon\App\Libs\Format;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Backup\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Backup\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Backup\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\Backup\Buttons\RestoreButton;
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\ArrayDataProvider;
use ModulesGarden\Servers\ProxmoxCloudVps\Core\UI\Widget\DataTable\DataTable;

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

    protected $id   = 'backupDataTable';
    protected $name = 'backupDataTableName';
    protected $title = 'backupDataTableTitle';

    public function isRawTitle()
    {
        return false;
    }

    public function initContent()
    {
        //Create
        $createButton = new CreateButton();
        $createButton->addClass("pmCreateBackupButton");
        if (!$this->resourceGuard()->hasBackupLimit())
        {
            $createButton->addClass("hidden");
        }
        $this->unsetShowTitle();
        if ($this->configuration()->isPermissionBackup())
        {
            $this->addButton($createButton);
        }

        //Restore
        $this->addActionButton(new RestoreButton());
        //Delete
        $this->addActionButton(new DeleteButton());
        //Delete Mass
        $this->addMassActionButton(new DeleteMassButton());
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('date'))->setSearchable(true, "date")->setOrderable('DESC'))
            ->addColumn((new Column('format'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('size'))->setSearchable(true)->setOrderable());
    }

    protected function loadData()
    {
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        $backupRepository = new FileRepository();
        $backupRepository->setApi($this->api());
        $backupRepository->findBackup($vm)
            ->findByStorages([$this->configuration()->getBackupStorage()]);
        $data = [];
        foreach ($backupRepository->fetch() as $file)
        {
            $row         = $file->getAttributes();
            $row['size'] = Format::convertBytes($row['size']);
            $row['date'] = $row['date'] . " " . $row['hour'];
            $data[]      = array_merge(['id' => base64_encode(json_encode($row))], $row);
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting("date", 'DESC');
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


}