<?php

namespace ModulesGarden\HostingQuotaNotifications\App\Libs\TicketTemplates;

use \ModulesGarden\HostingQuotaNotifications\App\Models\TicketTemplates;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Models\TicketDepartment;
use \ModulesGarden\HostingQuotaNotifications\App\Helper\Constants\Functions;

/**
 * Description of Manage
 *
 * @author Artur Pilch <artur.pi@modulesgarden.com>
 */
class Manage
{

   public static function getTicketTemplatesAsKeyIdAndNameAsValue($onlyTicketTemplates = false)
   {
       $out = $onlyTicketTemplates ? [] : Functions::addNone();
       $ticketTemplates  = TicketTemplates::orderBy('name')->get()->pluck('name', 'id')->toArray();
       return $out + $ticketTemplates;
   }
    
    public static function getTemplates()
    {
        $ticketTemplates = TicketTemplates::all()->toArray();
        $departments = TicketDepartment::getDepartmentsIDkeyNAMEvalue();
        foreach($ticketTemplates as &$template)
        {
            $template['department_id'] = $departments[$template['department_id']];
        }
        return $ticketTemplates;
    }
    
    public static function deleteTicketTemplate($id)
    {
        TicketTemplates::where('id', '=', $id)->delete();

    }
    
    public static function getTicketTemplate($id)
    {
        $data = TicketTemplates::where('id', '=', $id)->first();
        return empty($data) ? $data : $data->toArray();
    }
    
    public static function updateTicketTemplate($id, $data)
    {
        TicketTemplates::where('id', '=', $id)->update($data);
    }
    
}
