<?php

/* * ********************************************************************
 * WordPress Manager product developed. (Feb 5, 2018)
 * *
 *
 *  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\ProxmoxAddon\App\UI\ServerGroup\Pages;

use ModulesGarden\ProxmoxAddon as main;
use ModulesGarden\ProxmoxAddon\App\UI\ServerGroup\Buttons\CreateButton;
use ModulesGarden\ProxmoxAddon\App\UI\ServerGroup\Buttons\DeleteButton;
use ModulesGarden\ProxmoxAddon\App\UI\ServerGroup\Buttons\DeleteMassButton;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Column;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\DataProviders\Providers\QueryDataProvider;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\RawDataTable\RawDataTable;

/**
 * Description of PluginInstalled
 *
 * @author Pawel Kopec <pawelk@modulesgardne.com>
 */
class ServerGroupDataTable extends RawDataTable implements AdminArea
{

    protected $id = 'serverGroup';
    protected $name = 'serverGroup';
    protected $title = 'serverGroupTitle';

    public function isRawTitle()
    {
        return false;
    }

    public function initContent()
    {
        //Create
        $this->addButton(new CreateButton);
        //Update
        $this->addActionButton(new main\App\UI\ServerGroup\Buttons\UpdateButton());
        //Delete
        $this->addActionButton(new DeleteButton);
        //Delete Mass
        $this->addMassActionButton(new DeleteMassButton);
    }

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

    public function replaceFieldNode($key, $row)
    {
        $nodes = main\App\Models\ServerGroupItem::ofGroupId($row->id)->pluck("node")->toArray();
        return implode(", ", $nodes);
    }

    protected function loadHtml()
    {
        $sg = (new main\App\Models\ServerGroup())->getTable();
        $this->addColumn((new Column('id',$sg ))->setSearchable(true, Column::TYPE_STRING)->setOrderable('ASC'))
            ->addColumn((new Column('name',  $sg))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('node'))->setSearchable(true, Column::TYPE_STRING)->setOrderable())
            ->addColumn((new Column('created_at',  $sg))->setSearchable(true, Column::TYPE_STRING)->setOrderable());
    }

    protected function loadData()
    {
        $serverId = (int)$this->getRequestValue('id');
        $sg = (new main\App\Models\ServerGroup())->getTable();
        $sgi = (new main\App\Models\ServerGroupItem())->getTable();
        $dataProv          = new QueryDataProvider();
        $query = (new main\App\Models\ServerGroup())
            ->query()
            ->getQuery()
            ->select("{$sg}.id","{$sg}.name", "{$sg}.created_at")
            ->leftJoin($sgi,"{$sgi}.group_id", "=", "{$sg}.id")
            ->where("{$sg}.server_id", $serverId)
            ->groupBy("{$sg}.id");
        $dataProv->setDefaultSorting("name", 'ASC');
        $dataProv->setData(  $query );
        $this->setDataProvider($dataProv);
    }
}
