<?php
require '../../Conexion/class.oracle.php';
require '../../Backend/Core/UserSystem.php';
include_once '../../Backend/Util/Logger.php';
//$db = new EyeMySQLAdap();
session_start();
$connection = new PDOConnection();
$userSystem = new UserSystem($connection,-1);

$UserID = $_SESSION["UsuarioID"];

try {

	//if (true)
	if (isset($_POST['Filsc']))
	{
		$varfil= $_POST['Filsc'];

		//$varfil= 'Ciudad';
		$arr = array();

		switch ($varfil)
		{
			case "Mensajes":{

				$sql =  "SELECT MessageID, Description,CreateDate,Type ,Criticity, Status FROM message WHERE Status =1 AND UserID=" . $UserID;
				break;
				}

			case 'Provincia':{
					if(isset($_POST['IncludeAll']))
						$arr[]=array("ProvinceID"=>"-1","Description"=>"Todos");
					$sql = "select ProvinceID, Description from province";
				}
				break;
			case 'Canton':{
					$ProvinciaID = isset($_POST['ProvinciaID'])?$_POST['ProvinciaID']:-1 ;
					if(isset($_POST['IncludeAll']))
						$arr[]=array("CantonID"=>"-1","Description"=>"Todos");
					$sql = "select CantonID, Description from canton WHERE ProvinceID=" . $ProvinciaID;
				}
				break;
			case 'Parroquia':{
					$CantonID = isset( $_POST['CantonID'])? $_POST['CantonID'] : -1;
					if(isset($_POST['IncludeAll']))
						$arr[]=array("ParroquiaID"=>"-1","Description"=>"Todos");
					$sql = "select ParroquiaID, Description from parroquia WHERE CantonID=" . $CantonID;
				}
				break;

			case'Usuarios':{
					$sql = "select UserSystemID, Name|| ' ' || LastName  as Nombre, IdentityNumber, Ballot, Title, Address, Telephone, mail, Movil, Username, Password, CityID, CustomerID, CreateUser, CreateDate, UpdateUser, UpdateDate, ProfileID, Status, CityRestricted, AproverLevel   from usersystem";
				}
				break;

			case'Garantias':{
				$InspectionID = $_POST['InspectionID'];
					$sql = "select w.WarrantyID, w.WarrantyTypeID, t.Description as TipoGarantia, if(w.DateInit='1900-01-01','',date_format(w.DateInit,'%d/%m/%Y'))FechaInicio, if(w.DateEnd='1900-01-01','',date_format(w.DateEnd,'%d/%m/%Y'))FechaFin, w.Observations from warranty w, warrantytype t where w.WarrantyTypeID = t.WarrantyTypeID AND w.InspectionID = " . $InspectionID;
				}
				break;
			////////:::::::::::::::::::::::::::::::::::::::::::::::::::::::::://////////////////


		}
		//$fmt = new IntlDateFormatter( "es-ES", IntlDateFormatter::FULL, IntlDateFormatter::FULL, 'America/Bogota', IntlDateFormatter::GREGORIAN, "yyyyMMdd");

		Logger::info("This is message");
		Logger::debug($_POST['Filsc'] . ' -> [' . $sql  . ']');
		Logger::debug($sql);
		//$result=$db->query($sql);
	//$result = $connection->select($strSQL);
		//Logger::writeLog();
		$result =$connection->select($sql);
		if ($result) {
			foreach ($result as $row) {
                $arr[] = $row;
			}
        }
		/*while($obj = $db->fetchAssoc($result))
		{
			$arr[] = $obj;
		}*/
		$rows= count($arr);
		Echo '({success:true,"totalcount":"'.$rows.'","Data":'.json_encode($arr,JSON_INVALID_UTF8_IGNORE).'})';

	}
	else
	{
		Echo '({success:false,Datos:'.json_encode("Error al consultar...Por Favor Verifique!!!").'})';
	}
}
catch (Exception $e)
{
	Echo '({success:false,Datos:'.json_encode($e->getMessage()).'})';
}

?>
	
