<?php

defined ( '_JEXEC' ) or die ( 'Restricted access' );
/*
 * @component Calc Builder 
 * @copyright Copyright (C) January 2017. 
 * @license GPL 3.0 
 * This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 
 * See the GNU General Public License for more details. 
 * See <http://www.gnu.org/licenses/>. 
 * More info www.moonsoft.es 
 * gestion@moonsoft.es
 */

$document = JFactory::getDocument ();
// $document->addStyleSheet(JURI::base().'components/com_calcbuilder/assets/css/default.css','text/css','screen');
JHTML::_ ( 'behavior.framework', true );
$editor = JFactory::getEditor ();

$arr_fields_in_section = array ();
foreach ( $this->sections as &$section ) {
	$arr_fields_in_section = array_merge ( $arr_fields_in_section, explode ( ',', $section->fields ) );
}
$css = $this->__getCSS ();
foreach ( $css as $style ) {
	$document->addStyleSheet ( $style );
}
$js = $this->__getJS ();
foreach ( $js as $jscript ) {
	?><script src="<?php echo $jscript;?>"></script><?php
}

?>
<script
	src="<?php echo JURI::base();?>components/com_calcbuilder/assets/js/beautify-html.js"></script>
<style>
<!--
.lm_tips {
	margin-left: 5px;
	margin-top: 80px;
	border: 1px solid #ccc;
	border-radius: 5px;
	background-color: #f9f9f9;
	padding-bottom: 10px;
	padding-left: 5px;
	padding-right: 5px;
}
-->
</style>
<script type="text/javascript">
<!--
//var section='';
window.addEvent('load',function(){
	$$('.columns').addEvent('click',function(e){
		if(confirm("<?php echo JText::_('CB_CONFIRM_LAYOUT_CHANGE');?>")){				
			var elem=e.target;
			generateTable($(elem).getProperty('idlo'));
		}
	});	
	jQuery('body').tooltip({
		selector:'[rel=tooltip]',
		placement:'right',
		html:true
	});
});
function setContentEditor(layout,value){
  <?php
		$value = 'value';
		echo str_replace ( "'", "", $editor->setContent ( $$layout, $$value ) );
		?>
}
function generateTable(columns){
	var arrFields_A=new Array();	
	<?php
	$i = 0;
	foreach ( $this->allfields as $field ) {
		if (! in_array ( $field->id, $arr_fields_in_section )) {
			?>
		arrFields_A[<?php echo $i;?>]={'name':'<?php echo $field->name; ?>','varf':'<?php echo $field->var; ?>'};
		<?php
			
$i ++;
		}
		?>
	<?php
	
}
	foreach ( $this->sections as &$section ) {
		?>	
		var arrFields_<?php echo $section->id;?>=new Array();						
			<?php
		$i = 0;
		$fields_temp = explode ( ',', $section->fields );
		$fields_temp = array_map ( 'trim', $fields_temp );
		$fields_temp = array_filter ( $fields_temp, 'strlen' );
		foreach ( $fields_temp as $field ) {
			?>
				arrFields_<?php echo $section->id;?>[<?php echo $i;?>]={'name':'<?php echo $this->allfields[$field]->name; ?>','varf':'<?php echo $this->allfields[$field]->var; ?>'};				
			<?php
			
$i ++;
		}
		?>			
	<?php } ?>
	var containerAll=new Element('div');
	var rowFluid=new Element('div',{'class':'row-fluid'});
	var formHorizontal=new Element('div',{'class':'form-horizontal'});

	section=jQuery('#navmenu .active a').attr('href').split("_")[1];
	var arrFields=eval("arrFields_"+section);

	var numField=Math.ceil(arrFields.length/columns);
	var lastField=0;
	for(var i=0;i<columns;i++){
		var span=new Element('div',{'class':'span'+(12/columns)});
		var topLimit=lastField+numField;
		for(var j=lastField;j<topLimit;j++){
			if (typeOf(arrFields[j])=='null') break;			
			var controlGroup=new Element('div',{'class':'control-group'});
			var controlLabel=new Element('div',{'class':'control-label'});
			var controls=new Element('div',{'class':'controls'});
			controlLabel.set('html',arrFields[j].name);
			controls.set('html','##'+arrFields[j].varf+'##');
			controlGroup.adopt(controlLabel);
			controlGroup.adopt(controls);
			span.adopt(controlGroup);			
			lastField++;
		}	
		formHorizontal.adopt(span);	
	}
	rowFluid.adopt(formHorizontal);
	containerAll.adopt(rowFluid);
	
	var sects='';
	<?php foreach ($this->sections as &$section){?>
	sects+='<br />##<?php echo $section->name; ?>##';	
	<?php } ?>	
	if (section=="A")
		pretty_html=style_html(containerAll.get('html')+sects);
	else
		pretty_html=style_html(containerAll.get('html'));
	setContentEditor('layout_'+section,pretty_html);	
}

//-->
</script>
<form action="index.php" method="post" name="adminForm" id="adminForm">
	<div class="cb_table_wrapper row-fluid" style="width: 98%;">
		<div class="span8">
			<div class="form-horizontal">
				<div class="control-group">
					<label class="control-label"></label>
					<div class="controls">
						<a href="#layout_modal_info" role="button" class="btn btn-info"
							data-toggle="modal" style="margin-left: 10px;">
						<?php echo JText::_( 'SECTION_HELP_TITLE' ); ?>
					</a>
						<div id="layout_modal_info" class="modal hide fade" tabindex="-1"
							role="dialog" aria-hidden="true">
							<div class="modal-header">
								<button type="button" class="close" data-dismiss="modal"
									aria-hidden="true">&times;</button>
								<h3><?php echo JText::_( 'SECTION_HELP_TITLE' ); ?></h3>
							</div>
							<div class="modal-body">
								<p><?php echo JText::_( 'SECTION_HELP' ); ?></p>
							</div>
							<div class="modal-footer">
								<a data-dismiss="modal" class="btn">Close</a>
							</div>
						</div>
					</div>
				</div>
				<div class="control-group">
					<label class="control-label" rel="tooltip"
						data-original-title="<?php echo JText::_("T_CB_TABLE_LAYOUT")?>"><?php echo JText::_( 'CB_TABLE_LAYOUT' ); ?></label>
					<div class="controls">
						<div class="btn-group" data-toggle="buttons-radio">
							<button type="button" idlo="1" class="btn columns"><?php echo JText::_('ONE_COLUMNS');?></button>
							<button type="button" idlo="2" class="btn columns"><?php echo JText::_('TWO_COLUMNS');?></button>
							<button type="button" idlo="3" class="btn columns"><?php echo JText::_('THREE_COLUMNS');?></button>
						</div>
					</div>
				</div>
				<ul class="nav nav-pills" id="navmenu">
					<li class="active"><a data-toggle="tab" style="height: 20px;"
						href="#sec_A"><?php echo JText::_( 'MAIN_SECTION_FRONTEND' ); ?></a></li>
				<?php foreach ($this->sections as &$section){?>
				<li><a data-toggle="tab" style="height: 20px;"
						href="#sec_<?php echo $section->id;?>"><?php echo $section->name;?></a></li>
				<?php }?>
			</ul>
				<div id="editcell" class="tab-content">
					<div id="sec_A" class="tab-pane fade in form-horizontal active">
						<div class="span3">
							<blockquote><?php echo JText::_('MAIN_SECTION_FRONTEND_FIELDS');?></blockquote>
							<ul class="unstyled">
							<?php
							
foreach ( $this->allfields as $field ) {
								if (! in_array ( $field->id, $arr_fields_in_section )) {
									?>
							<li><?php echo $field->var; ?> (<?php echo $field->name; ?>)</li>
							<?php
								
}
							}
							?>
						</ul>
							<blockquote><?php echo JText::_('MAIN_SECTION_FRONTEND_SECTIONS');?></blockquote>
							<ul class="unstyled">				
							<?php foreach ($this->sections as &$section){?>
								<li><?php echo $section->name; ?> (SECTION)</li>
							<?php } ?>
						</ul>
						</div>
						<div class="span8">
					<?php
					$editor = JFactory::getEditor ();
					echo $editor->display ( 'layout_A', $this->item ['layout'], '550', '600', '100', '6' );
					?> 
					</div>
					</div>
				<?php foreach ($this->sections as &$section){?>
				<div id="sec_<?php echo $section->id;?>"
						class="tab-pane fade in form-horizontal">
						<div class="span3">
							<blockquote><?php echo JText::_('MAIN_SECTION_FRONTEND_FIELDS');?></blockquote>
							<ul class="unstyled">
							<?php
					$fields_temp = explode ( ',', $section->fields );
					$fields_temp = array_map ( 'trim', $fields_temp );
					$fields_temp = array_filter ( $fields_temp, 'strlen' );
					foreach ( $fields_temp as $field ) {
						?>
								<li><?php echo $this->allfields[$field]->var; ?> (<?php echo $this->allfields[$field]->name; ?>)</li>
							<?php } ?>
						</ul>
						</div>
						<div class="span8">
					<?php
					$editor = JFactory::getEditor ();
					echo $editor->display ( 'layout_' . $section->id, $section->layout, '550', '600', '100', '6' );
					?> 
					</div>
					</div>
				<?php } ?>
			</div>
			</div>
		</div>
		<div class="span3"
			style="border-left: 1px solid #ccc; padding-left: 42px;">
			<label><h3><?php echo JText::_('LAYOUT_POSITION');?></h3></label> <label><?php echo JText::_('LAYOUT_FORM_WIDTH')?></label>
			<input type="text" class="input-mini" name="form_layout_width"
				value="<?php echo $this->item['form_layout_width'];?>"> <label
				class="radio"> <input type="radio" name="layout_position" value="0"
				style="margin-top: 45px;"
				<?php echo ($this->item['layout_position']==0)?'checked':''; ?>> <img
				alt="<?php echo JText::_('LAYOUT_DEFAULT')?>"
				src="<?php echo JURI::base()?>components/com_calcbuilder/assets/img/layout_default.png">
			</label> <label class="radio"> <input type="radio"
				name="layout_position" value="1" style="margin-top: 45px;"
				<?php echo ($this->item['layout_position']==1)?'checked':''; ?>> <img
				alt="<?php echo JText::_('LAYOUT_UP')?>"
				src="<?php echo JURI::base()?>components/com_calcbuilder/assets/img/layout_up.png">
			</label> <label class="radio"> <input type="radio"
				name="layout_position" value="2" style="margin-top: 45px;"
				<?php echo ($this->item['layout_position']==2)?'checked':''; ?>> <img
				alt="<?php echo JText::_('LAYOUT_RIGHT')?>"
				src="<?php echo JURI::base()?>components/com_calcbuilder/assets/img/layout_right.png">
			</label> <label class="radio"> <input type="radio"
				name="layout_position" value="3" style="margin-top: 45px;"
				<?php echo ($this->item['layout_position']==3)?'checked':''; ?>> <img
				alt="<?php echo JText::_('LAYOUT_LEFT')?>"
				src="<?php echo JURI::base()?>components/com_calcbuilder/assets/img/layout_left.png">
			</label>
		</div>
	</div>
	<input type="hidden" name="id" value="<?php echo $this->item['id']; ?>" />
	<input type="hidden" name="idutility"
		value="<?php echo $this->item['id']; ?>" /> <input type="hidden"
		name="cid[]" value="<?php echo $this->item['id']; ?>" /> <input
		type="hidden" name="option" value="com_calcbuilder" /> <input
		type="hidden" name="task" value="storeLayout" /> <input type="hidden"
		name="boxchecked" value="0" /> <input type="hidden" name="controller"
		value="utility" /> <input type="hidden" name="check" value="post" /> <input
		type="hidden" name="class" value="0" />
<?php echo JHtml::_( 'form.token' ); ?>
</form>

