<?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 ();
$css = $this->__getCSS ();
foreach ( $css as $style ) {
	$document->addStyleSheet ( $style );
}
$js = $this->__getJS ();
foreach ( $js as $jscript ) {
	?><script src="<?php echo $jscript;?>"></script><?php
}
$exceldata=$this->excel['excel'];
$excelmapping=null;
if(isset($this->excel['mapping']))
	$excelmapping=$this->excel['mapping'];
?>
<script type="text/javascript">
<!--
jQuery(document).ready(function($){
	$('#addinput').click(function(){
		$('#inputmapping').append($('#inputClone').clone().html());
		deleterow();
	});	
	$('#addoutput').click(function(){
		$('#outputmapping').append($('#outputClone').clone().html());
		deleterow();
	});	
	$('#inputExecution').change(function(){
		if ($('#inputExecution option:selected').val()==2){
			$('#excelcustomcode').show();
		}else{
			$('#excelcustomcode').hide();
		}
	});
	<?php if(is_null($exceldata)||$exceldata->excelexec!=2):?>
		$('#excelcustomcode').hide();
	<?php endif;?>
	deleterow();
	$('.cb_radio button').click(function(){			
		$(this).closest('.cb_radio').find('.radio_hidden').val($(this).attr('aria-label'));
	});
});
function deleterow(){
	jQuery('.deleterow').each(function(index,elem){
		jQuery(elem).unbind('click');
		jQuery(elem).click(function(){
			jQuery(this).closest('.maprow').remove();
		});
	});
}
function createForm(){
	var ret=new Object();
	var input=new Array();
	jQuery('#inputmapping .maprow').each(function(index,elem){
		input.push(getMapRow(elem));
	});
	var output=new Array();
	jQuery('#outputmapping .maprow').each(function(index,elem){		
		output.push(getMapRow(elem));
	});
	ret.input=input;
	ret.output=output;
	jQuery('#adminForm').find('input[name="mapping"]').val(JSON.stringify(ret));
}
function getMapRow(elem){
	var objTmp=new Object();
	objTmp.sheet=jQuery(elem).find('input[name="sheet"]').val();
	objTmp.cell=jQuery(elem).find('input[name="cell"]').val();
	objTmp.phpvar=jQuery(elem).find('input[name="phpvar"]').val();
	return objTmp;
}
/* 1.5 */
function submitbutton(pressbutton) {	   
    createForm();
}
//window.addEvent('domready', function() {
jQuery(document).ready(function(){
  /*$('adminForm').addEvent('submit',function(event){
 	 createForm();
	});*/	
  Joomla.submitbutton = function(task) {
	   	if (task == '') 	{
	   		return false;
	   	}  	else  	{
	   		createForm();
		   	 Joomla.submitform(task);
	   	}
	};
});


//-->
</script>
	<div id="editcell" class="cb_table_wrapper form-horizontal">
		<form action="index.php" method="post" name="adminForm" id="adminForm" enctype="multipart/form-data">
		  <div class="control-group">
		    <label class="control-label" for="inputFile"><?php echo JText::_('EXCEL FILE');?></label>
		    <div class="controls">
		      <input type="file" id="inputFile" name="excelfile">
		      <?php if (!is_null($exceldata)&&isset($exceldata->excelfile)):?>
		      <label><h4><?php echo JText::_('EXCEL_FILE_UPLOADED');?></h4> <?php echo $exceldata->excelfile;?></label>
		      <?php endif;?>
		    </div>
		  </div>
		  <div class="control-group">
		    <label class="control-label" for="inputCache"><?php echo JText::_('EXCEL_CACHE');?></label>
		    <div class="controls">
		      <div class="btn-group cb_radio" data-toggle="buttons-radio">
					<button type="button" class="btn btn_no <?php echo (!is_null($exceldata)&&$exceldata->cache==0)?'active':'';?>" aria-label="0">NO</button>
					<button type="button" class="btn btn_yes <?php echo (!is_null($exceldata)&&$exceldata->cache==1)?'active':'';?>" aria-label="1">YES</button>
					<input type="hidden" class="radio_hidden" name="cache" value="<?php echo !is_null($exceldata)?$exceldata->cache:'';?>">
				</div>
		    </div>		    
		  </div>
		  <div class="control-group">
		    <label class="control-label" for="inputHTMLout"><?php echo JText::_('EXCEL_HTML');?></label>
		    <div class="controls">
		      <div class="btn-group cb_radio" data-toggle="buttons-radio">
					<button type="button" class="btn btn_no <?php echo (!is_null($exceldata)&&$exceldata->htmloutput==0)?'active':'';?>" aria-label="0">NO</button>
					<button type="button" class="btn btn_yes <?php echo (!is_null($exceldata)&&$exceldata->htmloutput==1)?'active':'';?>" aria-label="1">YES</button>
					<input type="hidden" class="radio_hidden" name="htmloutput" value="<?php echo !is_null($exceldata)?$exceldata->htmloutput:'';?>">
					<div class="input-prepend" style="margin-left:15px;padding-top: 4px;">
					  <span class="add-on"><?php echo JText::_('EXCEL_SHEET');?></span>
					  <input type="text" name="htmloutputpage" class="span2" value="<?php echo !is_null($exceldata)?$exceldata->htmloutputpage:'';?>">
					</div>					
				</div>
		    </div>		    
		  </div>
		  <div class="control-group">
		    <label class="control-label" for="inputPDFout"><?php echo JText::_('EXCEL_PDF');?></label>
		    <div class="controls">
		      <div class="btn-group cb_radio" data-toggle="buttons-radio">
					<button type="button" class="btn btn_no <?php echo (!is_null($exceldata)&&$exceldata->pdfoutput==0)?'active':'';?>" aria-label="0">NO</button>
					<button type="button" class="btn btn_yes <?php echo (!is_null($exceldata)&&$exceldata->pdfoutput==1)?'active':'';?>" aria-label="1">YES</button>
					<input type="hidden" class="radio_hidden" name="pdfoutput" value="<?php echo !is_null($exceldata)?$exceldata->pdfoutput:'';?>">
					<div class="input-prepend" style="margin-left:15px;padding-top: 4px;">
					  <span class="add-on"><?php echo JText::_('EXCEL_SHEET');?></span>
					  <input type="text" name="pdfoutputpage" class="span2" value="<?php echo !is_null($exceldata)?$exceldata->pdfoutputpage:'';?>">
					</div>							
				</div>
		    </div>		    
		  </div>
		  <div class="control-group">
		    <label class="control-label" for="inputPDFout"><?php echo JText::_('EXCEL_EXCEL');?></label>
		    <div class="controls">
		      <div class="btn-group cb_radio" data-toggle="buttons-radio">
					<button type="button" class="btn btn_no <?php echo (!is_null($exceldata)&&$exceldata->exceloutput==0)?'active':'';?>" aria-label="0">NO</button>
					<button type="button" class="btn btn_yes <?php echo (!is_null($exceldata)&&$exceldata->exceloutput==1)?'active':'';?>" aria-label="1">YES</button>
					<input type="hidden" class="radio_hidden" name="exceloutput" value="<?php echo !is_null($exceldata)?$exceldata->exceloutput:'';?>">												
				</div>
		    </div>		    
		  </div>
		  <div class="control-group">
		    <label class="control-label" for="inputExecution"><?php echo JText::_('EXCEL_EXECUTION');?></label>
		    <div class="controls">
		      <select name="excelexec" id="inputExecution">
		      	<option value="0" <?php echo (!is_null($exceldata)&&$exceldata->excelexec==0)?'selected="selected"':''?>><?php echo JText::_('EXCEL_EXEC_INIT');?></option>
		      	<option value="1" <?php echo (!is_null($exceldata)&&$exceldata->excelexec==1)?'selected="selected"':''?>><?php echo JText::_('EXCEL_EXEC_END');?></option>
		      	<option value="2" <?php echo (!is_null($exceldata)&&$exceldata->excelexec==2)?'selected="selected"':''?>><?php echo JText::_('EXCEL_EXEC_CUSTOM');?></option>
		      </select>
		      <label id="excelcustomcode"><h4><?php echo JText::_('EXCEL_CUSTOM_CODE');?></h4></label>
		    </div>		    
		  </div>
		  <div class="control-group">
					<label class="control-label"><?php echo JText::_( 'EXCEL_LOCALE' ); ?></label>
					<div class="controls">
						<select name="locale">
							<option value="cs" <?php echo (!is_null($exceldata)&&$exceldata->locale=='cs')?'selected="selected"':'';?>><?php echo JText::_( 'Czech Republic' ); ?></option>
							<option value="da" <?php echo (!is_null($exceldata)&&$exceldata->locale=='da')?'selected="selected"':'';?>><?php echo JText::_( 'Denmark' ); ?></option>
							<option value="de" <?php echo (!is_null($exceldata)&&$exceldata->locale=='de')?'selected="selected"':'';?>><?php echo JText::_( 'Germany' ); ?></option>
							<option value="en_uk" <?php echo (!is_null($exceldata)&&$exceldata->locale=='en_uk')?'selected="selected"':'';?>><?php echo JText::_( 'English (United Kingdom)' ); ?></option>
							<option value="en_us" <?php echo (!isset($exceldata->locale)||$exceldata->locale=='en_us')?'selected="selected"':'';?>><?php echo JText::_( 'English (USA)' ); ?></option>
							<option value="es" <?php echo (!is_null($exceldata)&&$exceldata->locale=='es')?'selected="selected"':'';?>><?php echo JText::_( 'Spanish' ); ?></option>
							<option value="fi" <?php echo (!is_null($exceldata)&&$exceldata->locale=='fi')?'selected="selected"':'';?>><?php echo JText::_( 'Finland' ); ?></option>
							<option value="hu" <?php echo (!is_null($exceldata)&&$exceldata->locale=='hu')?'selected="selected"':'';?>><?php echo JText::_( 'Hungary' ); ?></option>
							<option value="it" <?php echo (!is_null($exceldata)&&$exceldata->locale=='it')?'selected="selected"':'';?>><?php echo JText::_( 'Italy' ); ?></option>
							<option value="nl" <?php echo (!is_null($exceldata)&&$exceldata->locale=='nl')?'selected="selected"':'';?>><?php echo JText::_( 'Netherlands' ); ?></option>
							<option value="no" <?php echo (!is_null($exceldata)&&$exceldata->locale=='no')?'selected="selected"':'';?>><?php echo JText::_( 'Norway' ); ?></option>
							<option value="po" <?php echo (!is_null($exceldata)&&$exceldata->locale=='po')?'selected="selected"':'';?>><?php echo JText::_( 'Poland' ); ?></option>
							<option value="pt" <?php echo (!is_null($exceldata)&&$exceldata->locale=='pt')?'selected="selected"':'';?>><?php echo JText::_( 'Portuguese' ); ?></option>
							<option value="pt_br" <?php echo (!is_null($exceldata)&&$exceldata->locale=='pt_br')?'selected="selected"':'';?>><?php echo JText::_( 'Brazil' ); ?></option>
							<option value="ru" <?php echo (!is_null($exceldata)&&$exceldata->locale=='ru')?'selected="selected"':'';?>><?php echo JText::_( 'Russian' ); ?></option>
							<option value="sv" <?php echo (!is_null($exceldata)&&$exceldata->locale=='sv')?'selected="selected"':'';?>><?php echo JText::_( 'Sweden' ); ?></option>
							<option value="tr" <?php echo (!is_null($exceldata)&&$exceldata->locale=='tr')?'selected="selected"':'';?>><?php echo JText::_( 'Turkey' ); ?></option>
						</select>
					</div>
				</div>
		  <input type="hidden" name="option" value="com_calcbuilder" /> 
		<input	type="hidden" name="task" value="show" /> 
		<input type="hidden" name="boxchecked" value="0" /> 
		<input type="hidden" name="controller"	value="excel" /> 
		<input type="hidden" name="idobj"	value="<?php echo jRequest::getVar('idobj')?>" />
		<input type="hidden" name="id" value="<?php echo $exceldata?$exceldata->id:'';?>" />		
		<input type="hidden" name="mapping" value="" />
		<?php echo JHtml::_( 'form.token' ); ?>
		</form>
		  <div class="control-group">		    
		      <label class="control-label"><?php echo JText::_('EXCEL_MAPPING')?></label>		      
		      <div class="controls">
		      	<div class="row-fluid">
		      		<div class="span6" id="input">
		      			<div class="span12"><h3><?php echo JText::_('EXCEL_MAPPING_INPUT');?></h3></div>
		      			<div class="span12"><button class="btn btn-mini" type="button" id="addinput"><?php echo JText::_('ADD')?></button></div>
		      			<div class="span12 maprowwrapper">		      				
							<div class="span2"></div>
		      				<div class="span4"><h5><?php echo JText::_('EXCEL_CODE_VAR');?></h5></div>
		      				<div class="span1"><h5>>></h5></div>
			      			<div class="span3"><h5><?php echo JText::_('EXCEL_SHEET');?></h5></div>
							<div class="span2"><h5><?php echo JText::_('EXCEL_CELL');?></h5></div>
						</div>
						<div class="span12 row-fluid maprowwrapper" id="inputmapping">
						<?php if($excelmapping):?>
							<?php foreach ($excelmapping as $mapping):?>
								<?php if ($mapping->type==0):?>							
								<div class="span12 maprow">
									<div class="span2"><button class="btn btn-danger deleterow" ><i class="icon-remove" style="width: 12px;height: 14px;"></i></button></div>
									<div class="span4"><input type="text" class="input-mini" name="phpvar" value="<?php echo $mapping->phpvar?>"/></div>
									<div class="span1">>></div>
									<div class="span3"><input type="text" class="input-mini" name="sheet" value="<?php echo $mapping->sheet;?>"/></div>
									<div class="span2"><input type="text" class="input-mini" name="cell" value="<?php echo $mapping->cell;?>"/></div>
								</div>
								<?php endif;?>
							<?php endforeach;?>
						<?php endif;?>
						</div>
		      		</div>
		      		<div class="span6" id="output">
		      			<div class="span12"><h3><?php echo JText::_('EXCEL_MAPPING_OUT');?></h3></div>
		      			<div class="span12"><button class="btn btn-mini" type="button" id="addoutput"><?php echo JText::_('ADD')?></button></div>
		      			<div class="span12 maprowwrapper">
		      				<div class="span2"></div>
			      			<div class="span3"><h5><?php echo JText::_('EXCEL_SHEET');?></h5></div>
							<div class="span2"><h5><?php echo JText::_('EXCEL_CELL');?></h5></div>
							<div class="span1"><h5>>></h5></div>
							<div class="span4"><h5><?php echo JText::_('EXCEL_CODE_VAR');?></h5></div>
						</div>
						<div class="span12 row-fluid maprowwrapper" id="outputmapping">
						<?php if($excelmapping):?>
							<?php foreach ($excelmapping as $mapping):?>
								<?php if ($mapping->type==1):?>
								<div class="span12 maprow">
									<div class="span2"><button class="btn btn-danger deleterow" ><i class="icon-remove" style="width: 12px;height: 14px;"></i></button></div>
									<div class="span3"><input type="text" class="input-mini" name="sheet" value="<?php echo $mapping->sheet;?>"/></div>
									<div class="span2"><input type="text" class="input-mini" name="cell" value="<?php echo $mapping->cell;?>"/></div>
									<div class="span1">>></div>
									<div class="span4"><input type="text" class="input-mini" name="phpvar" value="<?php echo $mapping->phpvar?>"/></div>
								</div>
								<?php endif;?>
							<?php endforeach;?>
						<?php endif;?>
						</div>
		      		</div>
		      	</div>
		      </div>		    
		  </div>
	</div>
<div id="outputClone" style="display:none">
	<div class="span12 maprow">
		<div class="span2"><button class="btn btn-danger deleterow" ><i class="icon-remove" style="width: 12px;height: 14px;"></i></button></div>
		<div class="span3"><input type="text" class="input-mini" name="sheet"/></div>
		<div class="span2"><input type="text" class="input-mini" name="cell"/></div>
		<div class="span1">>></div>
		<div class="span4"><input type="text" class="input-mini" name="phpvar"/></div>
	</div>
</div>
<div id="inputClone" style="display:none">
	<div class="span12 maprow">
		<div class="span2"><button class="btn btn-danger deleterow" ><i class="icon-remove" style="width: 12px;height: 14px;"></i></button></div>
		<div class="span4"><input type="text" class="input-mini" name="phpvar"/></div>
		<div class="span1">>></div>
		<div class="span3"><input type="text" class="input-mini" name="sheet"/></div>
		<div class="span2"><input type="text" class="input-mini" name="cell"/></div>
	</div>
</div>

