<?php
	
	$CI =& get_instance();
	$year = $CI->uri->segment(3);
	$period = $CI->uri->segment(4);
	$CI->load->model('Compfinancialyear');
	if($year == '')	$year = date("Y");
	if($period == ''){
		$query = $CI->Compfinancialyear->currPeriod();
		if($query){
			$period = $query[0]->period;
		}else{
			$period = 1;
		}
	}

	$yrs = $CI->Compfinancialyear->get_financialYear();
	$query = $CI->Compfinancialyear->financialYear_year($year);
	if($query){
		$pers = $query;
	}else{
		$pers = NULL;
	}

	$CI->load->model('graph_model');
	//$gstSummary = $CI->graph_model->gst03summary($year,$period);
	//print_r($gstSummary);
	
?>
<!--Account Balance -->                   
<div class="row-fluid" style="">
	<div class="span12 scCol">
		<div class="block" style="position:relative">
			<div class="head">                                        
				<h2><?php echo $this->lang->line('gst03summary')?></h2>
				<ul class="buttons fr">
					<li><a href="#" class="block_loading"><span class="i-cycle"></span></a></li>
					<li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
				</ul>
				<div class="span6 fr" style="margin-top:5px;">
					<?php
						$preselyear = $year;
						$options = '';
						$options[] = $this->lang->line('selectyear');
						if(isset($yrs) && !empty($yrs)){	
							foreach($yrs as $row){
								$options[$row] = $row;
							}
						}
						$js='onchange="selectYearPeriod(this)" class="input-medium span4" id="add_year"';

						echo form_dropdown('add_year', $options, $preselyear, $js);
					?>   
					<?php
						$preselperiod = $period;
						$options = '';
						$options[] = $this->lang->line('period');
						if($pers){
							foreach($pers as $row){
								$options[$row->period] = $row->financialPeriod;
							}
						}
						$js='onchange="drawGraph()" class="input-medium span4" id="start_per"';

						echo form_dropdown('start_per', $options, $preselperiod, $js);
					?>
					<?php
						$preselperiod = $period;
						$options = '';
						$options[] = $this->lang->line('period');
						if($pers){
							foreach($pers as $row){
								$options[$row->period] = $row->financialPeriod;
							}
						}
						$js='onchange="drawGraph()" class="input-medium span4" id="end_per"';

						echo form_dropdown('end_per', $options, $preselperiod, $js);
					?>
				</div>
			</div>
			<div class="content" style="height:500px;">
				<div id="gst03summary" style=" margin: 0px auto;"></div>
	        </div>
	        <div class="ajaxLoader span12" style="display:none;text-align:center; height:100%;margin:0; position:absolute; left:0; top:0; z-index:1111; background:#85c420;">
                <img style="margin:90px auto;" src="<?php echo base_url(); ?>images/ajax-loader.gif" title="Ajax Loader" alt="Ajax Loader" />
            </div>
	        <script>
	        	$(document).ready(function(){
	        		$('select').select2();
	        		drawGraph();
	        	});
	        	var gstSummaryData = new Array();
	        	function drawGraph(){
	        		userCheckFlag = false;
	        		$('.ajaxLoader').css({display:'block',opacity:'.3'});
	        		var $year = $('#add_year').val();
	        		var $start = $('#start_per').val();
	        		var $end = $('#end_per').val();
	        		$.ajax({
					    type      : 'POST', 
					    url       : '<?php echo base_url(); ?>gst/getGstSummaryPeriodRange',
					    data      : {
					    				year:$year,
					    				start:$start,
					    				end:$end
					    			},
					    dataType  : 'json',
					    success   : function(data) {
	        				$('.ajaxLoader').css({display:'none'});
					    	gstSummaryData = data;
					    	drawChart();
					    }            
					});
	        	}

	        	function selectYearPeriod(obj){
						var year = obj.value;
				//		alert(year);
							
							$.ajax({
				  				type: "POST",
					  			dataType: "json",
								url: "<?php echo base_url()?>glreports/get_TrialBalance_year/"+year,
								data: year,
					  			async: false,
				  				success: function(content){
									if(content.status == "success") {
									var items = [];
									for ( var i = 0; i < content.message.length; i++) {
									items.push('<option value="'+content.message[i].period+'">'
										+ content.message[i].financialPeriod + '</option>');
									}  //end for
									jQuery("#start_per,#end_per").empty();
									jQuery("#start_per,#end_per").append(items.join('</br>'));
									//jQuery("#add_period").val(null);
									$("#start_per,#end_per").select2();
								} else {
									jQuery("#start_per,#end_per").empty();
									$("#error").html('<p>'+content.message+'</p>');
									$("#start_per,#end_per").select2('val',null);
								} //end if
							} //end success
								
							}); //end ajax
				//	window.location.reload(true); 
				}
	  
			    google.load("visualization", "1.1", {packages:["bar"]});
				google.setOnLoadCallback(drawChart);
				function drawChart() {
				var arr = [['Period', 'B7', 'B8']];
				$.each(gstSummaryData,function(key, value){
					arr.push(['Period '+value.period,value.totalB7,value.totalB8]);
				});
				var data = google.visualization.arrayToDataTable(arr);

				var options = {
				  chart: {
				    title: 'GST 03 Summary',
				  },
				  axes: {
		            y: {
		              0: { label: 'Amount'} // Top x-axis.
		            }
		          },
		          height: 500,
				  bars: 'vertical' // Required for Material Bar Charts.
				};

				var chart = new google.charts.Bar(document.getElementById('gst03summary'));

				chart.draw(data, options);
				}

			    $(window).resize(function(){
			        drawChart();
			    });
			</script>
	    </div>
	</div>
</div>