<?php

include_once "../dbconection.php";

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

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

		$queryResult = mysql_query($query);	
		while($data = mysql_fetch_assoc($queryResult)){
			$totalRows = $data['intCuantos'];
		}
		mysql_free_result($queryResult);
		
		///////////   ///////////   Obtener datos  ///////////   ///////////
		$query = "SELECT * FROM tblhospitales 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 strHospital like '".$_REQUEST['strLetra']."%' ";
				}else if(intval($_REQUEST['intCase'])==2){
					$query .= " and strHospital 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 = mysql_query($query);	
		//$totalRows = mysql_num_rows($queryResult);
							
		$sJSON = '{"totalCount":"'.$totalRows.'","Items":[';
		
		if($totalRows > 0){

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

				$sJSON .= "{";
					$sJSON .= '"intHospital":"'.$data['intHospital'].'"';
					$sJSON .= ',"strHospital":"'.$data['strHospital'].'"';
					$sJSON .= ',"strClave":"'.$data['strClave'].'"';
					$sJSON .= ',"intFavorito":"'.$data['intFavorito'].'"';
					$sJSON .= ',"datFechaAlta":"'.$data['datFechaAlta'].'"';
	
				$sJSON .= "},";
				
			}
		}
		$sJSON = substr($sJSON, 0, -1);
		$sJSON .= "]}";
		
		mysql_free_result($queryResult);
		
	}
}

print($sJSON);

?>