<?php

class ControllerCommonSendTask extends Controller
{

  public function index()
  {

    $this->load->type('tipocomum');
    $this->load->dmodel('setting/sendtask');
    $this->load->dmodel('setting/task');

    $filter = array();

    if (isset($this->request->get['tipo_task'])){
      $filter['tipo_task'] = $this->request->get['tipo_task'];
      switch($filter['tipo_task']){
        case Type_SendTask::TpSe_MarketplaceStatus : $tipo_task_control = Type_TaskUrl::Tp_TaskMarketplaceSendStatus;
      }
      $this->ftrgeralregistry->permissaoAcessoUrl($this->dmodel_setting_task->getTaskByType($tipo_task_control));
    } else {
      $this->ftrgeralregistry->permissaoAcessoUrl( $this->dmodel_setting_task->getTaskByType(Type_TaskUrl::Tp_TaskSendTask) );
      $tipo_task_control = Type_TaskUrl::Tp_TaskSendTask;
    }

    /*Default, caso o tipo de task se refira a outra url, validar para trocar o tipo e salvar corretamente ao rodar... */

    if (isset($this->request->get['tipo_queue'])){
      $filter['tipo_queue'] = $this->request->get['tipo_queue'];
    } else {
        $filter = array(
            'tipo_queue' => Type_SendQueue::TpQue_Whatsapp,
            'mode_tipo_queue' => Type_FilterContentValue::Tp_Diferente
        );
    }

    $tasks = $this->dmodel_setting_sendtask->getQueueTasks($filter);

    $atual = date('d/m/Y H:i:s');

    $data = array(
      'date_last_acting' => $this->db->dataAtual(),
      'type' => $tipo_task_control
    );


    $this->dmodel_setting_task->editDateTaskByType($data);

    if ($tasks) {
      foreach ($tasks as $key => $task) {

        if($task['enviado']!=1){

          $tz = new DateTimeZone('America/Sao_Paulo');
          $datatask = new DateTime($task['date_log_criacao']);
          $datatask->modify('+2 day');
          $datatask->setTimeZone($tz);

          $dateatual = DateTime::createFromFormat('d/m/Y H:i:s', $atual);
          $dateatual->setTimeZone($tz);

          if ($datatask > $dateatual) {
            try {
              $sendReturn = $this->sendtask->sendtask($task['tipo_task'], $task['data_value'], $task['date_attribute_value']);
              if ($sendReturn !== false) {
                $this->db->query("UPDATE " . DB_PREFIX . "task_queue SET enviado = 1 WHERE task_queue_id = '" . (int)$task['task_queue_id'] . "'");
              }
            } catch (Exception $e) {
              $this->db->query("UPDATE " . DB_PREFIX . "task_queue SET log_error = '" . $this->db->escape($e->getMessage()) . "', date_log_error = current_timestamp WHERE task_queue_id = '" . (int)$task['task_queue_id'] . "'");
            }
          }

        }

        if(!is_null($task['time_exclusion'])){
          $this->db->query("DELETE FROM ". DB_PREFIX ."task_queue WHERE task_queue_id = ".$task['task_queue_id']." AND ((date_log_criacao + INTERVAL ".$task['time_exclusion']." DAY) < CURRENT_DATE)");
        }else{
          $this->db->query("DELETE FROM ". DB_PREFIX ."task_queue WHERE task_queue_id = ".$task['task_queue_id']." and enviado = 1");
        }

      }
    }

  }

}

?>
