<?php
	/**
	 * Pet search 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 pet_search( $query ){
		$sql = "SELECT 
					pid, 
					CONCAT(name, ' (', pid, ')' ) as value 
				FROM pets 
				WHERE 
					name LIKE '%{$query}%'
				OR pid = '{$query}'
				OR microchip = '{$query}'
				OR tatuatge = '{$query}'";
		
		
		$con = new Connection();
		
		$rv = $con->execute($sql);
		
		return $rv;
	}
	
	/**
	 * @deprecated
	 
	function pet_form( $pid ){
		$bs = new Bootstrap();
		
		$open_form = $bs->openForm( 'pet-form', $_SERVER['PHP_SELF'] . '?op=s&pid=' . $pid );
		
		$form_elements = pet_form_elements( $pid );		
		
		$close_form = $bs->closeForm();
		
		return $open_form . $form_elements . $close_form;
	}
	*/
	
	
		
	function pet_form_elements( $pid ){
		$bs = new Bootstrap();
		
		$pet = new Pet( $pid );
		
		$input_pid = $bs->addInput('hidden', 'pid', $pid);
		
		$species = Connection::entity_load( 'species', array('tsn', 'familiar_name'), array(), 'familiar_name ASC' );
		$select_species = $bs->addSelect( 'species', $species, $pet->tsn, true,  _( "Breed" ) );
		
		$input_oid = $bs->addInput('hidden', 'oid', $pet->oid );
		
		$input_name = $bs->addInput('text', 
										'name', 
										$pet->name,
										true,
										_( "Name" ),
										'Muddy'
										);

		
		
		
		
		$gender[0]['id'] = 'M'; 
		$gender[0]['gender'] = _( 'Male' ); 
		
		$gender[1]['id'] = 'F'; 
		$gender[1]['gender'] = _('Female' ); 
		
		$select_gender = $bs->addSelect( 'gender', $gender, $pet->gender, true,  _( "Gender" ) );

		$input_description = $bs->addInput('text', 
										'description', 
										$pet->description,
										true,
										_( "Description" ),
										_( "e.g. Gentle and good shepard dog")
										);

		$input_color = $bs->addInput('text', 
										'color', 
										$pet->color,
										false,
										_( "Color" ),
										_( "e.g. white, black and grey" )
										);

		$date_picker = new Datepicker( 'date_of_birth', $pet->date_of_birth_timestamp, _( "Date of birth" ) );
		$input_date_of_birth = $date_picker->output();
		unset( $date_picker );
		
		
		$date_picker = new Datepicker( 'date_of_green_meadows', $pet->date_of_green_meadows_timestamp, _( "Date of green meadows" ) );
		$input_date_of_green_meadows = $date_picker->output();
		unset( $date_picker );
		

		$input_microchip = $bs->addInput('text', 
										'microchip', 
										$pet->microchip,
										false,
										_( "Microchip" ),
										_( "e.g. M-3535" )
										);

		$date_picker = new Datepicker( 'microchip-date', $pet->microchip_timestamp, _( "Microchip Date" ) );
		$input_microchip_date = $date_picker->output();
		unset( $date_picker );
		
		
		
		$input = new BootstrapInput( 'microchip-location', $pet->microchip_location, _("Microchip location") );
		$input->placeholder = _( "e.g. Neck" );
		$input_microchip_location = $input->output();
		unset( $input );
		
		
		$input = new BootstrapInput( 'tatuatge', $pet->tatuatge, _("Tatuatge") );
		$input->placeholder = _( "e.g. 142432417" );
		$input_tatuatge = $input->output();
		unset( $input );
		
		
		$date_picker = new Datepicker( 'tatuatge-date', $pet->tatuatge_timestamp, _( "Tatuatge Date" ) );
		$input_tatuatge_date = $date_picker->output();
		unset( $date_picker );
		
		
		return $input_pid.
				$select_species.
				$input_oid.
				$input_name.
				$select_gender.
				$input_description.
				$input_color.
				$input_date_of_birth.
				$input_date_of_green_meadows.
				$input_microchip.
				$input_microchip_date.
				$input_microchip_location.
				$input_tatuatge.
				$input_tatuatge_date
				;
	}
	

	
