<?php
	include ('includes/bootstrap.inc.php');
	if ( empty( Data::getSession('uid') ) ) {
		header( 'Location: login.php?op=e' );
	}
	
	$op 	= Data::getGet ( 'op' );
	
	$pid 	= Data::getGet( 'pid' );
	$vid 	= Data::getGet( 'vid', null );
	$oid	= null;
	
	$pet	= new Pet();
	$visit	= new Visit();
	$msg 	= '';
	
	/**
	 * initialize Bootstrap helper class
	 */
	$bs = new Bootstrap();
	
	$readonly = false;
	$is_update = false;


	
	/**
	 * check if animal exists and load a pet object
	 */
	if( empty( $pid ) == false && is_numeric( $pid ) == true ) {
		$pet->load( $pid );
		$oid = $pet->oid;
		
		// pet does not exists
		if( empty( $pet->pid ) ){
			Watchdog::write( 'content', 'Pet ID not found: ' . $pid, $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
			$layout->set ( "message-box", $bs->message( _( "Cannot find Pet " ) . $pid, Bootstrap::ALERT_WARNING, 3000 ));
			$layout->set ( "content", $bs->redirect('/search.php', 3500) );
			
			echo $layout->output();
		}
	}
	
	// check if visit is new and not closed / deleted
	if( empty( $vid ) == false && is_numeric( $vid ) == true ) {
		// load a visit object
		$visit->load( $vid );
		
		// pet does not exists
		if( empty( $visit->pid ) ){
			Watchdog::write( 'content', 'Visit ID not found: ' . $vid, $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
			$layout->set ( "message-box", $bs->message( _( "Cannot find Visit nr. " ) . $vid, Bootstrap::ALERT_WARNING, 3000 ));
			$layout->set ( "content", $bs->redirect('/search.php', 3500) );
			
			echo $layout->output();
		}
		
		$is_update = true;
		
		// Pet ID and Owner ID are read from visit object
		$pid = $visit->pid;
		$oid = $visit->oid;
	}
	
	// if logged user is a readonly role.
	if( $_SESSION['rid'] == User::USER_ROLE_READONLY ){
		$readonly = true;
	}

	// load correct object into Pet and Owner
	$pet->load( $pid );
	$owner = new Owner( $oid );
	
	
	
	
	/**
	 * Loads our page template and print in the main layout.
	 */
	$template_content 	= _DEFAULT_THEME_PATH . _DEFAULT_THEME . "/visit.tpl";
	$layout_visit		= new Template ( $template_content );
	
	/**
	 * Add JS logic to template
	 */
	 // create a javascript array to allow translation of various messages
	$JS_translated_messages = "<script>" . "\n";
	$JS_translated_messages .= "var translated_messages = [];" . "\n";
	$JS_translated_messages .= "translated_messages[0]='" . _("Attention") . "';" . "\n";
	$JS_translated_messages .= "translated_messages[1]='" . _("Following information are required:") . "';" . "\n";
	$JS_translated_messages .= "translated_messages[2]='" . _("Are you sure to save this visit ?") . "';" . "\n";
	$JS_translated_messages .= "translated_messages[3]='" . _("Visit save in progress...") . "';" . "\n";
	$JS_translated_messages .= "translated_messages[4]='" . _("Saving...") . "';" . "\n";
	$JS_translated_messages .= "translated_messages[5]='" . _("Are you sure to delete this visit ?") . "';" . "\n";
	$JS_translated_messages .= "</script>" . "\n";
	
	$JS_init_vars = "<script>" . "\n";
	$JS_init_vars .= "var pid = " . $pid . ";" . "\n";
	$JS_init_vars .= "var vid = " . Data::isEmpty( $vid, 'null' ) . ";" . "\n";
	$JS_init_vars .= "var pet = " . json_encode( $pet ) . ";" . "\n";
	$JS_init_vars .= "var visit = " . json_encode( $visit ) . ";" . "\n";
	$JS_init_vars .= "var owner = " . json_encode( $owner ) . ";" . "\n";
	$JS_init_vars .= "</script>" . "\n";
	
	
	$layout_visit->set ( "visit-logic-init", $JS_translated_messages . $JS_init_vars );
	$layout_visit->set ( "visit-logic", '<script type="text/javascript" src="assets/js/visit.logic.js"></script>' );

	
	
	

	/**
	 * Visit teaser section
	 */
	$teaser_is_disabled = false;
	if( $visit->isCancelled() == true || $readonly === true ){
		$teaser_is_disabled = true;
	}
	
	$panel_visit_teaser = $bs->addInput('text', 
											'visit-teaser', 
											$visit->teaser, 
											false, 
											'',
											_( "Visit short description" ),
											'',
											'visit-teaser',
											$teaser_is_disabled
											);
	
	$layout_visit->set ( "panel-visit-teaser-data", $panel_visit_teaser );
	unset( $teaser_is_disabled );

	
	
	
	
	
	/**
	 * Visit status section
	 */
	if( $is_update == true ){
		$visit_status = Connection::entity_load('visit_status');
	}else{
		$visit_status = Connection::entity_load('visit_status', array(), array( 'status_id' => Visit::STATUS_OPENED ) );
	}
	
	$status_is_disabled = false;
	if( $visit->isOpened() == false || $readonly === true ){
		$status_is_disabled = true;
	}
	
	$panel_visit_status = $bs->addSelect( 'status-id',
											$visit_status,
											$visit->status_id,
											true,
											'',
											'status-id',
											$status_is_disabled
											);
	$layout_visit->set ( "panel-visit-status-data", $panel_visit_status );
	unset( $status_is_disabled );
	
	
	
	
	
	/**
	 * Visit type section
	 */
	$type_is_disabled = false;
	if( $is_update == true || $readonly === true ){
		$type_is_disabled = true;
	}
	$visit_type = Connection::entity_load('visit_type');
	$panel_visit_type = $bs->addSelect( 'type-id',
											$visit_type,
											$visit->type_id,
											true,
											'',
											'type-id',
											$type_is_disabled
											);
	$layout_visit->set ( "panel-visit-type-data", $panel_visit_type );
	unset( $type_is_disabled );
	
	
	
	


	
	/**
	 * Pet Section
	 */
	$pet_popover = $bs->addPopOver( 'pet-popover', '<i class="glyphicon glyphicon-leaf"></i>', 'title', _( "Pet detail" ) );
	
	$layout_visit->set ( "panel-pet-title", _("Pet") );
	$layout_visit->set ( "panel-pet-options", $pet_popover );
	$layout_visit->set ( "panel-pet-data", '<div id="panel-pet-data"></div>' );
	
	
	
	
	
	
	
	/**
	 * Owner section
	 */
	$owner_modal_form_button[] = $bs->addButton ( 'save', _( "save" ), 'save', 'owner_save()' );
	$owner_modal_form_button[] = $bs->addButton ( 'close', _( "close" ), 'close', '', 'close-owner-modal', array ('data-dismiss="modal"') );
	$owner_modal_form = $bs->modal( _( "Owner" ), null, $owner_modal_form_button, "modal-owner" );

	if( $is_update == false && $readonly == false ){
		$panel_owner_title = $bs->addInput('text', 
											'owner-search', 
											'', 
											true, 
											'',
											_( "Owner" ) . ' (' . _( "Person to contact" ) . ')',
											'',
											'owner-search'
											);
		$panel_owner_options 	=  '<a href="#" id="owner-edit" data-toggle="modal" data-target="#modal-owner" data-rel="reload"><i class="glyphicon glyphicon-edit"></i></a>
									<a href="#" id="owner-reload" data-rel="reload"><i class="glyphicon glyphicon-refresh"></i></a>';

	}else{
		$panel_owner_title = _( "Owner" );
		$panel_owner_options 	= '';
	}
	
	$layout_visit->set ( "panel-owner-title", $panel_owner_title );
	$layout_visit->set ( "panel-owner-options", $panel_owner_options );
	$layout_visit->set ( "panel-owner-data", '<div id="panel-owner-data"></div>' );
	$layout_visit->set ( "panel-owner-modal-form", $owner_modal_form );
	
	
	
	
	
	
	
	/**
	 * Reason to visit section
	 */
	if( $is_update == false ){
		$panel_reason_title = $bs->addInput('text', 
											'reason-search', 
											'', 
											true, 
											'',
											_( "Main reason for visit" ) . ' (' . _( "e.g. Vaccination" ) . ')',
											'',
											'reason-search'
											);
		$panel_reason_data = '';

	}else{
		$panel_reason_title	= _( "Main reason for visit" );
		$panel_reason_data	= '<div id="#panel-reason-data-result"><input type="hidden" value="' . $visit->reason_id .'" id="reason-for-visit" name="reason-for-visit">' . $visit->reason . '</div>';
	}
	
	$layout_visit->set ( "panel-reason-title", $panel_reason_title );
	$layout_visit->set ( "panel-reason-options", '' );

	// this panel is loaded using typeahead search option 
	$layout_visit->set ( "panel-reason-data", '<div id="panel-reason-data">' . $panel_reason_data . '</div>' );
	
	
	
	
	
	
	
	/**
	 * Diagnosis
	 */
	if( $visit->isOpened() == true ){
		$panel_diagnosis_title = $bs->addInput('text', 
											'diagnosis-search', 
											'', 
											true, 
											'',
											_( "Diagnosis/Suspect" ) . ' (' . _( "e.g. Diabetes mellitus" ) . ')',
											'',
											'diagnosis-search'
											);
		$panel_diagnosis_data = '';
	}else{
		$panel_diagnosis_title	= _( "Diagnosis/Suspect" );
		$panel_diagnosis_data	= '<div id="#panel-diagnosis-data-result"><input type="hidden" value="' . $visit->diagnosis_id .'" id="diagnosis" name="diagnosis">' . $visit->diagnosis . '</div>';
	}

	$layout_visit->set ( "panel-diagnosis-title", $panel_diagnosis_title );
	$layout_visit->set ( "panel-diagnosis-options", '' );
	
	// this panel is loaded using typeahead search option 
	$layout_visit->set ( "panel-diagnosis-data", '<div id="panel-diagnosis-data">' . $panel_diagnosis_data . '</div>' );
	
	
	
	
	

	
	/**
	 * Drugs
	 */
	if( $visit->isOpened() == true ){
		$panel_drug_title = $bs->addInput('text', 
											'drug-search', 
											'', 
											false, 
											'',
											_( "Drug prescription" ),
											'',
											'drug-search'
											);
		$panel_drug_data = '';
	}else{
		$panel_drug_title	= _( "Drug prescription" );
		
	}

	// fill drug readonly list
	if( is_array( $visit->drugs ) === true ){
		$panel_drug_data = '';
		foreach ( $visit->drugs as $row => $drug ) {
			$panel_drug_data .= '<div class="drug-row"><input type="hidden" value="' . $drug['did'] . '" id="did" name="did"><input type="text" value="' . $drug['qty'] . '" id="drug-qty" name="drug-qty" size="2" readonly>' . $drug['name'] . ' </div>';
		}			
	}

	$layout_visit->set ( "panel-drug-title", $panel_drug_title );
	$layout_visit->set ( "panel-drug-options", '' );
	
	// this panel is loaded using typeahead search option 
	$layout_visit->set ( "panel-drug-data", '<div id="panel-drug-data">' . $panel_drug_data . '</div>' );
	
	
	
	
	
	
	
	/**
	 * Diagnostic
	 */
	if( $visit->isOpened() == true ){
		$panel_diagnostic_title = $bs->addInput('text', 
											'diagnostic-search', 
											'',
											false, 
											'',
											_( "Diagnostic test" ),
											'',
											'diagnostic-search'
											);
		$panel_diagnostic_data = '';
	}else{
		$panel_diagnostic_title = _( "Diagnostic test" );
	}
	
	// fill drug readonly list
	if( is_array( $visit->diagnostic_tests ) === true ){
		$panel_diagnostic_data = '';
		foreach ( $visit->diagnostic_tests as $row => $diagnostic_test ) {
			$panel_diagnostic_data .= '<div class="diagnostic-row"><input type="hidden" value="' . $diagnostic_test['id'] . '" id="diagnostic-test" name="diagnostic-test">' . $diagnostic_test['term_name'] . ' </div>';
		}			
	}

	$layout_visit->set ( "panel-diagnostic-title", $panel_diagnostic_title );
	$layout_visit->set ( "panel-diagnostic-options", '' );
	
	// this panel is loaded using typeahead search option 
	$layout_visit->set ( "panel-diagnostic-data", '<div id="panel-diagnostic-data">' . $panel_diagnostic_data . '</div>' );
	
	
	
	
	
	
	
	/**
	 * Visit procedure panel
	 */
	if( $visit->isOpened() == true ){
		$panel_procedure_title = $bs->addInput('text', 
											'procedure-search', 
											'', 
											true, 
											'',
											_( "Procedure or treatment" ) . ' (' . _( "e.g. Bandaging" ) . ')',
											'',
											'procedure-search'
											);

		$panel_procedure_data = '';
	}else{
		$panel_procedure_title = _( "Procedure or treatment" );
	}
	
	// fill procedure readonly list
	if( is_array( $visit->procedures ) === true ){
		$panel_procedure_data = '';
		foreach ( $visit->procedures as $row => $procedure ) {
			$panel_procedure_data .= '<div class="procedure-row"><input type="hidden" value="' . $procedure['id'] . '" id="procedure" name="procedure">' . $procedure['term_name'] . '</div>';
		}			
	}

	$layout_visit->set ( "panel-procedure-title", $panel_procedure_title );
	$layout_visit->set ( "panel-procedure-options", '' );
	
	// this panel is loaded using typeahead search option 
	$layout_visit->set ( "panel-procedure-data", '<div id="panel-procedure-data">' . $panel_procedure_data . '</div>' );
	
	
	
	
	
	
	
	/**
	 * Notes sections
	 */
	$panel_note_options = '';
	if( $is_update == true ){
		$panel_note_options 	= '<a href="#" id="troubleshooting_get" data-rel="reload"><i class="glyphicon glyphicon-check"></i></a>';
	}
	
	$layout_visit->set ( "panel-note-title", _( "Add a note" ) );
	$layout_visit->set ( "panel-note-options", $panel_note_options );

	if( $visit->isCancelled() == false && $readonly == false ){
		$panel_note_title	= $bs->addInput('text', 
											'note-title', 
											'', 
											!$is_update, 
											'',
											_( "Title of the annotation" ),
											_( "Title of the annotation" ),
											'note-title'
											);
		
		$panel_note_text	= $bs->addTextArea( 'note-text', 
											'', 
											!$is_update, 
											'', 
											_("Annotation text"), 
											_("Write some annotation text here. You can write multiple notes once a visit is saved."), 
											'note-text');
		
	}else{
		$panel_note_title 	= '';
		$panel_note_text	= '';
	}
	
	$layout_visit->set ( "panel-note-data", '<div id="panel-note-data">' . $panel_note_title . $panel_note_text . '</div>' );
	
	
	
	
	
	
	
	/*
	 * Notes archive
	 */
	$layout_visit->set ( "panel-notes-archive-title", _( "Notes" ) );
	$layout_visit->set ( "panel-notes-archive-options", "" );
	
	// create the note archive list
	$panel_note_archive_data = '';
	if( is_array( $visit->notes ) && empty( $visit->notes ) == false ){
		foreach( $visit->notes as $note ){
			$panel_note_archive_data .= '<div class="note-created">' . Data::UnixTimestampToDate( $note['created'] ) . ' ' . _("created by") . ' ' . $note['firstname'] . ' ' . $note['lastname'] . '</div>' . 
										'<div class="note-title">' . $note['title'] . '</div>' . 
										'<div class="note-text">' . $note['text'] . '</div>';
		}
		
	}
	$layout_visit->set ( "panel-notes-archive-data", '<div id="panel-note-archive-data">' . $panel_note_archive_data . '</div>' );





	
	
	/**
	 * Fill empty sections
	 */
	/*
	$panel_empty_options 	= '<a href="#" id="troubleshooting_get" data-rel="reload"><i class="glyphicon glyphicon-check"></i></a>';
	$layout_visit->set ( "panel-empty-title", _( "Empty" ) );
	$layout_visit->set ( "panel-empty-options", $panel_empty_options );
	$layout_visit->set ( "panel-empty-data", '<div id="panel-empty-data"></div>' );
	*/
	
	
	
	// save visit button
	// $navbar .= '<a class="action_button" href="#" id="visit-action-save"><i class="glyphicon glyphicon-floppy-disk"></i></a> ';
	$buttons = '';
	
	$button_save = new BootstrapButton( 'visit-action-save', _("save" ) );
	
	if( $visit->status_id == Visit::STATUS_CANCELLED ){
		$button_save->disabled = true;
	}
	$buttons .= $button_save->output();
	
	$buttons .= $bs->addButton( 'visit-action-new', _("new visit" ), '', "window.location.href='visit.php?pid=" . $pid . "'") . " ";
	$buttons .= $bs->addButton( 'visit-action-relate-new', _("relate new visit" ) ) . " ";
	$buttons .= $bs->addButton( 'visit-action-print', _("print" ), '', "window.open('visit.print.php?vid=" . $vid . "', '_blank')") . " ";
	
	
	$button_delete = new BootstrapButton( 'visit-action-delete', _("delete" ) );
	$button_delete->disabled = true;
	if( $visit->status_id == Visit::STATUS_CLOSED ){
		$button_delete->disabled = false;
	}
	$buttons .= $button_delete->output();
	$layout_visit->set ( "buttons", $buttons );

	/**
	 * Set page layout
	 */
	$layout->set	( 'section-name', _( "Visit" ) );
	
	// create the search input using Typeahead suggestion library
	$search_logic = '<script type="text/javascript" src="assets/js/search.logic.js"></script>';

	$bso = new BootstrapInput( 'input-search', '' );
	$bso->placeholder = _('Search...');
	$search_input = $bso->output();

	$layout->set ( "search", $search_logic . $search_input );
	$layout->set ( 'content', $layout_visit->output() );

	
	/**
	 * Outputs the page with the set Layout.
	 */
	
	echo template_common_header();
	echo $layout->output();
	echo template_common_footer();
?>
