<?php
include_once('common.php');
?>
<?php  

	require_once('lib/nusoap.php');
	
	class app_function{

		function __construct(){
			// echo "dsds"; die;
        }

		public function getAppointments()
		{

			// retrieve parameters from url
			$url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
			$url_components = parse_url($url);
			parse_str($url_components['query'], $params);

			//connect to pursuit licensing database
			try {
				$hostname = "51.89.165.204";
				$dbname = "PursuitLicensing";
				$username = "vdsuser";
				$pw = "vdsuser";
				$conn = new PDO ("dblib:host=$hostname;dbname=$dbname","$username","$pw");
			} catch (PDOException $e) {
				echo "Failed to get config connection: " . $e->getMessage() . "\n";
				exit;
			}

			//get id from url
			$optician_id = $params['OpticianId'];

			//query database
			$result = $conn->prepare("SELECT * FROM OpticianDetails WHERE OpticianId = $optician_id;");
			$result->execute();

			//get information from database and use to connect to diary web service
			$row = $result->fetch(PDO::FETCH_LAZY);

			//send info to available-slot.php
			session_start();
            $_SESSION['Site'] = $row['Site'];
			$_SESSION['Password'] = $row['Password'];
			$_SESSION['WebserviceAddress'] = $row['WebserviceAddress'];
                        $_SESSION['OpticianName'] = $row['OpticianName'];
			$_SESSION['OpticianAddress'] = $row['OpticianAddress'];
			$_SESSION['OpticianPhone'] = $row['OpticianPhone'];
			$_SESSION['OpticianLogo'] = $row['OpticianLogo'];


			//connect to webservice
			$client = new nusoap_client($row['WebserviceAddress'],'wsdl');
			$client->soap_defencoding = 'UTF-8';
			$param = array('site'=>$row['Site'],'Password'=>$row['Password'],'SenderIP'=>'62.133.6.123');
			$result = $client->call('GetAppointments',$param);
			return $result['GetAppointmentsResult'];
			
		}
	}

?>

