<?php

namespace ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Buttons;

use \ModulesGarden\HostingQuotaNotifications\Core\UI\Builder\BaseContainer;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AjaxElementInterface;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Modals\ExampleModal;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates;

/**
 * base button controller
 *
 * @author Sławomir Miśkowicz <slawomir@modulesgarden.com>
 */
class BaseDatatableModalButton extends BaseContainer implements AjaxElementInterface
{
    protected $id             = 'baseDatatableModalButton';
    protected $class          = ['btn btn-circle btn-outline btn-inverse btn-success btn-icon-only'];
    protected $icon           = 'fa fa-plus';
    protected $title          = 'baseDatatableModalButton';
    protected $htmlAttributes = [
        'href'        => 'javascript:;',
        'data-toggle' => 'tooltip',
    ];
    protected $modal          = null;

    public function returnAjaxData()
    {
        return (new ResponseTemplates\HtmlDataJsonResponse($this->modal->getHtml()))->setCallBackFunction($this->callBackFunction);
    }

    public function initContent()
    {
        $this->initLoadModalAction(new ExampleModal());
    }

    public function setModal($modal)
    {
        $modal->setMainContainer($this->mainContainer);
        $this->modal = $modal;
        if ($modal instanceof \ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AjaxElementInterface)
        {
            $this->mainContainer->addAjaxElement($modal);
        }
        
        return $this;
    }

    protected function initLoadModalAction($modal)
    {
        $this->htmlAttributes['@click'] = 'loadModal($event, \'' . $this->id . '\', \'' . $this->getNamespace() . '\', \'' . $this->getIndex() . '\')';
        $this->setModal($modal);
        
        return $this;
    }
}
