<?php

include_once "../dbconection.php";
include_once "../funciones.php";
include_once "../errorhandler.php";

set_time_limit ( 30 ); 

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

$method = $_REQUEST['method'];

if($method=="getConsultasPendientes"){
	$sJSON = '{"Items":[';

	$intCaso = intval( $_REQUEST['intCaso'] );
	$intMedico = $_REQUEST['intMedico'];
	$intEstatus = $_REQUEST['intEstatus'];
	$intEmpresa = $_REQUEST['intEmpresa'];
	
	if($intMedico!=""){	

        //SELECT t1.*, CONCAT(t2.strNombres," ",t2.strApellidos) as strCliente FROM tblconsultas t1 INNER JOIN tblclientes2 t2 ON (t1.intCliente=t2.intCliente) WHERE t1.intMedico = 120 ORDER BY t1.intIdAut ASC

		$sqlQuery = "SELECT t1.*, CONCAT(t2.strNombres,' ',t2.strApellidos) as strCliente, t3.intConsultorio  
		FROM tblconsultas t1 INNER JOIN tblclientes2 t2 ON (t1.intCliente=t2.intCliente)
		INNER JOIN tblusuarios t3 ON (t1.intMedico=t3.intUsuario)
		WHERE t1.intEmpresa = ". $intEmpresa ;
		if($intCaso===1){
		    $sqlQuery .= " AND t1.intMedico = ". $intMedico ;
		}
		
		//$sqlQuery .= " AND t1.intEstatus = ". $intEstatus ;
		$sqlQuery .= " AND t1.intEstatus < 3" ;
		
		//$sqlQuery .= " ORDER BY t1.intIdAut ASC";			
		//$sqlQuery .= " ORDER BY t1.intEstatus DESC";			
		$sqlQuery .= " ORDER BY t1.intEstatus DESC, t1.intIdAut ASC";			

		//exit($sqlQuery);
		
		$queryResult = $mysqli->query($sqlQuery);	
		$totalRows = $queryResult->num_rows;
				
		//$sJSON = '{"Expediente":[';

		if($totalRows>0){
	   		
			while ($row = mysqli_fetch_array($queryResult)) {
				
				$sJSON .= "{";
				$intIdAut = $row["intIdAut"];
				$intEstatus = $row["intEstatus"];
				$intCliente = $row["intCliente"];
				$strCliente = $row["strCliente"];
				$strIdConsulta = $row["strIdConsulta"];
				$intConsultorio = $row["intConsultorio"];
				$datFecha = $row["datFecha"];

				$sJSON .= '"intIdAut":"'. $intIdAut .'"';
				$sJSON .= ',"intEstatus":"'. $intEstatus .'"';
				$sJSON .= ',"intCliente":"'. $intCliente .'"';
				$sJSON .= ',"strCliente":"'. $strCliente .'"';
				$sJSON .= ',"strIdConsulta":"'. $strIdConsulta .'"';
				$sJSON .= ',"intConsultorio":"'. $intConsultorio .'"';
				$sJSON .= ',"datFecha":"'. $datFecha .'"';
				
				$sJSON .= "},";		
			}			
	
			$sJSON = substr($sJSON, 0, -1);

		}

		$sJSON .= "]}";
			
		print($sJSON);
		
	}else{
		print("El envio del id del medico no puede ser nulo.");
	}

}

?>