<?php
namespace ThurData\Servers\KerioEmail\App\UI\Client\DomainAlias\Pages;

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

/**
 *
 * Created by PhpStorm.
 * User: ThurData
 * 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 KerioManager())
            ->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);

    }
}