<?php
include_once("lib/general/cGeneral.php");
include_once("Model/UserAuth.php");
ini_set('display_errors', 1); 

class CentroreportesController extends cGeneral{

    public function get_post(){
        global $General;
        $data = $General->postRequest();
        $response = array();
        #$conexion = $General->getConexion("DS");
        $conexion = $General->getConexionDS(MOD_AMBIENT);

        if($General->checktoken($data["token"])== "OK"){
            $aux =0;
            $sql_post = " SELECT 
                                    post.`id`, post.`idTema`, post.`copy`,
                                    post.`likes`, post.`care`, post.`love`, post.`angry`, post.`sad`,
                                    post.`haha`, post.`wow`, post.`shares`, post.`comment`, 
                                    post.fbid, post.`url`,
                                    post.`fechaCaptura`, post.`fechaMod`, post.`fechaPub`
                                FROM
                                    post 
                                LEFT JOIN temas ON temas.`id` = post.`idTema`
                                WHERE post.`activo` = 1 ";

            if(isset($data["fecha_inicio"]) && !empty($data["fecha_inicio"])){
                $sql_post .= " AND (post.`fechaCaptura` >= '".$data["fecha_inicio"]." 00:00:00'";

                if(isset($data["fecha_fin"]) && !empty($data["fecha_fin"])){
                    $sql_post .= " AND post.`fechaCaptura` <= '".$data["fecha_fin"]." 23:59:59') ";
                }
                else{
                    $sql_post .= " ) ";
                }
    
            }


            #Ingresamos el valor del id_edo
            if(isset($data["id_edo"]) && !empty($data["id_edo"])){
                #Obtengo el ID del edo en Virality
                $homo__id_Virality = $this->get___homo_id($data["id_edo"],"Virality");
                #$isMun = $this->check_ismun($homo__id_Virality);

                $sql_post .= "  AND temas.`idEstado` = ".$homo__id_Virality." ";

            }
            
            
            if(isset($data["order"]) && !empty($data["order"])){
                $sql_post .= " ORDER BY ".$data["order"]." ";
            }        
                                
            if(isset($data["limit"]) && !empty($data["limit"])){
                $sql_post .= " LIMIT ".$data["limit"]." ";
            }
            else{
                    $sql_post .= " LIMIT 100";
                
            }                 
                                
            $sql_post .= "    ;";          

           # echo $sql_post; exit;       
            $resultado = mysqli_query($conexion,$sql_post);
            try{
                if (!$resultado) {
                    throw new Exception("Error de BD, no se pudo consultar la base de datos IDENTIDAD " );
                    $General->closeConexcionDB($conexion);
                    $response["code"] = 301;
                    $response["msj"] = "Error de BD, no se pudo consultar la base de datos ";
                }
                else{
                    $get_row = array();
                    while ($fila =mysqli_fetch_array($resultado, MYSQLI_ASSOC)){
                        $get_row[] =$fila;
                    }

                    $response["code"] = 200;
                    $response["data"] = $get_row;

                    #echo count($get_row);
                }
            }
            catch(Exception $e){
                echo $e->getMessage();
                die();
            }      
           # $General->settoken($data["token"]);        
        }
        else{
            $response['code'] = 301;
            $response['msj']  = "El token ya fue utilizado. Genera un nuevo token para continuar";  
        }

        
        echo json_encode($response,true);
    }
        
    public function get_cat_homologaciones(){
        global $General;
        $data = $General->postRequest();
        $response = array();
        #$conexion = $General->getConexion("");
        $conexion = $General->getConexion(MOD_AMBIENT);

        if($General->checktoken($data["token"])== "OK"){
            $aux =0;
            $sql_homologaciones = "  SELECT nombreCliente, idViz, idVirality, idDSU, idCentralizado,
                                            idGES, idPauta, idSeguridad, idGiras, isMun, activo,
                                            idPadre, idMunPadre, comments FROM homologaciones_clientes; ";
            
           # echo $sql_homologaciones; exit;       
            $resultado = mysqli_query($conexion,$sql_homologaciones);
            try{
                if (!$resultado) {
                    throw new Exception("Error de BD, no se pudo consultar la base de datos IDENTIDAD " );
                    $General->closeConexcionDB($conexion);
                    $response["code"] = 301;
                    $response["msj"] = "Error de BD, no se pudo consultar la base de datos ";
                }
                else{
                    $get_row = array();
                    while ($fila =mysqli_fetch_array($resultado, MYSQLI_ASSOC)){
                        $get_row[] =$fila;
                    }

                    $response["code"] = 200;
                    $response["data"] = $get_row;

                    #echo count($get_row);
                }
            }
            catch(Exception $e){
                echo $e->getMessage();
                die();
            }      
           # $General->settoken($data["token"]);        
        }
        else{
            $response['code'] = 301;
            $response['msj']  = "El token ya fue utilizado. Genera un nuevo token para continuar";  
        }

        
        echo json_encode($response,true);
    }

    public function get___homo_id($id_edo,$plataforma){
        global $General;
        #$conexion = $General->getConexion("");
        $conexion = $General->getConexion(MOD_AMBIENT);
        #Consulto la data de header de reporte
        $sql_homo__id = "SELECT 
                                id".$plataforma."  AS idHomo
                            FROM
                                homologaciones_clientes 
                            WHERE idViz = ".$id_edo." 
                            LIMIT 1 ;
                            ";
                
        $resultado = mysqli_query($conexion,$sql_homo__id);
        
                        

        try{
            if (!$resultado) {
                throw new Exception("Error de BD, no se pudo consultar la base de datos sql_homo__id" );
                $General->closeConexcionDB($conexion);
                $response["code"] = 301;
                $response["msj"]  = "Error de BD, no se pudo consultar la base de datos ";
            }
            else{
                $fila =mysqli_fetch_array($resultado, MYSQLI_ASSOC);                    
            }
        }
        catch(Exception $e){
            echo $e->getMessage();
            die();
        }

        return $fila["idHomo"];
    }

    public function check_ismun_homologacion($id_edo){
        global $General;
        $ConfigMySQLDS = $General->getConexion(""); 

        #Consulto la data de header de reporte
        $sql_checkismun = "select 
                                isMun 
                            from
                                homologaciones_clientes 
                            where idViz = ".$id_edo." 
                                LIMIT 1 ;
                                ";
                
        $resultado = mysqli_query($ConfigMySQLDS,$sql_checkismun);                          

        try{
            if (!$resultado) {
                throw new Exception("Error de BD, no se pudo consultar la base de datos " );
                $General->closeConexcionDB($conexion);
                $response["code"] = 301;
                $response["msj"]  = "Error de BD, no se pudo consultar la base de datos ";
            }
            else{
                $get_row = array();
                while ($fila =mysqli_fetch_array($resultado, MYSQLI_ASSOC)){
                    $get_row[] =$fila;
                }
                
            }
        }
        catch(Exception $e){
            echo $e->getMessage();
            die();
        }

        return $get_row[0]["isMun"];
    }
}
?>


