<?php

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

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

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

    public function __construct($model = null)
    {
        parent::__construct($model);
    }

    public function read()
    {
        $this->loadDataToConfiguration();
    }

    public function create()
    {
        
    }

    public function update()
    {
        Manage::updateTicketTemplate(Request::getValue('templateId'), $this->formData);
        return (new ResponseTemplates\HtmlDataJsonResponse())->setMessageAndTranslate('configurationUpdated')->setStatusSuccess();
    }

    public function delete()
    {
        
    }

    private function loadDataToConfiguration()
    {
        $id = Request::getValue('templateId');
        if (!$id)
        {
            return;
        }

        $selects = ['department_id', 'admin_id'];

        $ticketTemplate = Manage::getTicketTemplate($id);

        foreach ($ticketTemplate as $key => $value)
        {
            if (in_array($key, $selects))
            {
                $this->data[$key]['value'] = (int)$value;
                continue;
            }
            $this->data[$key] = $value;
        }
    }
}
