<?php
	function visit_oid( $vid ) {
		$v = new Visit( $vid );
		return $v->oid;
	}
	
	
	/**
	 * Visit search wrapper (usually called from remote.php)  
	 * 
	 * @param $query string to search
	 * @link http://
	 * @author Old Auntie <oldauntie@oldauntie.org>
	 * @version 1.0
	 */
	function visit_search( $section, $query ){
		
		if( empty ( $section ) == false ){
			if( $section == 'reason' ){
				$sql = "SELECT id, term_name as value
							FROM venom_codes
							WHERE label = 'Reason for visit'
							AND term_name like '%{$query}%'
								ORDER BY term_name ASC";
				
			}
			
			if( $section == 'diagnosis' ){
				$sql = "SELECT id, term_name as value
							FROM venom_codes
							WHERE label = 'Diagnosis'
							AND term_name like '%{$query}%'
								ORDER BY term_name ASC";
			}
			
			if( $section == 'diagnostic' ){
				$sql = "SELECT id, term_name as value
							FROM venom_codes
							WHERE label = 'Diagnostic test'
							AND term_name like '%{$query}%'
								ORDER BY term_name ASC";
			}
			
			if( $section == 'drug' ){
				$sql = "SELECT did as id, concat(name, ' - ', pharmaceutical_form) as value
							FROM drugs
							WHERE country_id = '" . _DEFAULT_COUNTRY_ID . "'
							AND name like '%{$query}%'
								ORDER BY name ASC";
			}
			
			if( $section == 'procedure' ){
				$sql = "SELECT id, term_name as value
							FROM venom_codes
							WHERE label = 'Procedure'
							AND term_name like '%{$query}%'
								ORDER BY term_name ASC";
				
								
			}
			
		}else{
			// visit search goes here
			$sql = "SELECT 
						vid, 
						vid as value 
					FROM visit_head 
					WHERE 
						vid = '{$query}'";
			
		}
		
		$con = new Connection();
		$rv = $con->execute($sql);
		
		return $rv;
	}
	
	/**
	 * Visit detail wrapper (usually called from remote.php)
	 * 
	 * @param $query string to search
	 * @link http://www.oldauntie.org
	 * @author Maggie Marple <oldauntie@oldauntie.org>
	 * @version 1.0
	 */
	function visit_get( $vid ){
		$output = new StdClass;
		
		if( empty( $vid ) == false && is_numeric( $vid ) ){
			$oVisit = new Visit ( $vid );
			
			return $oVisit;
		}
	}

	
	function visit_save( $json_visit ){
		// return new visit(1);
		
		$v = new Visit( $json_visit->vid );
		$v->pid				= $json_visit->pid;
		$v->oid				= $json_visit->oid;
		$v->uid				= $_SESSION['uid'];
		$v->reason_id		= $json_visit->reason_id;
		$v->diagnosis_id 	= $json_visit->diagnosis_id;
		$v->teaser			= $json_visit->teaser;
		$v->status_id		= $json_visit->status_id;
		$v->type_id			= $json_visit->type_id;
		$v->notes[]			= (array)$json_visit->notes;
		
		// if visit type is a "standard visit" / "basic trouble-shooting", is automatically closed setting status = 2
		if( $v->type_id == 1 ){
			$v->status_id = 2;
		}

		if( is_array( $json_visit->drugs ) && empty( $json_visit->drugs ) === false ){
			foreach ( $json_visit->drugs as $value ) {
				$v->drugs[] = (array)$value;
			}
		}
		
		if( is_array( $json_visit->diagnostic_tests ) && empty( $json_visit->diagnostic_tests ) === false ){
			foreach ( $json_visit->diagnostic_tests as $value ) {
				$v->diagnostic_tests[] = (array)$value;
			}
		}
		
		if( is_array( $json_visit->procedures ) && empty( $json_visit->procedures ) === false ){
			foreach ( $json_visit->procedures as $value ) {
				$v->procedures[] = (array)$value;
			}
		}
		
		$rv = $v->save();
		if( $rv == true ){
			$pet = new Pet( $v->pid );
			$pet->updateLastOwner( $v->oid );
		}
		
		
		
		return $v;
	}
	
	function visit_delete( $json_visit ){
		$v = new Visit( $json_visit->vid );
		$rv = $v->delete();
		
		return $v;
	}

