<?php
	include( 'includes/bootstrap.inc.php' );
	
	if( empty( Data::getSession( 'uid' ) ) ){
		header('Location: login.php?op=e');
		die();
	}
	
	$bs = new Bootstrap();
	$output = '';
	
	if( Security::checkByUid( Security::ROLE_ADMIN, $_SESSION['uid'] ) == false ){
		$layout->set ( "message-box", $bs->message( _( "Access denied" ), Bootstrap::ALERT_DANGER, 3000 ));
		$output = $bs->redirect( _LANDING_PAGE, 3500 );
		Watchdog::write( Watchdog::TYPE_ACCESS_DENIED, 'attempt to access species settings', $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
	}
	
	
	$op 	= Data::getGet( 'op' );
	$template_content 	= _DEFAULT_THEME_PATH . _DEFAULT_THEME . "/species.tpl";
	$layout_species		= 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 .= "</script>" . "\n";
	
	$layout_species->set ( "species-logic", $JS_translated_messages . '<script type="text/javascript" src="assets/js/species.logic.js"></script>' );
	
	
	$help_text = _("Note: This section is used to select species from the 'itis' universe to be used in the application and classify pets.") . "<br/>";
	$help_text .= _("It's not possibile to delete a specie if in use not to leave orphans. Only Admins can modify this section");
	$help_text .= _("It's not possibile to delete a specie if in use not to leave orphans. Only Admins can modify this section");
	$layout_species->set( 'species-help', $help_text );

	$rv = false;
	switch ($op){
		// handle json / ajax call
		case 'f':
			break;
		
		case 's':
			if( Species::exists( Data::getPost( 'tsn' ) ) == false ){
				$s = new Species();
				$s->tsn				= Data::getPost( 'tsn' );
				$s->complete_name	= Data::getPost( 'complete-name' );
				$s->familiar_name	= Data::getPost( 'familiar-name' );
				
				$rv = $s->save();
			}
			
			if($rv == true){
				// set a global message
				$message->set( _("Species saved"), Message::MESSAGE_TYPE_SUCCESS);
				$layout->set( "content", $bs->dialog( _("Saving species") ) . $bs->redirect( 'species.php', 1500 ) );
			}else{
				Watchdog::write( Watchdog::TYPE_DATABASE, 'cannot save species data', $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
				$layout->set ( "message-box", $bs->message( _("Cannot save species."), Bootstrap::ALERT_DANGER, 3000 ));
				$layout->set( "content", $bs->redirect( 'species.php', 3500 ) );
			}
			
			break;
			
		case 'd':
			if( Species::in_use( Data::getGet( 'tsn' ) ) == false ){
				$s = new Species( Data::getGet( 'tsn' ) );
				$rv = $s->delete();
			}
			
			if($rv == true){
				// set a global message
				$message->set( _("Species deleted"), Message::MESSAGE_TYPE_SUCCESS);
				$layout->add( "content", $bs->dialog( _("Deleting species") ) . $bs->redirect('species.php', 3000) );
			}else{
				Watchdog::write( Watchdog::TYPE_DATABASE, 'cannot delete species', $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
				$layout->set ( "message-box", $bs->message( _("Cannot delete species."), Bootstrap::ALERT_DANGER, 3000 ));
				$layout->add( "content", $bs->redirect('species.php', 3500) );
			}
			break;
		
		default:
			
			break;
	}
	
	
	
	// $tsn = BootstrapInput::create( 'tsn' );
	$bso = new BootstrapInput('tsn');
	$bso->setType( BootstrapInput::TYPE_HIDDEN );
	$tsn = $bso->output();
	unset( $bso );

	$itis_search = BootstrapInput::create( 'itis-search', '', _( "1. Select an animal"), false, _("e.g. Canis familiarus domesticus") );

	$bso = new BootstrapInput( 'complete-name', '', _("2. verify selected complete (latin) name"), true );
	$bso->placeholder = _("e.g. Canis familiarus domesticus");
	$bso->readonly = true;
	$complete_name = $bso->output();
	unset( $bso );

	$familiar_name = BootstrapInput::create( 'familiar-name', '', _( "3. choose a familiar name" ), true, _( "e.g. Dog, C. domesticus" ) );

	$species_add_button = BootstrapButton::create( 'add', _("Add") );


	// create a Datatable object and populate with data taken from User table
	$rs = species_list( false );
	$DT = new Datatable('species_unused');
	$DT->addRows($rs);

	$DT->addAction( _( 'delete' ), $_SERVER['PHP_SELF'] . '?op=d&tsn=%s', array('tsn'));
	// render the Datatable object and put the HTML output into a variable
	$species_unused_table = $DT->output();



	// create a Datatable object and populate with data taken from Species table
	$rs = species_list( true );
	$DT = new Datatable('species_used');
	$DT->addRows( $rs );

	// render the Datatable object and put the HTML output into a variable
	$species_used_table = $DT->output();


			
	$layout_species->set( 'panel-form-open', Bootstrap::openForm( 'species-form', $_SERVER['PHP_SELF'] . '?op=s' ) . $tsn);
	$layout_species->set( 'panel-itis-search', $itis_search );
	$layout_species->set( 'panel-complete-name', $complete_name );
	$layout_species->set( 'panel-familiar-name', $familiar_name );
	$layout_species->set( 'panel-add-button', $species_add_button );
	$layout_species->set( 'panel-form-close', Bootstrap::closeForm() );

	$layout_species->set( 'panel-species-unused-title', _( "Species not used in the System" ) );
	$layout_species->set( 'panel-species-unused-list', $species_unused_table );
	$layout_species->set( 'panel-species-used-title', _( "Species used in the System" ) );
	$layout_species->set( 'panel-species-used-list', $species_used_table );

	$layout->set( 'section-name', _( "Species" ) );
	// 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->add( "content", $layout_species->output() );
	
	echo template_common_header();
	echo $layout->output();
	echo template_common_footer();
?>
