<?php

namespace ModulesGarden\HostingQuotaNotifications\App\UI\TicketTemplates\Providers;

use ModulesGarden\HostingQuotaNotifications\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Widget\Forms\DataProviders\BaseModelDataProvider;
use \ModulesGarden\HostingQuotaNotifications\Core\UI\Interfaces\AdminArea;
use \ModulesGarden\HostingQuotaNotifications\App\Libs\TicketTemplatesLangs\Manage;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Request;
use \ModulesGarden\HostingQuotaNotifications\App\Models\TicketTemplatesLangs;


/**
 * Description of TicketTemplatesLangsProvider
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class TicketTemplatesLangsProvider extends BaseModelDataProvider implements AdminArea
{

    public function __construct($model = null)
    {
        parent::__construct('ModulesGarden\HostingQuotaNotifications\App\Models\TicketTemplatesLangs');
    }

    public function read()
    {
        parent::read();

        $val                         = $this->data['lang'];
        unset($this->data['lang']);
        $this->data['lang']['value'] = $val;
    }

    public function create()
    {
        $ticketTemplatesLangsModel = new TicketTemplatesLangs();
        $ticketTemplatesLangsModel->fill($this->formData)->save();
        return (new HtmlDataJsonResponse())->setMessageAndTranslate('ticketTemplateLangAdded')->setStatusSuccess();
    }

    public function update()
    {
        parent::update();
        return (new HtmlDataJsonResponse())->setMessageAndTranslate('ticketTemplateLangUpdated')->setStatusSuccess();
    }

    public function delete()
    {
         if($this->getMassActionsValues())
         {
             return $this->massDeleteLangs($this->getMassActionsValues());
         }
         Manage::delete($this->formData['id']);
         return (new HtmlDataJsonResponse())->setMessageAndTranslate('ticketTemplateLangDeleted')->setStatusSuccess();
    }
    
    public function getTemplateLangs()
    {
        $langs = Manage::getTemplateLangs(Request::getValue('templateId'));
        foreach ($langs as &$lang)
        {
            $lang->lang = ucfirst($lang->lang);
        }
        return $langs->toArray();
    }

    public function massDeleteLangs($items)
    {
        foreach($items as $item)
        {
            Manage::delete($item);
        }
        return (new HtmlDataJsonResponse())->setMessageAndTranslate('ticketTemplateLangsDeleted')->setStatusSuccess();
    }
    
}
