<?php
namespace ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DistributionList\Pages;
use ModulesGarden\Servers\ZimbraEmail\App\Helpers\ZimbraManager;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Models\DistributionList;
use ModulesGarden\Servers\ZimbraEmail\App\Libs\Zimbra\Components\Api\Soap\Repository;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Admin\Custom\Fields\EnabledField;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DistributionList\Buttons\AddListButton;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DistributionList\Buttons\DeleteListButton;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DistributionList\Buttons\EditListButton;
use ModulesGarden\Servers\ZimbraEmail\App\UI\Client\DistributionList\Buttons\MassDeleteListButton;
use function ModulesGarden\Servers\ZimbraEmail\Core\Helper\di;
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:28
 * Class Lists
 */
class Lists extends DataTable implements ClientArea
{
    protected $id    = 'lists';
    protected $name  = 'lists';
    protected $title = null;

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

    /**
     *
     * return status as html element
     * @param $key
     * @param $row
     * @return string
     */
    public function replaceFieldStatus($key, $row)
    {
        /**
         * check status
         */
        $enabled =  $row[$key] === 'enabled' ? true : false;

        /**
         *
         * create enabled element
         */
        $field = new EnabledField();
        $field->setEnabled($enabled);
        $field->setRawTitle(di('lang')->absoluteT('zimbra','account','status',$row[$key]));

        return $field->getHtml();
    }

    public function initContent()
    {
        $this->addMassActionButton(new MassDeleteListButton());
        $this->addButton(new AddListButton());
        $this->addActionButton(new EditListButton());
        $this->addActionButton(new DeleteListButton());
    }

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

        /**
         * load api
         */
        $api     = (new ZimbraManager())->getApiByServer($hosting->server);

        $repository = new Repository($api->soap);
        $lists = $repository->lists->getAllDistributionListsByDomain($hosting->domain);

        $data = [];
        foreach($lists as $list)
        {
            /* @var $list  DistributionList */

            $tmp = [
                'id' => $list->getId(),
                'email' => $list->getName(),
                'name' => $list->getDataResourceA(DistributionList::ATTR_DISPLAY_NAME),
                'status' => $list->getDataResourceA(DistributionList::ATTR_MAIL_STATUS)
            ];

            $data[] = $tmp;
        }

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

        $this->setDataProvider($dataProv);
    }

}