<?php
namespace src\controllers;

use \core\Controller;
use \src\controllers\UtilsController;
use \src\handlers\FiltersHandler;
use \src\handlers\LoginHandler;
use \src\handlers\PermissionHandler;
use \src\models\Client;
use \src\models\Interaction;
use \src\models\Request;
use \src\models\Salesman;
use \src\models\User;

class InteractionsController extends Controller {
    private $loggedUser;
    private $pageActive;
    private $paramsName;

    public function __construct() {
        date_default_timezone_set('America/Sao_Paulo');
        $this->loggedUser = LoginHandler::checkLogin();

        if($this->loggedUser == false) {
            $this->redirect('/login');
        }

        $this->pageActive = 'crm';
        $this->paramsName = PermissionHandler::getPermissions($this->loggedUser->group);

        if(!in_array('crm_view', $this->paramsName)) {
            $this->redirect('/');
        }        
    }

    public function index() {
        $dateInit = '01-01-'.date('Y');

        $emptyFilter = [
            // 'd_init' => date('d/m/Y', strtotime($dateInit)),
            // 'd_end' => date('d/m/Y', strtotime('+ 15 days', strtotime(date('Y-m-d')))),
            'status' => 1,
            'type_interaction' => '',
            'opt' => '',
            'tag' => '',
            'category' => '',
            'limit' => 50
        ];

        if(isset($_POST['filter']) && !empty($_POST['filter'])) {
            $_SESSION['filter_intections'] = $_POST['filter'];
            if($_SESSION['filter_intections']['clear'] == 'clear') {
                $_SESSION['filter_intections'] = $emptyFilter;
                $this->redirect('/crm');
            }
        }

        if(!isset($_SESSION['filter_intections'])) {
            $_SESSION['filter_intections'] = $emptyFilter;
        }

        $offset = 0;
        $limit = $_SESSION['filter_intections']['limit'];

        $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);
        $filter = FiltersHandler::filterInteractions($_SESSION['filter_intections'], $salesman['id']);

        $conversatitonList = Interaction::getConversations($filter, $salesman['id']);

        foreach ($conversatitonList as $key => $conversatiton) {            
            $conversatitonList[$key]['last_message'] = Interaction::getResult($filter, $conversatiton['id']);

            // Verificar se atende os requisitos de ativa e inativa

            if(!empty($conversatitonList[$key]['last_message'])) {
                if($_SESSION['filter_intections']['status'] == 1) {

                    if(!$conversatitonList[$key]['last_message']['next_interation']) {
                        $conversatitonList[$key]['last_message'] = '';
                    }

                } else if($_SESSION['filter_intections']['status'] == 2) {

                    if($conversatitonList[$key]['last_message']['next_interation']) {
                        $conversatitonList[$key]['last_message'] = '';
                    }
                }
            }

            if(!empty($conversatitonList[$key]['last_message'])) {
                $datesToOrder[$key] = $conversatitonList[$key]['last_message']['next_interation']; // datas para ordenação

                $now = date('Y-m-d');

                if(strtotime($conversatitonList[$key]['last_message']['next_interation']) < strtotime($now)) {
                    $color = 'bg-red';
                } else if(strtotime($conversatitonList[$key]['last_message']['next_interation']) == strtotime($now)) {
                    $color = 'bg-yellow';
                } else {
                    $color = '';
                }

                $conversatitonList[$key]['color'] = $color;

                $conversatitonList[$key]['last_message']['next_interation'] = UtilsController::convertDate($conversatitonList[$key]['last_message']['next_interation']);

                $conversatitonList[$key]['last_message']['salesman_name'] = Salesman::getSalesmanName($conversatitonList[$key]['id_salesman']);

            } else {
                unset($conversatitonList[$key]);
            }
        }

        if(!empty($datesToOrder)) {
            array_multisort($datesToOrder, SORT_ASC, $conversatitonList);
        }

        $message = null;
        $alert = User::getNotification($this->loggedUser->id);

        if($alert) {
            $message['ref'] = $alert['id'];
            $message['text'] = $alert['message'];
            $message['alertType'] = 'alert-success'; 
        }

        $arrayFilter = $_SESSION['filter_intections'];

        $this->render('crm', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'result' => $conversatitonList,
            'arrayInfo' => $arrayFilter,
            'message' => $message,
            'titlePage' => 'CRM - Supporte Agronegócios'
        ]);
    }

    // public function index() {
    //     $dateInit = '01-01-'.date('Y');

    //     $emptyFilter = [
    //         'd_init' => date('d/m/Y', strtotime($dateInit)),
    //         'd_end' => date('d/m/Y', strtotime('+ 15 days', strtotime(date('Y-m-d')))),
    //         'type_interaction' => '',
    //         'opt' => '',
    //         'tag' => '',
    //         'category' => '',
    //         'limit' => 50
    //     ];

    //     if(isset($_POST['filter']) && !empty($_POST['filter'])) {
    //         $_SESSION['filter_intections'] = $_POST['filter'];
    //         if($_SESSION['filter_intections']['clear'] == 'clear') {
    //             $_SESSION['filter_intections'] = $emptyFilter;
    //             $this->redirect('/crm');
    //         }
    //     }

    //     if(!isset($_SESSION['filter_intections'])) {
    //         $_SESSION['filter_intections'] = $emptyFilter;
    //     }

    //     $offset = 0;
    //     $limit = $_SESSION['filter_intections']['limit'];

    //     $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);
    //     $filter = FiltersHandler::filterInteractions($_SESSION['filter_intections'], $salesman['id']);

    //     $this->arrayInfo['result'] = array();

    //     foreach ($filter['clientsList'] as $key => $client) {
            
    //         if($client) {
    //             $data = Interaction::getResult($filter, $client);

    //             if($data) {
    //                 $this->arrayInfo['result'][] = $data;
    //                 // if(!Interaction::verifyExistsNewInteraction($data['id_client'], $data['id'], $data['next_interation'])) {}
    //             }
    //         }
    //     }

    //     foreach ($this->arrayInfo['result'] as $key => $item) {
    //         $datesToOrder[$key] = $item['next_interation'];

    //         $this->arrayInfo['result'][$key]['next_interation'] = UtilsController::convertDate($item['next_interation']);

    //         $now = date('Y-m-d');

    //         if(strtotime($item['next_interation']) < strtotime($now)) {
    //             $color = 'bg-red';
    //         } else if(strtotime($item['next_interation']) == strtotime($now)) {
    //             $color = 'bg-yellow';
    //         } else {
    //             $color = '';
    //         }

    //         $this->arrayInfo['result'][$key]['color'] = $color;
    //     }

    //     if($datesToOrder && $this->arrayInfo['result']) {
    //         array_multisort($datesToOrder, SORT_ASC, $this->arrayInfo['result']);
    //     }

    //     $message = null;
    //     $alert = User::getNotification($this->loggedUser->id);

    //     if($alert) {
    //         $message['ref'] = $alert['id'];
    //         $message['text'] = $alert['message'];
    //         $message['alertType'] = 'alert-success'; 
    //     }        

    //     $this->arrayInfo['filter'] = $_SESSION['filter_intections'];

    //     $this->render('crm', [
    //         'loggedUser' => $this->loggedUser,
    //         'pageActive' => $this->pageActive,
    //         'permissions' => $this->paramsName,
    //         'arrayInfo' => $this->arrayInfo,
    //         'message' => $message,
    //         'titlePage' => 'CRM - Supporte Agronegócios'
    //     ]);
    // }

    public function start() {
        $clientsList = array();
        $lastInteractions = array();
        $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);

        if($salesman) {
            $clientsList = Client::getClientsFromSalesman($salesman['id']);
            // $lastInteractions = Interaction::getLastInteraction($salesman['id'], 2);
        }

        $dateNow = date('d/m/Y');

        $this->render('init_iteration', [
            'loggedUser' => $this->loggedUser,
            'pageActive' => $this->pageActive,
            'permissions' => $this->paramsName,
            'clients' => $clientsList,
            'date' => $dateNow,
            'titlePage' => 'CRM Nova Interação - Supporte Agronegócios'
        ]);
    }

    public function view() {
        $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);

        if($salesman || in_array('crm_administrative', $this->paramsName)) {
            // $client = filter_input(INPUT_GET, 'client');
            // $type = filter_input(INPUT_GET, 'type');
            $conversation = intval(filter_input(INPUT_GET, 'cvt'));

            if($conversation) {
                $filter['collum'] = 'type';
                $filter['simbol'] = '!=';
                $filter['value'] = '';
                $conversationData['info'] = Interaction::getConversation('id', $conversation, $filter);

                if($conversationData['info']['type'] == 1) {
                    $conversationData['client'] = Client::getClient($conversationData['info']['id_client']);
                    $conversationData['type_name'] = '(Cliente)';
                } else {
                    $conversationData['client'] = Interaction::getClientProspection($conversationData['info']['id_client']);
                    $conversationData['type_name'] = '(Prospecção)';
                } 

                $dateNow = date('d/m/Y');

                $this->render('view_iteration', [
                    'loggedUser' => $this->loggedUser,
                    'pageActive' => $this->pageActive,
                    'permissions' => $this->paramsName,
                    'client' => $conversationData,
                    'date' => $dateNow,
                    'titlePage' => 'CRM Interação - Supporte Agronegócios'
                ]);

            } else {
                $this->redirect('/crm');
            }
        } else {
            $this->redirect('/crm');
        }
    }

    // public function view() {
    //     $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);

    //     if($salesman || in_array('crm_administrative', $this->paramsName)) {
    //         $client = filter_input(INPUT_GET, 'client');
    //         $type = filter_input(INPUT_GET, 'type');

    //         if($client && $type) {

    //             if($type == 1) {
    //                 $clientsData = Client::getClient($client);
    //                 $clientsData['type_name'] = '(Cliente)';
    //                 $clientsData['type_ref'] = $type;
    //             } else {
    //                 $clientsData = Interaction::getClientProspection($client);
    //                 $clientsData['type_name'] = '(Prospecção)';
    //                 $clientsData['type_ref'] = $type;
    //             }                

    //             $dateNow = date('d/m/Y');

    //             $this->render('view_iteration', [
    //                 'loggedUser' => $this->loggedUser,
    //                 'pageActive' => $this->pageActive,
    //                 'permissions' => $this->paramsName,
    //                 'client' => $clientsData,
    //                 'date' => $dateNow,
    //                 'titlePage' => 'CRM Interação - Supporte Agronegócios'
    //             ]);
    //         } else {
    //             $this->redirect('/crm');
    //         }
    //     } else {
    //         $this->redirect('/crm');
    //     }
    // }

    public function save() {
        $response = false;
        $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);

        if($salesman) {
            $data = filter_input(INPUT_POST, 'dataInput', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY);

            // Verificar se existe conversa criada para o cliente
            $filter['collum'] = 'type';
            $filter['simbol'] = '=';
            $filter['value'] = intval($data['type']);

            $conversationData = Interaction::getConversation('id_client', $data['client_id'], $filter);

            // Se não existir, criando uma nova
            if(!$conversationData) {
                $crmNew['id_client'] = $data['client_id'];
                $crmNew['id_salesman'] = $salesman['id'];
                $crmNew['type'] = $data['type'];
                $crmNew['category'] = intval($data['category']);

                $crmRef = Interaction::createConversation($crmNew);
            } else {
                $crmRef = $conversationData['id'];
            }
            // Fim Verificar se existe conversa criada para o cliente
            
            $salesmanName = Salesman::getSalesmanName($salesman['id']);
            
            $arrayData['id_crm'] = $crmRef;
            $arrayData['type'] = $data['type'];
            $arrayData['id_salesman'] = $salesman['id'];
            $arrayData['date_created'] = date('Y-m-d H:i:s');
            $arrayData['next_interation'] = ($data['next_date'])?$data['next_date']:null;
            $arrayData['conversation'] = $data['text'];
            $arrayData['salesman_name'] = $salesmanName;
            $arrayData['category'] = intval($data['category']);

            if(intval($data['type'] == 1)) {
                $client = Client::getClient($data['client_id']);    

                $arrayData['id_client'] = $data['client_id'];
                $arrayData['client_name'] = $client['name'];
                $arrayData['client_email'] = $client['email'];
                $arrayData['client_phone'] = $client['first_phone'];

            } else if(intval($data['type'] == 2)) {

                if($data['client_id']) {

                    $dataClient = Interaction::getClientProspection($data['client_id']);

                    $arrayData['id_client'] = $dataClient['id'];
                    $arrayData['client_name'] = $dataClient['name'];
                    $arrayData['client_email'] = $dataClient['email'];
                    $arrayData['client_phone'] = $dataClient['phone'];

                } else {
                    $arrayProspection['name'] = $data['client_name'];
                    $arrayProspection['email'] = $data['client_email'];
                    $arrayProspection['phone'] = $data['client_phone'];
                    $arrayProspection['category'] = $arrayData['category'];

                    $prospection = Interaction::saveClientProspection($arrayProspection);

                    $arrayData['id_client'] = $prospection;
                    $arrayData['client_name'] = $data['client_name'];
                    $arrayData['client_email'] = $data['client_email'];
                    $arrayData['client_phone'] = $data['client_phone'];
                }
            }

            Interaction::save($arrayData);
            $response = true;
        }

        echo json_encode($response);
    }

    public function saveInEdit() {
        $response = false;
        $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);

        if($salesman) {
            $conversation = intval(filter_input(INPUT_POST, 'ref'));
            $text = filter_input(INPUT_POST, 'text');
            $next_date = filter_input(INPUT_POST, 'date');

            // Verificar se existe conversa criada para o cliente
            $filter['collum'] = 'type';
            $filter['simbol'] = '!=';
            $filter['value'] = '';

            $conversationData = Interaction::getConversation('id', $conversation, $filter);

            if($conversationData) {
                $salesmanName = Salesman::getSalesmanName($salesman['id']);
            
                $arrayData['id_crm'] = $conversation;
                $arrayData['type'] = $conversationData['type'];
                $arrayData['id_salesman'] = $salesman['id'];
                $arrayData['date_created'] = date('Y-m-d H:i:s');
                $arrayData['next_interation'] = ($next_date)?$next_date:null;
                $arrayData['conversation'] = $text;
                $arrayData['salesman_name'] = $salesmanName;
                $arrayData['category'] = $conversationData['category'];

                if(intval($conversationData['type'] == 1)) {
                    $client = Client::getClient($conversationData['id_client']);    

                    $arrayData['id_client'] = $conversationData['id_client'];
                    $arrayData['client_name'] = $client['name'];
                    $arrayData['client_email'] = $client['email'];
                    $arrayData['client_phone'] = $client['first_phone'];

                } else if(intval($conversationData['type'] == 2)) {

                    $dataClient = Interaction::getClientProspection($conversationData['id_client']);

                    $arrayData['id_client'] = $dataClient['id'];
                    $arrayData['client_name'] = $dataClient['name'];
                    $arrayData['client_email'] = $dataClient['email'];
                    $arrayData['client_phone'] = $dataClient['phone'];
                }
            }

            Interaction::save($arrayData);
            $response = true;
        }

        echo json_encode($response);
    }

    public function verifyDate() {
        $response = false;
        $date = filter_input(INPUT_POST, 'date');

        if($date) {
            $date = UtilsController::convertDate($date);
            $response = UtilsController::verifyFutureDate($date);
        }

        echo json_encode($response);
    }

    public function searchRequest() {
        $text = filter_input(INPUT_POST, 'text');
        $response = '';

        if($text) {
            $result = Request::getRequestsById($text, 10);

            if($result) {
                $response = '<ul>';
                
                foreach($result as $item) {
                    $response .= '<li data-list="'.$item['id'].'">';
                    $response .= $item['id'].' - '.Client::getName($item['id_client']);
                    $response .= '</li>';
                }

                $response .= "</ul>";
            }            
        }        
        echo json_encode($response);
    }

    public function getClientsProspections() {
        $response = '';
        
        $result = Interaction::getClientsProspection();

        if($result) {
            $response = '<option value="">Selecionar</option>';
            
            foreach($result as $item) {
                $response .= '<option value="'.$item['id'].'">';
                $response .= $item['name'];
                $response .= '</option>';
            }
        }            
              
        echo json_encode($response);
    }

    // Buscar ultimas interações da conversa selecionada (Edição)
    public function getLastInConversations() {
        $conversation = filter_input(INPUT_POST, 'crm');
        $limit = filter_input(INPUT_POST, 'limit'); // Está vindo na requisição js
        $filter['collum'] = 'type';
        $filter['simbol'] = '!=';
        $filter['value'] = '';

        $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);
        $conversationData = Interaction::getConversation('id', $conversation, $filter);

        if($conversationData['type'] == 1) {
            $clientData = Client::getClient($conversationData['id_client']);
        } else {
            $clientData = Interaction::getInteractionByClient($conversationData['id_client']);
        }

        if($clientData['id_salesman'] != $salesman['id']) {

            if(in_array('crm_administrative', $this->paramsName)) {
                $salesman['id'] = $clientData['id_salesman'];
            }
        }

        if($conversation) {
            $result = Interaction::getLastInteraction($conversation, $limit);

            if($result) {
                $response = '';
                
                foreach($result as $item) {
                    $response .= '<li>';
                    $response .= '<span><strong>Vendedor: </strong>'.$item['salesman_name'].'</span>';
                    $response .= '<span><strong>Cliente: </strong>'.$item['client_name'].'</span>';
                    $response .= '<span>'.str_replace('\n', '<br>', $item['conversation']).'</span>';
                    $response .= '<span class="fixed-date">'.date('d/m/Y H:i:s', strtotime($item['date_created'])).'</span>';
                    $response .= '</li>';
                }
            }
        }

        echo json_encode($response);
    }

    // Buscar ultimas interações da conversa selecionada (Criação)
    public function getLastConversationsByClient() {
        $client = filter_input(INPUT_POST, 'client');
        $type = filter_input(INPUT_POST, 'type');
        $limit = filter_input(INPUT_POST, 'limit'); // Está vindo na requisição js
        $response = '';

        $filter['collum'] = 'type';
        $filter['simbol'] = '=';
        $filter['value'] = $type;

        $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);
        $conversationData = Interaction::getConversation('id_client', $client, $filter);

        if($type == 1) {
            $clientData = Client::getClient($client);
        } else {
            $clientData = Interaction::getInteractionByClient($client);
        }

        if($conversationData) {
            if($clientData['id_salesman'] != $salesman['id']) {

                if(in_array('crm_administrative', $this->paramsName)) {
                    $salesman['id'] = $clientData['id_salesman'];
                }
            }

            if($conversationData['id']) {
                $result = Interaction::getLastInteraction($conversationData['id'], $limit);

                if($result) {
                    
                    foreach($result as $item) {
                        $response .= '<li>';
                        $response .= '<span><strong>Vendedor: </strong>'.$item['salesman_name'].'</span>';
                        $response .= '<span><strong>Cliente: </strong>'.$item['client_name'].'</span>';
                        $response .= '<span>'.str_replace('\n', '<br>', $item['conversation']).'</span>';
                        $response .= '<span class="fixed-date">'.date('d/m/Y H:i:s', strtotime($item['date_created'])).'</span>';
                        $response .= '</li>';
                    }
                }
            }
        }

        $data['list'] = $response;

        if($conversationData) {
            $data['category'] = $conversationData['category'];
        } else {
            $data['category'] = $clientData['category'];
        }

        echo json_encode($data);
    }

    // Buscar ultimas interações do cliente selecionado
    // public function getLastInteractions() {
    //     $client = filter_input(INPUT_POST, 'client');
    //     $type = filter_input(INPUT_POST, 'typec');
    //     $limit = filter_input(INPUT_POST, 'limit'); // Está vindo na requisição js
    //     $response = '';

    //     $salesman = Salesman::getSalesmanByInteration($this->loggedUser->email);

    //     if($type == 1) {
    //         $clientData = Client::getClient($client);
    //     } else {
    //         $clientData = Interaction::getInteractionByClient($client);
    //     }

    //     // echo json_encode($clientData);exit;

    //     if($clientData['id_salesman'] != $salesman['id']) {

    //         if(in_array('crm_administrative', $this->paramsName)) {
    //             $salesman['id'] = $clientData['id_salesman'];
    //         }
    //     }

    //     if($client && $salesman) {
    //         $result = Interaction::getLastInteraction($salesman['id'], $client, $type, $limit);

    //         if($result) {
    //             $response = '';
                
    //             foreach($result as $item) {
    //                 $response .= '<li>';
    //                 $response .= '<span><strong>Vendedor: </strong>'.$item['salesman_name'].'</span>';
    //                 $response .= '<span><strong>Cliente: </strong>'.$item['client_name'].'</span>';
    //                 $response .= '<span>'.str_replace('\n', '<br>', $item['conversation']).'</span>';
    //                 $response .= '<span class="fixed-date">'.date('d/m/Y H:i:s', strtotime($item['date_created'])).'</span>';
    //                 $response .= '</li>';
    //             }
    //         }
    //     }

    //     echo json_encode($response);
    // }

    // CRM ADMINISTRATIVO
    public function crm() {
        if(in_array('crm_administrative', $this->paramsName)) {            
            // $dateInit = '01-01-'.date('Y');

            $emptyFilter = [
                // 'd_init' => date('d/m/Y', strtotime($dateInit)),
                // 'd_end' => date('d/m/Y', strtotime('+ 15 days', strtotime(date('Y-m-d')))),
                'status' => 1,
                'type_interaction' => '',
                'opt' => '',
                'tag' => '',
                'category' => '',
                'salesman' => '',
                'limit' => 50
            ];

            if(isset($_POST['filter']) && !empty($_POST['filter'])) {
                $_SESSION['filter_intections'] = $_POST['filter'];
                if($_SESSION['filter_intections']['clear'] == 'clear') {
                    $_SESSION['filter_intections'] = $emptyFilter;
                    $this->redirect('/crm');
                }
            }

            if(!isset($_SESSION['filter_intections'])) {
                $_SESSION['filter_intections'] = $emptyFilter;
            }

            $offset = 0;
            $limit = $_SESSION['filter_intections']['limit'];

            $salesmanList = Salesman::getAllActive();
            $filter = FiltersHandler::filterInteractionsAdministrative($_SESSION['filter_intections']);

            $conversatitonList = Interaction::getConversationsAdmin($filter);

            foreach ($conversatitonList as $key => $conversatiton) {
                
                $conversatitonList[$key]['last_message'] = Interaction::getResult($filter, $conversatiton['id']);

                // Verificar se atende os requisitos de ativa e inativa

                if(!empty($conversatitonList[$key]['last_message'])) {
                    if($_SESSION['filter_intections']['status'] == 1) {

                        if(!$conversatitonList[$key]['last_message']['next_interation']) {
                            $conversatitonList[$key]['last_message'] = '';
                        }

                    } else if($_SESSION['filter_intections']['status'] == 2) {

                        if($conversatitonList[$key]['last_message']['next_interation']) {
                            $conversatitonList[$key]['last_message'] = '';
                        }
                    }
                }

                if(!empty($conversatitonList[$key]['last_message'])) {
                    $datesToOrder[$key] = $conversatitonList[$key]['last_message']['next_interation']; // datas para ordenação

                    $now = date('Y-m-d');

                    if(strtotime($conversatitonList[$key]['last_message']['next_interation']) < strtotime($now)) {
                        $color = 'bg-red';
                    } else if(strtotime($conversatitonList[$key]['last_message']['next_interation']) == strtotime($now)) {
                        $color = 'bg-yellow';
                    } else {
                        $color = '';
                    }

                    $conversatitonList[$key]['color'] = $color;

                    $conversatitonList[$key]['last_message']['next_interation'] = UtilsController::convertDate($conversatitonList[$key]['last_message']['next_interation']);

                    $conversatitonList[$key]['last_message']['salesman_name'] = Salesman::getSalesmanName($conversatitonList[$key]['id_salesman']);

                } else {
                    unset($conversatitonList[$key]);
                }
            }

            if(!empty($datesToOrder)) {
                array_multisort($datesToOrder, SORT_ASC, $conversatitonList);
            }

            $arrayFilter = $_SESSION['filter_intections'];

            $this->render('crm_admin', [
                'loggedUser' => $this->loggedUser,
                'pageActive' => 'crm_administrative',
                'permissions' => $this->paramsName,
                'result' => $conversatitonList,
                'arrayInfo' => $arrayFilter,
                'salesman' => $salesmanList,
                'titlePage' => 'CRM Administrativo - Supporte Agronegócios'
            ]);

        } else {
            $this->redirect('/');
        }        
    }


    public function clearFilter() {
        unset($_SESSION['filter_intections']);
        echo json_encode(true);
    }
}