<?php

class ControllerFeedbackFeedback extends Controller
{

    public function authorization(){

        $this->load->language('feedback/feedback');
        $this->load->dmodel('feedback/feedback');

        $json = array();

        if(isset($this->request->post['feedback_status'])){
            $feedback_status = $this->request->post['feedback_status'];
        }else{
            $json['warning'] = $this->language->get('error_feedback_status');
        }

        if(!$json){
            $this->dmodel_feedback_feedback->setFeedbackStatus($feedback_status);
            $json['success'] = ($feedback_status) ? $this->language->get('text_feedback_status_enabled') : $this->language->get('text_feedback_status_disabled');
        }

        $this->response->setOutputJson($json);

    }

     public function sendmessages()
    {

        $this->load->dmodel('setting/task');
        $this->ftrgeralregistry->permissaoAcessoUrl( $this->dmodel_setting_task->getTaskByType(Type_TaskUrl::TP_TaskFeedback) );

        if($this->config->get('feedback_automatico_status') && $this->feedback->isEnabled()){

            $data = array(
                'date_last_acting' => $this->db->dataAtual(),
                'type' => Type_TaskUrl::TP_TaskFeedback
            );
            $this->dmodel_setting_task->editDateTaskByType($data);


            $this->load->dmodel('setting/sendtask');
            $filter = array(
                'tipo_queue' => Type_SendQueue::TpQue_Whatsapp
            );

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

            if($results){

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

                foreach ($results as $key => $result){

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

                        $tz = new DateTimeZone('America/Sao_Paulo');
                        $datatask = new DateTime($result['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 {
                                $this->sendwhats->sendwhats($result['tipo_task'], $result['data_value'], $result['date_attribute_value']);
                                $this->db->query("UPDATE oc_task_queue SET enviado = 1 WHERE task_queue_id = '" . (int)$result['task_queue_id'] . "'");
                            } catch (Exception $e) {
                                $this->db->query("UPDATE oc_task_queue SET log_error = '" . $this->db->escape($e->getMessage()) . "', date_log_error = current_timestamp WHERE task_queue_id = '" . (int)$result['task_queue_id'] . "'");
                            }
                        }

                    }

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

                }

            }
        }

    }

}
