<?php

include_once "../dbconection.php";

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

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

	$intEmpresa = $_REQUEST['intEmpresa'];
	
	$intHospital = $_REQUEST['intHospital'];
	$txtNombre = $_REQUEST['txtHospital'];
	
	$selectPais = $_REQUEST['selectPais'];
	$selectEstado = $_REQUEST['selectEstado'];
	$selectMunicipio = $_REQUEST['selectMunicipio'];
	$txtColonia = $_REQUEST['txtColonia'];
	$txtCalle = $_REQUEST['txtCalle'];
	$txtNumExt = $_REQUEST['txtNumExt'];
	$txtNumInt = $_REQUEST['txtNumInt'];
	$txtCodigoPostal = $_REQUEST['txtCodigoPostal'];
	if($txtCodigoPostal==""){
		$txtCodigoPostal = 'NULL';	
	}
	$txtTelefono = $_REQUEST['txtTelefono'];
	
	if($_REQUEST['intMode']==1){
		$query = "INSERT INTO tblhospitales (strNombre,
		strPais, strEstado, strMunicipio, intCodigoPostal, 
		strColonia, strCalle, strNumExt, strNumInt ,
		strTelefono, intEmpresa ) 
		VALUES ('". $txtNombre ."',
				'". $selectPais ."','". $selectEstado ."','". $selectMunicipio ."',". $txtCodigoPostal .",
				'". $txtColonia ."','". $txtCalle ."','". $txtNumExt ."','". $txtNumInt ."',
				'". $txtTelefono ."',". $intEmpresa .")";
	
	}else if($_REQUEST['intMode']==2){
		$query = "UPDATE tblhospitales SET strNombre = '".$txtNombre ."',
		strPais = '".$selectPais."', strEstado = '".$selectEstado."', strMunicipio = '".$selectMunicipio."', intCodigoPostal = ".$txtCodigoPostal.",
		strColonia = '".$txtColonia."', strCalle = '".$txtCalle."', strNumExt = '".$txtNumExt."', strNumInt = '".$txtNumInt."',
		strTelefono = '".$txtTelefono."'
		WHERE intHospital = ".$intHospital."";
	
	}else if($_REQUEST['intMode']==3){
		$query = "DELETE FROM tblhospitales WHERE intHospital = ".$intHospital."";
		
	}
	
	$sJSON = $query;
	//exit($sJSON);
	
	$sJSON = "";
	
	//$queryResult = mysql_query($query);	
	$queryResult = $mysqli->query($query);
					
	//$sJSON = "";
	
	if($queryResult){
		$sJSON .= "exito";
	}else{
		$sJSON .= "error";
	}
	
}else if(isset($_REQUEST['intEmpresa'])){ // Si es llamado por intEmpresa
	
	$query = "SELECT * FROM tblhospitales WHERE intEmpresa = ". $_REQUEST["intEmpresa"];
	if(intval($_REQUEST['intCase'])==1){
		if(isset($_REQUEST['strLetra'])){
			if($_REQUEST['strLetra']!=""){
				$query .= " and strNombre like '".$_REQUEST['strLetra']."%' ";
			}
		}
	}else if(intval($_REQUEST['intCase'])==2){
		if(isset($_REQUEST['strLetra'])){
			if($_REQUEST['strLetra']!=""){
				$query .= " and (strNombre like '%".$_REQUEST['strLetra']."%') ";
			}
		}
	}
	
	//$queryResult = mysql_query($query);	
	//$totalRows = mysql_num_rows($queryResult);
	$queryResult = $mysqli->query($query);
	$totalRows = $queryResult->num_rows;

	$sJSON = '{"Items":[';
	
	if($totalRows > 0){
		
		while($data = mysqli_fetch_assoc($queryResult)){
			$intHospital = $data['intHospital'];
			$strNombre = $data['strNombre'];

			$strPais = $data['strPais'];	
			$strEstado = $data['strEstado'];	
			$strMunicipio = $data['strMunicipio'];	
			$intCodigoPostal = $data['intCodigoPostal'];	
			$strColonia = $data['strColonia'];	
			$strCalle = $data['strCalle'];	
			$strNumInt = $data['strNumInt'];	
			$strNumExt = $data['strNumExt'];	

			$strTelefono = $data["strTelefono"];

			$sJSON .= "{";
				$sJSON .= '"intHospital":"'.$intHospital.'"';
				$sJSON .= ',"strNombre":"'.$strNombre.'"';
				
				$sJSON .= ',"id":"'.$intHospital.'"';
				$sJSON .= ',"label":"'.$strNombre.'"';
			
				$sJSON .= ',"strPais":"'.$strPais.'"';
				$sJSON .= ',"strEstado":"'.$strEstado.'"';
				$sJSON .= ',"strMunicipio":"'.$strMunicipio.'"';
				$sJSON .= ',"intCodigoPostal":"'.$intCodigoPostal.'"';
				$sJSON .= ',"strColonia":"'.$strColonia.'"';
				$sJSON .= ',"strCalle":"'.$strCalle.'"';
				$sJSON .= ',"strNumInt":"'.$strNumInt.'"';
				$sJSON .= ',"strNumExt":"'.$strNumExt.'"';

				$sJSON .= ',"strTelefono":"'.$strTelefono.'"';
			$sJSON .= "},";
		}
	
		$sJSON = substr($sJSON, 0, -1);

	}
	$sJSON .= "]}";

	$queryResult->free();

}

print($sJSON);

?>