<?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
 */
// JHTML::script('TabPane.js', JURI::base().'components/com_calcbuilder/assets/js/', true);
$doc = JFactory::getDocument ();
$doc->addScript ( "https://www.google.com/jsapi" );
$pathImagenCSV = JURI::base () . 'components/com_calcbuilder/assets/img/export-csv.png';
$css = $this->__getCSS ();
foreach ( $css as $style ) {
	$doc->addStyleSheet ( $style );
}
$js = $this->__getJS ();
foreach ( $js as $jscript ) {
	?><script src="<?php echo $jscript;?>"></script><?php
}
?>
<style>
ul.tabs {
	margin: 0;
	padding: 0 0 2px 0;
}

li.tab {
	display: inline;
	padding: 4px .4em 2px .4em;
	list-style: none;
	background-color: #D0D0D0;
	border: 2px #333 solid;
	-moz-border-radius: 4px 4px 0 0;
	-webkit-border-radius: 4px 4px 0 0;
	border-radius: 4px 4px 0 0;
	cursor: pointer;
}

li.tab.active {
	background-color: #FAFAFA;
	border-bottom-color: #111;
}

li.tab .remove {
	color: #900;
}

div.content {
	margin: 0 0 1em 0;
	padding: .4em;
	border-top: 2px #333 solid;
}

.matriz {
	float: left;
	margin-left: 20px;
	margin-right: 20px;
	margin-top: 10px;
}

.header {
	font-size: 10px;
	font-weight: bold;
}

.row {
	font-size: 9px;
}

.oddrow {
	font-size: 9px;
	background-color: #F0F0F0;
}

.hoverrow {
	font-size: 9px;
	background-color: #D0D0D0;
}
</style>
<script type="text/javascript">
	window.addEvent('load',function(){
		jQuery('#navmenu').tabs();
	});

      // Load the Visualization API and the piechart package.
      //google.load('visualization', '1.0', {'packages':['corechart']});
      google.load('visualization', '1', {'packages':['table']});
      google.load('visualization', '1', {'packages': ['corechart']});
      

      var tableClassNames = {headerRow: 'header',tableRow:'row',oddTableRow:'oddrow',
    	      hoverTableRow:'hoverrow', selectedTableRow:'hoverrow'};
      
      
      // Set a callback to run when the Google Visualization API is loaded.
      google.setOnLoadCallback(drawChart);
      
      // Callback that creates and populates a data table, 
      // instantiates the pie chart, passes in the data and
      // draws it.
      function drawChart() {

      // Create the data table.
      var data = new google.visualization.DataTable();
      data.addColumn('string', '<?php echo JText::_('CALCULATORS')?>');
      data.addColumn('number', '<?php echo JText::_('REQUESTS')?>');
      <?php foreach ($this->general as $name=>$reqs){ ?>
	  data.addRow(['<?php echo $name;?>',<?php echo $reqs;?>]);
	  <?php } ?>
            
      // Set chart options
      var options = {'title':'<?php echo JText::_('TITLE_GENERAL_CHART')?>',
                     'width':400,
                     'height':250};

      // Instantiate and draw our chart, passing in some options.
      //var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
      var chart = new google.visualization.PieChart($('general_chart'));
      chart.draw(data, options);

      var comboChart = new google.visualization.ComboChart($('general_line_chart'));
      comboChart.draw(data, {
        title : '<?php echo JText::_('TITLE_GENERAL_CHART')?>',
        vAxis: {title: "<?php echo JText::_('REQUESTS')?>"},
        hAxis: {title: "<?php echo JText::_('CALCULATORS')?>"},
        seriesType: "bars",
        'width':400,
        'height':250,
        legend:'none'
      });
      /*var table = new google.visualization.Table($('general_table'));
      table.draw(data, {
          showRowNumber: false, 
          cssClassNames:tableClassNames,
    	  'height':250
      });*/
      
    }
function getCSV(idcalc){
	jQuery('#idcalc').val(idcalc);
	jQuery('#csvform').submit();
}
    </script>
<form action="index.php" method="post" id="csvform" name="csvform">
	<input type="hidden" name="option" value="com_calcbuilder" /> <input
		type="hidden" name="controller" value="stats" /> <input type="hidden"
		name="task" value="export" /> <input type="hidden" name="format"
		value="csv" /> <input type="hidden" name="boxchecked" value="0" /> <input
		type="hidden" name="idcalc" id="idcalc" value="" />
    <?php echo JHtml::_( 'form.token' ); ?>
    </form>
<form action="index.php" method="post" name="adminForm" id="adminForm">
	<div class="cb_table_wrapper" id="editcell">
		<div class="matriz" id="general_table">
			<div>
				<table class="table table-striped">
					<thead>
						<th><?php echo JText::_('REQUESTS')?></th>
						<th><?php echo JText::_('CALCULATORS')?></th>
					</thead>
					<tbody>
    				<?php foreach ($this->general as $name=>$reqs){ ?>
    					<tr>
							<td><?php echo $name;?></td>
							<td><?php echo $reqs;?></td>
						</tr>
    				<?php } ?>
    			</tbody>
				</table>
			</div>
		</div>
		<div id="general_chart" style="float: left;"></div>
		<div id="general_line_chart" style="float: left; margin-left: 15px"></div>
		<div style="clear: both; margin: 15px;">&nbsp;</div>
		<div id="navmenu">
			<ul class="nav nav-tabs">
    		<?php
						$active = true;
						foreach ( $this->stats as $calcName => $calcValue ) {
							?>
	    	<li <?php if ($active){ ?> class="active" <?php $active=false;}?>>
					<a data-toggle="tab" style="height: 20px;"
					href="#<?php
							
echo $this->utility [$calcName] ['id'];
							?>"><?php echo $calcName;?></a>
				</li>
	    	<?php }?>	        
	    </ul>	    
	    <?php
					$countChars = 0;
					$active = true;
					foreach ( $this->stats as $calcName => $calcValue ) {
						$idcalc = $this->utility [$calcName] ['id'];
						?>
	    <div
				class="tab-pane in <?php if ($active){ ?>active<?php $active=false;}?>"
				id="<?php echo $this->utility[$calcName]['id'];?>">
				<div>
					<h3>
						<img style="cursor: pointer;" src="<?php echo $pathImagenCSV;?>"
							onclick="getCSV('<?php echo $idcalc;?>')" />&nbsp;&nbsp;&nbsp;<?php echo $calcName;?></h3>
				</div>
	    	<?php foreach ($calcValue as $fieldsName=>$arrValues){ ?>	    		
	    		<div class="">
					<div id="table_<?php echo $countChars;?>"
						style="float: left; margin-left: 20px; margin-right: 20px"></div>
					<div id="chart_<?php echo $countChars;?>" style='float: left'></div>
				</div>
				<script>
		    		function drawChart_<?php echo $countChars;?>() {	
		    	      // Create the data table.
		    	      var data = new google.visualization.DataTable();
		    	      data.addColumn('string', '<?php echo $fieldsName;?>');
		    	      data.addColumn('number', '<?php echo JText::_('REQUESTS')?>');
		    	      <?php foreach ($arrValues as $values){ ?>
  		    		  data.addRow(['<?php echo str_replace("'","\"",$values->value);?>',<?php echo $values->req;?>]);
  		    		  <?php } ?>
		    	            
		    	      // Set chart options
		    	      var options = {'title':'<?php echo $fieldsName;?>',
		    	                     'width':260,
		    	                     'height':180};
	
		    	      // Instantiate and draw our chart, passing in some options.
		    	      //var chart = new google.visualization.PieChart(document.getElementById('chart_div'));
		    	      var chart = new google.visualization.PieChart($('chart_<?php echo $countChars;?>'));
		    	      chart.draw(data, options);		    	      		    	      
		    	    }
		    		window.addEvent('domready', function() {
		    			google.setOnLoadCallback(drawChart_<?php echo $countChars;?>);
		    		});
		    				    			    		
		    	</script>
	    	<?php
							$countChars ++;
						}
						?>
		    <div id="graltableTitle_<?php echo $idcalc?>"
					style="clear: both; width: 100%; margin: 15px; text-align: center">
					<h2><?php echo JText::_('DATA_LOG')?></h2>
				</div>
				<div style="height: 300px; overflow: auto">
					<table class="table table-bordered table-condensed table-striped">
						<thead>
	    				<?php
						$arrFields = array ();
						foreach ( $this->gridfields [$idcalc] as $fields ) {
							?>
  		    		    <th><?php echo $fields?></th>  		    		    
  		    		  	<?php
							
$arrFields [] = $fields;
						}
						?>
	    			</thead>
						<tbody>
	    				<?php
						
foreach ( $this->grid [$idcalc] as $val ) {
							?><tr><?php
							for($j = 0; $j < count ( $arrFields ); $j ++) {
								$name = $arrFields [$j];
								?><td><?php echo isset($val[$name])?$val[$name]:'';?></td><?php
							}
						}
						?>
	    			
						
						</tbody>
					</table>
				</div>
			</div>          	
    	<?php }?>
    	 
	</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="stats" />
<?php echo JHtml::_( 'form.token' ); ?>
</form>