<?php



include_once "../dbconection.php";

include_once "../funciones.php";

include_once "../errorhandler.php";



//header('Content-Type: text/plain; charset=iso-8859-1');



if(isset($_REQUEST['method'])){ // Si es llamado por intUsuario

	

	if($_REQUEST['method']=="paises"){

		$query = "SELECT name as label, code as id FROM country WHERE CODE IN('MEX')";

		//SELECT * FROM `country` WHERE CODE IN('MEX','USA','ARG')

	}else if($_REQUEST['method']=="estados"){

		$query = "SELECT distinct district as label, district as id FROM city WHERE CountryCode='". $_REQUEST['pais'] ."' ";

	}else if($_REQUEST['method']=="ciudades"){

		$query = "SELECT name as label, name as id FROM city WHERE District='". $_REQUEST['estado'] ."' and CountryCode='". $_REQUEST['pais'] ."' ";

	}else if($_REQUEST['method']=="lugares"){

		$query = "SELECT strTitulo as label, intLugarEvento as id FROM tbllugareventos WHERE intEmpresa=". $_REQUEST['intEmpresa'];

	}else if($_REQUEST['method']=="tipos"){

		$query = "SELECT strTitulo as label, intTipoEvento as id FROM tbltipoeventos WHERE intEmpresa=". $_REQUEST['intEmpresa'];

	}else if($_REQUEST['method']=="tiposusuarios"){

		$query = "SELECT strTitulo as label, intTipoUsuario as id FROM tbltipousuarios WHERE intEmpresa=". $_REQUEST['intEmpresa'];

	}else if($_REQUEST['method']=="usuarios"){

		$query = "SELECT strNombre as label, intUsuario as id FROM tblusuarios WHERE intEmpresa=". $_REQUEST['intEmpresa'];



	}else if($_REQUEST['method']=="clientes"){



		$query = "SELECT concat_ws(' ', strNombres, strApellidos) as label, intCliente as id FROM tblclientes2 WHERE 1=1";



		if(isset($_REQUEST['q'])){

			$query .= " and (strNombres like '%". $_REQUEST['q'] ."%' OR strApellidos like '%". $_REQUEST['q'] ."%')";

		}



	}else if($_REQUEST['method']=="productos"){

		$query = "SELECT strNombre as label, intProducto as id FROM tblproductos WHERE intEmpresa=". $_REQUEST['intEmpresa'];

	}else if($_REQUEST['method']=="productoscategorias"){

		$query = "SELECT strTitulo as label, intCategoria as id FROM tblproductoscategorias WHERE bitVisible=1 and intEmpresa=". $_REQUEST['intEmpresa'];

	}else if($_REQUEST['method']=="medicamentos"){

		//$query = "SELECT strMedicamento as label, intMedicamento as id FROM tblmedicamentos WHERE intEmpresa=". $_REQUEST['intEmpresa']. " Limit 0,50";

		$query = "SELECT concat_ws(' - ', strMedicamento, strSustancia, strPresentacion) as label, intMedicamento as id FROM tblmedicamentos WHERE intEmpresa=". $_REQUEST['intEmpresa'];

		if(isset($_REQUEST['q'])){

			$query .= " and (strMedicamento like '". $_REQUEST['q'] ."%' or strSustancia like '%". $_REQUEST['q'] ."%')";

		}



	}else if($_REQUEST['method']=="diagnosticos"){

		$query = "SELECT strDiagnostico as label, strClave as id FROM tblcie10 WHERE intEmpresa=". $_REQUEST['intEmpresa'] ;

		if(isset($_REQUEST['q'])){

			$query .= " and strDiagnostico like '". $_REQUEST['q'] ."%'";

		}

	}else if($_REQUEST['method']=="laboratorios"){

		$query = "SELECT distinct strLaboratorio as label, strLaboratorio as id FROM tblmedicamentos WHERE intEmpresa=". $_REQUEST['intEmpresa']  ;

		//if(isset($_REQUEST['q'])){

//			$query .= " and strEnfermedad like '". $_REQUEST['q'] ."%'";

//		}



		$query .= " ORDER BY strLaboratorio ASC";



	}else if($_REQUEST['method']=="formas"){

		$query = "SELECT distinct strForma as label, strForma as id FROM tblmedicamentos WHERE intEmpresa=". $_REQUEST['intEmpresa'] ;

		$query .= " ORDER BY strForma ASC";

	

	}

	

	//exit($query);

	/*

	$queryResult = mysql_query($query);	

	$totalRows = mysql_num_rows($queryResult);

	*/



	$queryResult = $mysqli->query($query);

	$totalRows = $queryResult->num_rows;



	$sJSON = '{"Items":[';

	

	if($totalRows > 0){

		

		//while($data = mysql_fetch_assoc($queryResult)){}

		while($data = mysqli_fetch_assoc($queryResult)){



			//$id = limpiar_caracteres_especiales($data['id']);

			$id = ($data['id']);

			$label = ($data['label']);

			$label = str_replace("                        ", " ", $label);



			$sJSON .= "{";

				$sJSON .= '"id":"'.$id.'"';

				$sJSON .= ',"label":"'.$label.'"';

			$sJSON .= "},";

		}

	

		$sJSON = substr($sJSON, 0, -1);



	}

	$sJSON .= "]}";

	

}



print($sJSON);



?>