<?php
	include ('includes/bootstrap.inc.php');
	
	if(empty(Data::getSession('uid'))){
		header('Location: login.php?op=e');
		die();
	}
	
	$op 	= Data::getGet ( 'op' );
	$oid 	= Data::getGet ( 'oid' );
	$output = '';
	// create a new user object
	$o = new Owner($oid);
	$bs = new Bootstrap();
	
	$layout_owner = new Template( _DEFAULT_THEME_PATH . _DEFAULT_THEME . "/generic_content.tpl" );

	
	switch ($op){
		case 'f':
			/**
			 * Check user permission to edit owner's data. log the error
			 */
			if( Security::checkByUid( Security::ROLE_USER, $_SESSION['uid'] ) == false ){
				$layout->set ( "message-box", $bs->message( LANG_SECURITY_ACCESS_DENIED, Bootstrap::ALERT_DANGER, 3000 ));
				$layout->set ( "content", $bs->redirect('owner.php', 3500) );
				Watchdog::write( 'Access denied ', 'attempt to modify owner data', $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
				break;
			}
			
			
			$owner_form			= owner_form( $oid );
			$owner_form_submit	= $bs->addButton( 'save', _("save"), _("save"), "$('#owner-form').submit()" );
			
			
			// @todo: delete owner or logically set a status = 0 ??? what about their visit ? by now we don't allow deleteing...
			/*
			$owner_form_delete 		= $bs->addButton( 'delete', 'delete' );
			$owner_form_delete_logic = $bs->confirm('button-delete', _( 'Do you want to delete (deactivate) selected owner ?' ), $_SERVER['PHP_SELF'] . '?op=d&oid=' . $oid);
			*/
			
			$layout_owner->set( "generic-content", $owner_form . $owner_form_submit );
			$layout->set ( "content", $layout_owner->output() );
			break;
		
		case 's':
			if( Security::checkByUid( Security::ROLE_USER, $_SESSION['uid'] ) == false ){
				$layout->set ( "message-box", $bs->message( LANG_SECURITY_ACCESS_DENIED, Bootstrap::ALERT_DANGER, 3000 ));
				$layout->set ( "content", $bs->redirect('owner.php', 3500) );
				Watchdog::write( 'Access denied ', 'attempt to modify owner data', $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
				break;
			}
			
			$o->country_id 		= Data::getPost( 'country_id' );
			$o->firstname 		= Data::getPost( 'firstname' );
			$o->lastname		= Data::getPost( 'lastname' );
			$o->address 		= Data::getPost( 'address' );
			$o->zip		 		= Data::getPost( 'zip' );
			$o->city 			= Data::getPost( 'city' );
			$o->ssn		 		= Data::getPost( 'ssn' );
			$o->phone			= Data::getPost( 'phone' );
			$o->mobile			= Data::getPost( 'mobile' );
			$o->email			= Data::getPost( 'email' );
			
			$rv = $o->save();
			
			if($rv == true){
				// set a global message
				$message->set( _("Owner saved"), Message::MESSAGE_TYPE_SUCCESS);
				$layout->set ( "content", $bs->dialog( _("Saving owner") ) . $bs->redirect('owner.php', 3000) );
			}else{
				Watchdog::write( Watchdog::TYPE_DATABASE, 'cannot save owner data', $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
				$layout->set ( "message-box", $bs->message( _("Cannot save owner. Please check form again or contact application support."), Bootstrap::ALERT_DANGER, 3000 ));
				$layout->set ( "content", $bs->redirect('owner.php?op=f&oid=' . $o->oid, 3500) );
			}
			break;
		
		case 'd':
			/**
			 * Check user permission to delete owner. Show a message and log the error
			 */
			if( Security::checkByUid( Security::ROLE_USER, $_SESSION['uid'] ) == false ){
				$layout->set ( "message-box", $bs->message( LANG_SECURITY_ACCESS_DENIED, Bootstrap::ALERT_DANGER, 3000 ));
				$layout->set ( "content", $bs->redirect('owner.php', 3500) );
				Watchdog::write( 'Access denied ', 'attempt to delete owner', $_SESSION['uid'], Watchdog::WATCHDOG_WARNING );
				break;
			}
			
			$rv = $o->delete();
			
			if($rv == true){
				// set a global message
				$message->set(LANG_OWNER_FORM_DELETE_SUCCESS, Message::MESSAGE_TYPE_SUCCESS);
				$layout->set ( "content", $bs->dialog(LANG_OWNER_FORM_DELETE_IN_PROGRESS) . $bs->redirect('owner.php', 3000) );
			}else{
				$layout->set ( "message-box", $bs->message( LANG_OWNER_FORM_DELETE_ERROR, Bootstrap::ALERT_DANGER, 3000 ));
				$layout->set ( "content", $bs->redirect('owner.php?op=f&uid=' . $u->uid, 3500) );
			}
			break;
		
		default:
			$o = Connection::entity_load('owners', array('oid', 'email', 'firstname', 'lastname', 'phone', 'mobile'), array(), 'lastname ASC');
			
			// create a Datatable object and populate with data taken from User table
			$DT = new Datatable('owners');
			
			
			// populate datatable
			$DT->addRows( $o );
			
			
			/**
			 * Check user permission to create table action.
			 */
			if( Security::checkByUid( Security::ROLE_USER, $_SESSION['uid'] ) == true ){
				$DT->addAction('edit', 'owner.php?op=f&oid=%s', array('oid'));
			}
			// @todo: implement view
			// $DT->addAction('view', 'owner.php?op=view&uid=%s', array('oid'));
			
			// render the Datatable object and put the HTML output into a variable
			$output = $DT->output();
			
			if( Security::checkByUid( Security::ROLE_USER, $_SESSION['uid'] ) == true ){
				$output .= '<a href="'.$_SERVER['PHP_SELF'].'?op=f">' . $bs->addButton('add', 'add') . '</a>';
			}
			
			$layout_owner->set ( "generic-content", $output);
			$layout->set ( "content", $layout_owner->output() );
			
			break;
	}
	
	

	
	
	$layout->set ( 'section-name', _( "Owners" ) );

	// 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 );
	
	
	
	/**
	 * Outputs the page with the user's profile.
	 */
	echo template_common_header();
	echo $layout->output ();
	echo template_common_footer();
?>
