<?php



include_once "../dbconection.php";



//header('Content-Type: text/html; charset=UTF-8'); 

//header('Content-Type: text/html; charset=ISO-8859-1'); 



$arrUsuarios = array("","Administrador", "Facturacion", "Vendedor");



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

	

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

	

		//$intEmpresa = $_REQUEST['intEmpresa'];

		$intMedicamento = $_REQUEST['intMedicamento'];

		$intFavorito = $_REQUEST['intFavorito'];

		

		if($_REQUEST['intMode']==1 || $_REQUEST['intMode']==2){

			$txtMedicamento = $_REQUEST['txtMedicamento'];

			$txtSustancia = $_REQUEST['txtSustancia'];

			$txtForma = $_REQUEST['selectForma'];

			$txtPresentacion = $_REQUEST['txtPresentacion'];

			$txtLaboratorio = $_REQUEST['selectLaboratorio'];

		}

		

		if($_REQUEST['intMode']==1){

			$query = "INSERT INTO tblmedicamentos (strMedicamento, strSustancia, strForma, strPresentacion, strLaboratorio, intFavorito, intEmpresa, intUsuario ) 

			VALUES ('". $txtMedicamento ."','". $txtSustancia ."','". $txtForma ."','". $txtPresentacion ."','". $txtLaboratorio ."', ". $intFavorito .", 1, 1)";

		

		}else if($_REQUEST['intMode']==2){

			$query = "UPDATE tblmedicamentos SET strMedicamento = '".$txtMedicamento ."', strSustancia = '".$txtSustancia ."', strForma = '".$txtForma ."', 

			strPresentacion = '".$txtPresentacion ."', strLaboratorio = '".$txtLaboratorio ."', intFavorito = ".$intFavorito .", intEmpresa = 1, intUsuario = 1

			WHERE intMedicamento = ".$intMedicamento."";

		

		}else if($_REQUEST['intMode']==3){

			$query = "DELETE FROM tblmedicamentos WHERE intMedicamento = ".$intMedicamento."";

			

		}else if($_REQUEST['intMode']==4){

			$query = "UPDATE tblmedicamentos SET intFavorito = ".$intFavorito ." WHERE intMedicamento = ".$intMedicamento."";

			

		}

		//exit($query);

		

		$sJSON = $query;

		

		$sJSON = "";

		

		//$queryResult = mysql_query($query);	

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



		if($queryResult){

			$sJSON .= "exito";

		}else{

			$sJSON .= "error";

		}

		

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

		

		///////////   ///////////   Obtener totales  ///////////   ///////////

		$query = "SELECT count(intMedicamento) as intCuantos FROM tblmedicamentos WHERE intEmpresa = ". $_REQUEST["intEmpresa"] ;

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



			if($_REQUEST['strLetra']!==""){

//				$query .= " Limit 0,100";

//				$bEntro = true;

//			}else{

				if(intval($_REQUEST['intCase'])==1){

					$query .= " and strMedicamento like '".$_REQUEST['strLetra']."%' ";

				}else if(intval($_REQUEST['intCase'])==2){

					$query .= " and (strSustancia like '%".$_REQUEST['strLetra']."%' ";

					$query .= " or strMedicamento like '%".$_REQUEST['strLetra']."%' )";

				}else if(intval($_REQUEST['intCase'])==3){

					$query .= " and strLaboratorio like '%".$_REQUEST['strLetra']."%' ";

				}

			}



		}

		

		//exit($query);

		/*

		$queryResult = mysql_query($query);	

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

			$totalRows = $data['intCuantos'];

		}

		mysql_free_result($queryResult);

		*/

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

		//$totalRows = $queryResult->num_rows;

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

			$totalRows = $data['intCuantos'];

		}

		//mysqli_free_result($queryResult);

		$queryResult->free();

		

		///////////   ///////////   Obtener datos  ///////////   ///////////

		$bEntro = false;

		$query = "SELECT * FROM tblmedicamentos WHERE intEmpresa = ". $_REQUEST["intEmpresa"] ;

		

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

			//if($_REQUEST['strLetra']=="#"){

			if($_REQUEST['strLetra']===""){

				//$query .= " Limit 0,100";

				$bEntro = true;

			}else{

				if(intval($_REQUEST['intCase'])==1){

					$query .= " and strMedicamento like '".$_REQUEST['strLetra']."%' ";

				

				}else if(intval($_REQUEST['intCase'])==2){

					$query .= " and (strSustancia like '%".$_REQUEST['strLetra']."%' ";

					$query .= " or strMedicamento like '%".$_REQUEST['strLetra']."%' )";



				}else if(intval($_REQUEST['intCase'])==3){

					$query .= " and strLaboratorio like '%".$_REQUEST['strLetra']."%' ";

				}

			}	

		}

		

		$query .= " ORDER BY strMedicamento ASC ";



		if(isset($_REQUEST['start']) && $bEntro == false){

			$query .= " Limit ". $_REQUEST['start'] .",".$_REQUEST['limit']."";

		}

		

		//exit($query);



		//$queryResult = mysql_query($query);	

		//$totalRows = mysql_num_rows($queryResult);



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

		//$totalRows = $queryResult->num_rows;



		$sJSON = '{"totalCount":"'.$totalRows.'","Items":[';

		

		if($totalRows > 0){



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



				$sJSON .= "{";

					$sJSON .= '"intMedicamento":"'.$data['intMedicamento'].'"';

					$sJSON .= ',"strMedicamento":"'.$data['strMedicamento'].'"';

					$sJSON .= ',"strSustancia":"'.$data['strSustancia'].'"';

					$sJSON .= ',"strForma":"'.$data['strForma'].'"';

	

					$sJSON .= ',"strPresentacion":"'.$data['strPresentacion'].'"';

					$sJSON .= ',"strLaboratorio":"'.$data['strLaboratorio'].'"';

					$sJSON .= ',"intFavorito":"'.$data['intFavorito'].'"';

					$sJSON .= ',"datFechaAlta":"'.$data['datFechaAlta'].'"';

	

				$sJSON .= "},";

				

			}



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



		}

		$sJSON .= "]}";



		$queryResult->free();

	}

}



print($sJSON);



?>