<?php

namespace ModulesGarden\ProxmoxAddon\App\UI\CloudInitScript\Pages;

use ModulesGarden\ProxmoxAddon\App\Models\CloudInitScript;
use ModulesGarden\ProxmoxAddon\App\UI\CloudInitScript\Buttons\DeleteButton;
use ModulesGarden\ProxmoxAddon\App\UI\CloudInitScript\Buttons\DeleteMassButton;
use ModulesGarden\ProxmoxAddon\App\UI\CloudInitScript\Buttons\UpdateButton;
use ModulesGarden\ProxmoxAddon\Core\Helper\BuildUrl;
use ModulesGarden\ProxmoxAddon\Core\UI\Interfaces\AdminArea;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\Buttons\ButtonRedirect;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\Column;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\DataProviders\Providers\QueryDataProvider;
use ModulesGarden\ProxmoxAddon\Core\UI\Widget\DataTable\DataTable;

class CloudInitScriptDataTable extends DataTable implements AdminArea
{

    public function initContent()
    {
        //create
        $buttonRedirect = new ButtonRedirect('create');
        $buttonRedirect->setShowTitle();
        $buttonRedirect->setRawUrl(BuildUrl::getUrl('CloudInitScript', 'create'));
        $buttonRedirect->setIcon('lu-icon-in-button lu-zmdi lu-zmdi-plus');
        $buttonRedirect->replaceClasses(['lu-btn lu-btn--primary']);
        $buttonRedirect->deleteHtmlAttribute('data-toggle');
        $this->addButton($buttonRedirect);
        //update
        $this->addActionButton(new UpdateButton());
        //delete
        $this->addActionButton(new DeleteButton());
        //mass delete
        $this->addMassActionButton(new DeleteMassButton());

    }

    protected function loadHtml()
    {
        $this->addColumn((new Column('id'))->setSearchable(true)->setOrderable('DESC'))
             ->addColumn((new Column('name'))->setSearchable(true, Column::TYPE_STRING));
    }

    protected function loadData()
    {
        $query    = (new CloudInitScript())
            ->query()
            ->getQuery()
            ->select("id", "name");
        $dataProv = new QueryDataProvider();
        $dataProv->setDefaultSorting("id", 'DESC');
        $dataProv->setData($query);
        $this->setDataProvider($dataProv);
    }
}