<?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"){
		$intDiagnostico = $_REQUEST['intDiagnostico'];
		$intFavorito = $_REQUEST['intFavorito'];
	
		if($_REQUEST['intMode']==1 || $_REQUEST['intMode']==2){
			$txtClave = $_REQUEST['txtClave'];
			$txtDiagnostico = $_REQUEST['txtDiagnostico'];
		}
		
		if($_REQUEST['intMode']==1){
			$query = "INSERT INTO tblcie10 (strClave, strDiagnostico, intFavorito, intEmpresa, intUsuario ) 
			VALUES ('". $txtClave ."','". $txtDiagnostico ."', ". $intFavorito .", 1, 1)";
		
		}else if($_REQUEST['intMode']==2){
			$query = "UPDATE tblcie10 SET strClave = '".$txtClave ."', strDiagnostico = '".$txtDiagnostico ."', intFavorito = ". $intFavorito .", intEmpresa = 1, intUsuario = 1
			WHERE intDiagnostico = ".$intDiagnostico."";
		
		}else if($_REQUEST['intMode']==3){
			$query = "DELETE FROM tblcie10 WHERE intDiagnostico = ".$intDiagnostico."";
			
		}else if($_REQUEST['intMode']==4){
			$query = "UPDATE tblcie10 SET intFavorito = ".$intFavorito ." WHERE intDiagnostico = ".$intDiagnostico."";
			
		}
		//exit($query);
		
		$sJSON = $query;
		
		$sJSON = "";
		
		$queryResult = $mysqli->query($query);	
									
		if($queryResult){
			$sJSON .= "exito";
		}else{
			$sJSON .= "error";
		}
		
	}else if($_REQUEST['method']=="consultar"){
		
		///////////   ///////////   Obtener totales  ///////////   ///////////
		$query = "SELECT count(intDiagnostico) as intCuantos FROM tblcie10 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 strDiagnostico like '".$_REQUEST['strLetra']."%' ";
				}else if(intval($_REQUEST['intCase'])==2){
					$query .= " and strDiagnostico like '%".$_REQUEST['strLetra']."%' ";
//				}else if(intval($_REQUEST['intCase'])==3){
//					$query .= " and strLaboratorio like '%".$_REQUEST['strLetra']."%' ";
				}
//			}	
		}

		/*
		$queryResult = mysqli_query($query);	
		while($data = mysqli_fetch_assoc($queryResult)){
			$totalRows = $data['intCuantos'];
		}
		mysqli_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  ///////////   ///////////
		$query = "SELECT * FROM tblcie10 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 strDiagnostico like '".$_REQUEST['strLetra']."%' ";
				}else if(intval($_REQUEST['intCase'])==2){
					$query .= " and strDiagnostico like '%".$_REQUEST['strLetra']."%' ";
//				}else if(intval($_REQUEST['intCase'])==3){
//					$query .= " and strLaboratorio like '%".$_REQUEST['strLetra']."%' ";
				}
//			}	
		}

		if(isset($_REQUEST['start'])){
			$query .= " Limit ". $_REQUEST['start'] .",".$_REQUEST['limit']."";
		}
		
		//$queryResult = mysqli_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 .= '"intDiagnostico":"'.$data['intDiagnostico'].'"';
					$sJSON .= ',"strDiagnostico":"'.$data['strDiagnostico'].'"';
					$sJSON .= ',"strClave":"'.$data['strClave'].'"';
					$sJSON .= ',"intFavorito":"'.$data['intFavorito'].'"';
					$sJSON .= ',"datFechaAlta":"'.$data['datFechaAlta'].'"';
	
				$sJSON .= "},";
				
			}
		}
		$sJSON = substr($sJSON, 0, -1);
		$sJSON .= "]}";
		
		//mysqli_free_result($queryResult);
		$queryResult->free();

	}
}

print($sJSON);

?>