<?php
namespace JExtstore\Component\JRealtimeAnalytics\Administrator\View\Config;
/**
 *
 * @package JREALTIMEANALYTICS::CONFIG::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage config
 * @author Joomla! Extensions Store
 * @copyright (C) 2013 - Joomla! Extensions Store
 * @license GNU/GPLv2 http://www.gnu.org/licenses/gpl-2.0.html 
 */
defined ( '_JEXEC' ) or die ( 'Restricted access' );
use Joomla\CMS\Language\Text;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\Helpers\Toolbars as ToolbarHelper;
use JExtstore\Component\JRealtimeAnalytics\Administrator\Framework\View as JRealtimeView;

/**
 * Config view
 *
 * @package JREALTIMEANALYTICS::CONFIG::administrator::components::com_jrealtimeanalytics
 * @subpackage views
 * @subpackage config
 * @since 1.0
 */
class HtmlView extends JRealtimeView {
	// Template view variables
	protected $params_form;
	protected $params;
	
	/**
	 * Add the page title and toolbar.
	 *
	 * @since	1.6
	 */
	protected function addDisplayToolbar() {
		ToolbarHelper::title( Text::_( 'COM_JREALTIME_CONFIG' ), 'jrealtimeanalytics' );
		ToolbarHelper::save('config.saveentity', 'COM_JREALTIME_SAVECONFIG');
		ToolbarHelper::custom('cpanel.display', 'home', 'home', 'COM_JREALTIME_CPANEL', false);
	}
	
	/**
	 * Effettua il rendering dei tabs di configurazione del componente
	 * @access public
	 * @return void
	 */
	public function display($tpl = null) {
		$doc = $this->app->getDocument();
		$this->loadJQuery($doc);
		$this->loadBootstrap($doc);
		$this->loadValidation($doc);
		
		// Load specific JS App
		$doc->getWebAssetManager()->addInlineScript("
					Joomla.submitbutton = function(pressbutton) {
						if(!jQuery.fn.validation) {
							jQuery.extend(jQuery.fn, jrealtimejQueryBackup.fn);
						}
						jQuery('#adminForm').validation();
				
						if (pressbutton == 'cpanel.display') {
							Joomla.submitform( pressbutton );
							return true;
						}
		
						if(jQuery('#adminForm').validate()) {
							Joomla.submitform( pressbutton );
							return true;
						}

						var parentId = jQuery('ul.errorlist').parents('div.tab-pane').attr('id');

						var nodeElement = document.querySelector('#tab_configuration a[data-element=' + parentId + ']');
						if(nodeElement) {
							var tabInstance = new bootstrap.Tab(nodeElement);
							tabInstance.show();
						}

						return false;
					};
				");
		
		$model = $this->getModel();
		$params = $model->getData();
		$form = $model->getForm();
		
		// Bind the form to the data.
		if ($form && $params) {
			$form->bind($params);
		}
		
		$this->params_form = $form;
		$this->params = $params;
		
		// Aggiunta toolbar
		$this->addDisplayToolbar();
		
		// Output del template
		parent::display();
	}
}
?>