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

use ModulesGarden\ProxmoxAddon\App\Libs\Format;
use ModulesGarden\ProxmoxAddon\App\Services\ApiService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ProductService;
use ModulesGarden\ProxmoxAddon\App\Services\Cloud\ResourceManager;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\MountPoint\Buttons\BackupSwitchButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\MountPoint\Buttons\CreateButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\MountPoint\Buttons\DeleteButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\MountPoint\Buttons\DeleteMassButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\MountPoint\Buttons\RestoreButton;
use ModulesGarden\Servers\ProxmoxCloudVps\App\UI\MountPoint\Buttons\UpdateButton;
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;
use function ModulesGarden\Servers\ProxmoxCloudVps\Core\Helper\sl;

class MountPointDataTable extends DataTable implements ClientArea
{
    use ApiService;
    use ProductService;
    protected $id = 'mountPointDataTable';
    protected $name = 'mountPointDataTable';
    protected $title = 'mountPointDataTableTitle';

    public function initContent()
    {
        $resourceManager = new ResourceManager();
        $this->setInternalAlertMessage("Adding, editing or removing disk will reboot the virtual machine.");
        //Create
        $createButton = new CreateButton();
        $createButton->addClass("pmCreateDiskButton");
        if (! $resourceManager->disk()->hasFreeTotal())
        {
            $createButton->addClass("hidden");
        }
        $this->unsetShowTitle();

        $this->addButton($createButton);
        //Update
        $this->addActionButton(new UpdateButton());
        //Delete
        $this->addActionButton(new DeleteButton());
    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('id'))->setSearchable(true, "string")->setOrderable('DESC'))
            ->addColumn((new Column('name'))->setSearchable(true, "string")->setOrderable())
            ->addColumn((new Column('mp'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('backup'))->setSearchable(true)->setOrderable())
            ->addColumn((new Column('bytes'))->setSearchable(true)->setOrderable());
    }

    public function replaceFieldName($key, $row)
    {
        $id = preg_replace('/[^0-9]/', '', $row[$key]);
        $id++;
        sl("lang")->addReplacementConstant("id",$id);
        return sl("lang")->abtr('Disk :id:');
    }

    public function replaceFieldPath($key, $row)
    {
        if ($row[$key])
        {
            return $row[$key];
        }
        return '-';
    }

    public function customColumnHtmlBackup()
    {
        return (new BackupSwitchButton())->getHtml();
    }

    public function replaceFieldBackup($key, $row)
    {
        if ($row['master'])
        {
            return "on";
        }
        return $row['backup'] == "1" ? "on" : "off";
    }

    public function replaceFieldBytes($key, $row)
    {
        return Format::convertBytes($row['bytes']);
    }

    protected function loadData()
    {
        $data = [];
        $vm = \ModulesGarden\ProxmoxAddon\Core\Helper\sl('Vm')->getVm();
        foreach ( $vm->getMounPoints()->fetch() as $entity)
        {
            $data[] = [
                "id"     => $entity->getId(),
                "name"   => $entity->getName(),
                "mp"   => $entity->getMp(),
                "backup" => $entity->getBackup(),
                "size"   => $entity->getSize(),
                "bytes"  => $entity->getBytes(),
                "master" => $entity->getId() == "rootfs"
            ];
        }
        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting("id", 'DESC');
        $dataProv->setData($data);
        $this->setDataProvider($dataProv);
    }


}