<?php

header('Content-Type: application/json');

include('../../../php/conn.php');

switch ($_POST["accion"]) {

  case 'obtenerTokensDisponibles':

    // Obtiene la lista de Tokens asignados -----------------
    // ------------------------------------------------------
    $notin = "";

    if ($res = mysqli_query($enlace, "SELECT cod_token FROM tb_usuario_survey WHERE cod_survey = '".$_POST["codigo"]."'"))
    {
      while($row = mysqli_fetch_array($res))
      {
        $notin .= "'".$row["cod_token"]."', ";
      }

      $notin = substr($notin, 0, -2);
    }

    if (strlen($notin) == 0)
    {
      $notin = "''";
    }
    // ------------------------------------------------------

    // if ($res = mysqli_query($enlace, "SELECT COUNT(*) AS numero FROM cela_tokens_".$_POST["codigo"]." WHERE tid NOT IN(SELECT cod_token FROM tb_usuario_survey WHERE cod_survey=".$_POST["codigo"].");")) {

    if ($res = mysqli_query($enlacehost, "SELECT COUNT(*) AS numero FROM cela_tokens_".$_POST["codigo"]." WHERE completed = 'N' AND tid NOT IN(".$notin.");")) {

      if (mysqli_num_rows($res) > 0) {

        while($row = mysqli_fetch_assoc($res)) {

          echo $row["numero"];

        }

      }

    }

    break;

  case 'listarEncuestas':
    $r = array();

    // if ($res = mysqli_query($enlace, "SELECT cod_survey, surveyls_title, COUNT(cod_survey) AS cant FROM tb_usuario_survey AS s INNER JOIN cela_surveys_languagesettings AS l ON s.cod_survey=l.surveyls_survey_id WHERE cod_usuario=".$_POST["usuario"]." GROUP BY cod_survey;")) {

    if ($res = mysqli_query($enlace, "SELECT cod_survey, surveyls_title, COUNT(cod_survey) AS cant FROM tb_usuario_survey AS s INNER JOIN cela_surveys_languagesettings AS l ON s.cod_survey=l.surveyls_survey_id WHERE cod_usuario=".$_POST["usuario"]." GROUP BY cod_survey;")) {
      
      if (mysqli_num_rows($res) > 0) {
        
        while($row = mysqli_fetch_assoc($res)) {
          
          array_push($r, $row);

        }
        
        echo json_encode(array('estado' => 1, 'registros' => $r));

      }

      else{

        echo json_encode(array('estado' => 0));

      }

    }

    break;

  case 'cargaFilter2':
    $r = array();

    // if ($res = mysqli_query($enlace, "SELECT cod_survey, surveyls_title, COUNT(cod_survey) AS cant FROM tb_usuario_survey AS s INNER JOIN cela_surveys_languagesettings AS l ON s.cod_survey=l.surveyls_survey_id WHERE cod_usuario=".$_POST["usuario"]." GROUP BY cod_survey;")) {

    if ($res = mysqli_query($enlace, "SELECT territorio FROM tb_resultados_prueba1 WHERE territorio1 = '".$_POST["filter1"]."'GROUP BY TERRITORIO")) {
      
      if (mysqli_num_rows($res) > 0) {
        
        while($row = mysqli_fetch_assoc($res)) {
          
          array_push($r, $row);

        }
        
        echo json_encode(array('estado' => 1, 'registros' => $r));

      }

      else{

        echo json_encode(array('estado' => 0));

      }

    }

    break;

  case 'cargaMeses':
    $r = array();

    // Cambiando el idioma del servidor
    $query = "SET lc_time_names = 'es_PE';";

    if ($res = mysqli_query($enlace, $query)) {
    }

    // $query = "SELECT MONTH(submitdate) AS cod_mes, MONTHNAME(submitdate) AS nom_mes FROM cela_survey_".$_POST["tabla"]." WHERE submitdate IS NOT NULL AND YEAR(submitdate) = ".$_POST["anho"]." GROUP BY YEAR(submitdate) ORDER BY 1";
    $query = "SELECT T.cod_mes, (SELECT M.nom_mes FROM tb_mes M WHERE M.cod_mes = T.cod_mes) AS nom_mes FROM ".$_POST["tabla"]." T WHERE submitdate IS NOT NULL AND T.cod_anho = ".$_POST["anho"]." GROUP BY T.cod_anho ORDER BY 1";

    // if ($res = mysqli_query($enlacehost, $query)) {
    if ($res = mysqli_query($enlace, $query)) {
      
      if (mysqli_num_rows($res) > 0) {
        
        while($row = mysqli_fetch_assoc($res)) {
          
          array_push($r, $row);

        }
        
        echo json_encode(array('estado' => 1, 'registros' => $r));

      }

      else{

        echo json_encode(array('estado' => 0));

      }

    }

    break;

    case 'echartCabecera':
    $r = array();

    // Cambiando el idioma del servidor
    $query = "SET lc_time_names = 'es_PE';";

    if ($res = mysqli_query($enlace, $query)) {
    }

    $query = "SELECT cod_tipopresentacion, cod_grupoindicador, origen_crucedatos, origen_componente, componente_criteriodatos, presentacion_orden, presentacion_titulo FROM tb_Indicadores_Presentacion WHERE cod_encuesta = '".$_GET["_cod_encuesta"]."' AND presentacion_orden = ".$_GET["orden"]." ORDER BY presentacion_orden";

    if ($res = mysqli_query($enlace, $query)) {
      
      if (mysqli_num_rows($res) > 0) {
        
        while($row = mysqli_fetch_assoc($res)) {
          
          array_push($r, $row);

        }
        
        echo json_encode(array('estado' => 1, 'registros' => $r));

      }

      else{

        echo json_encode(array('estado' => 0));

      }

    }

    break;

  default:

    # code...

    break;

}

?>