<?php
namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DomainAlias\Pages;

use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\Domain;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DomainAlias\Buttons\AddDomainAliasButton;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DomainAlias\Buttons\AddListButton;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DomainAlias\Buttons\DeleteDomainAliasButton;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DomainAlias\Buttons\MassDeleteDomainAliasButton;
use ModulesGarden\Servers\ZimbraEmail\Core\Models\Whmcs\Hosting;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Interfaces\ClientArea;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\Column;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\DataProviders\DataProvider;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
use ModulesGarden\Servers\ZimbraEmail\Core\UI\Widget\DataTable\DataTable;

/**
 *
 * Created by PhpStorm.
 * User: Tomasz Bielecki ( tomasz.bi@modulesgarden.com )
 * Date: 18.09.19
 * Time: 13:37
 * Class DomainAliases
 */
class DomainAliases extends DataTable implements ClientArea
{
    protected $id    = 'lists';
    protected $name  = 'lists';
    protected $title = null;

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

    public function initContent()
    {
        $this->addMassActionButton(new MassDeleteDomainAliasButton());

        $this->addButton(new AddDomainAliasButton());

        $this->addActionButton(new DeleteDomainAliasButton());
    }

    public function loadData()
    {
        /**
         * load hosting
         */
        $hosting = Hosting::where('id', $this->getRequestValue('id'))->first();

        /**
         * load api client by server id
         * get repo
         * get aliases per domain
         */
        $aliases = (new ZimbraManager())
            ->getApiByServer($hosting->server)
            ->soap
            ->repository()
            ->domains
            ->getAliases($hosting->domain);


        /**
         *
         * prepare new array
         */
        $data = [];

        /**
         * parse aliases
         */
        foreach ($aliases as $domain)
        {
            /* @var $domain Domain */
            $tmp = [
                'id'    => $domain->getId(),
                'name'  => $domain->getName(),
                'description' => $domain->getAttr(Domain::ATTR_DESCRIPTION)
            ];

            $data[] = $tmp;
        }

        $dataProv = new ArrayDataProvider();
        $dataProv->setDefaultSorting('name', 'ASC')->setData($data);

        $this->setDataProvider($dataProv);

    }
}