<?php
	$uid = $this->session->userdata('usertype_id');
	$profile_id = $this->session->userdata('profile_id');
	if($uid==1)
		$data = array("Export");
	else
	{
		$data = array();
		$va1l = $this->db;
		$va1l->select("setting_name");
		$va1l->where("profile_id",$profile_id);
		$va1l->where("setting_name !=",'main');
		$va1l->where("menu_id",'848492');
		$va1l ->where("del_date",NULL);
		$res_val = $va1l->get("mst_user_profile_setting");
		foreach($res_val ->result_array() as $row)
		{
			$data[] = $row['setting_name'];
			
		}
	
	}

	$date_format = $this->session->userdata('date_format');  
	$time_format = $this->session->userdata('time_format');  
	$js_date_format = $this->session->userdata('js_date_format');  
	$js_time_format = $this->session->userdata('js_time_format');  
	 
?>
<style>
#load_ac_report_grid
{
	display:none !important; 
}
</style>
<script type="text/javascript">
loadMultiSelectDropDown();
jQuery().ready(function (){
	jQuery("#ac_reportsdate").datepicker({dateFormat:"<?php echo $js_date_format; ?>",changeMonth: true,changeYear: true});
	jQuery("#ac_reportedate").datepicker({dateFormat:"<?php echo $js_date_format; ?>",changeMonth: true,changeYear: true});

	jQuery("input:button, input:submit, input:reset").button();
	jQuery("#ac_report_grid").jqGrid({
		url:"<?php echo base_url(); ?>index.php/ac_report/loadData",
		datatype: "local",
		colNames:["<?php echo $this->lang->line("ID"); ?>", '<?php echo $this->lang->line("Device"); ?>','<?php echo $this->lang->line("Ac_On_Time"); ?>','<?php echo $this->lang->line("Ac_Off_Time"); ?>', '<?php echo $this->lang->line("Duration"); ?>'],
		colModel:[
			{name:"id",index:"id",hidden:true, width:15, jsonmap:"id"},
			{name:"asset",editable:true, index:"asset", width:0, align:"left", jsonmap:"asset"},
			{name:"ac_on",editable:true, index:"ac_on", width:260, align:"left", jsonmap:"ac_on", formatter: 'date', formatoptions:{srcformat:"Y-m-d H:i:s",newformat:"<?php echo $date_format.' '.$time_format; ?>"}},
			{name:"ac_off",editable:true, index:"ac_off", width:260, align:"left", jsonmap:"ac_off", formatter: 'date', formatoptions:{srcformat:"Y-m-d H:i:s",newformat:"<?php echo $date_format.' '.$time_format; ?>"}},
			
			{name:"duration",editable:true, index:"duration", width:200, align:"left", jsonmap:"duration"}
			
		], 
		rowNum:grid_paging,
		height: 'auto',
               
		rownumbers: true,
		autowidth: true,
		shrinkToFit: false,
		rowList:[10,20,30,50,100,10000],
		pager: jQuery("#ac_report_pager"),
		sortname: "dm.device_id",
		loadComplete: function(){
			$("#loading_top").css("display","none");
			$("#ac_report_grid").setGridParam({datatype: 'json'}); 
		},
		loadBeforeSend: function(){$("#loading_top").css("display","block");},
		viewrecords: true,
		multiselect: false, 
		sortorder: "ASC",
		grouping: true,
		groupingView : { 
			groupField : ['asset'],
			groupColumnShow : [false],
			groupText : ['<b>{0}</b>'],
			groupCollapse : false,
			groupSummary : [true],
			showSummaryOnHide: true
                        
		},
                groupingView : { 
			groupField : ['asset'],
			groupColumnShow : [false],
			groupText : ['<b>{0}</b>'],
			groupCollapse : false,
			groupSummary : [true],
			showSummaryOnHide: true
                        
		},
		footerrow: false,
		userDataOnFooter: false,
		caption:"AC Report",
		editurl:"users/deleteData",
		jsonReader: { repeatitems : false, id: "0" }
	});
  
	jQuery("#ac_report_grid").jqGrid("navGrid", "#ac_report_pager", {add:false, edit:false, del:false, search:false}, {}, {}, {}, {multipleSearch:false});
	$("#ac_report_pager option[value=10000]").text('All');
	$("#ac_report_pager .ui-pg-selbox").change(function(){
		grid_paging=$("#v_pager .ui-pg-selbox").val();
		//alert(grid_paging);
	});
	<?php
	if(in_array('Export',$data)){
	?>
	jQuery("#ac_report_grid").jqGrid("navButtonAdd","#ac_report_pager",{caption:"<?php echo $this->lang->line("Export"); ?>",
		onClickButton:function(){
			
			var sdate = $('#ac_reportsdate').val();
			var edate = $('#ac_reportedate').val();
			var group = $('#group_run_report').val();
			//var device = $('#ac_reportdevice').val();
			var dev="";
			for(i=0;i<=assets_count;i++){
				if($("#ddcl-ac_reportdevice-i"+i).is(':checked')){
					dev+=$("#ddcl-ac_reportdevice-i"+i).val()+",";
				}
			}
			if(dev == ''){
				$("#alert_dialog").html('<?php echo $this->lang->line("Please select device"); ?>');
				$("#alert_dialog").dialog("open");
				return false;
			}
			var myPostData = $('#ac_report_grid').jqGrid("getGridParam", "postData");
			var sidx = myPostData.sidx;
			var sord = myPostData.sord;
			// sdate="+sdate+"&edate="+edate+"&
			var qrystr ="/export?sdate="+sdate+"&edate="+edate+"&group="+group+"&device="+dev+"&sidx="+sidx+"&sord="+sord;
			document.location = "<?php echo base_url(); ?>index.php/ac_report/loadData"+qrystr;
		}
	});
	/*jQuery("#ac_report_grid").jqGrid("navButtonAdd","#ac_report_pager",{caption:"<?php echo $this->lang->line("pdf"); ?>",
		onClickButton:function(){
			
			var sdate = $('#ac_reportsdate').val();
			var edate = $('#ac_reportedate').val();
			var group = $('#group_run_report').val();
			//var device = $('#ac_reportdevice').val();
			var dev="";
			for(i=0;i<=assets_count;i++){
				if($("#ddcl-ac_reportdevice-i"+i).is(':checked')){
					dev+=$("#ddcl-ac_reportdevice-i"+i).val()+",";
				}
			}
			if(dev == ''){
				$("#alert_dialog").html('<?php echo $this->lang->line("Please select device"); ?>');
				$("#alert_dialog").dialog("open");
				return false;
			}
		
			var qrystr ="/export?sdate="+sdate+"&edate="+edate+"&group="+group+"&device="+dev;
			document.location = "<?php echo base_url(); ?>index.php/reports/ac_report/export_pdf"+qrystr;
		}
	});*/
	/*jQuery("#ac_report_grid").jqGrid("navButtonAdd","#ac_report_pager",{caption:"Pdf",
		onClickButton:function(){
			var sdate = $('#ac_reportsdate').val();
			var edate = $('#ac_reportedate').val();
			var group = $('#group_run_report').val();
			var qrystr ="/pdf?sdate="+sdate+"&edate="+edate+"&device="+group;
			
			window.open("<?php echo site_url('ac_report/export_pdf'); ?>"+qrystr,'_blank');
			
		}
	});*/
	<?php } ?>
	$("#ac_reportsdate").datepicker('setDate', new Date('<?php echo date("Y/m/d H:i:s"); ?>'));
	$("#ac_reportedate").datepicker('setDate', new Date('<?php echo date("Y/m/d H:i:s"); ?>'));
	$("#loading_top").css("display","none");
	$("#ac_report_alert").dialog({
	  autoOpen: false,
	  modal: true
	});
	$("#ac_reportdevice").html(assets_combo_opt_report);
	$("#ac_reportdevice").dropdownchecklist({ firstItemChecksAll: true, textFormatFunction: function(options) {
                var selectedOptions = options.filter(":selected");
                var countOfSelected = selectedOptions.size();
                switch(countOfSelected) {
                    case 0: return "<i><?php echo $this->lang->line("Please Select"); ?><i>";
                    case 1: return selectedOptions.text();
                    case options.size(): return "<b><?php echo $this->lang->line("All Assets"); ?></b>";
                    default: return countOfSelected + " Assets";
                }
            }, icon: {}, width: 150});
	$("#ddcl-ac_reportdevice").css('vertical-align','middle');
	$("#ddcl-ac_reportdevice-ddw").css('overflow-x','hidden');
	$("#ddcl-ac_reportdevice-ddw").css('overflow-y','auto');
	$("#ddcl-ac_reportdevice-ddw").css('height','200px');
	$(".ui-dropdownchecklist-dropcontainer").css('overflow','visible');
});
function searchac_report(){
	
	var sdate = $('#ac_reportsdate').val();
	var edate = $('#ac_reportedate').val();
	//var device = $('#ac_reportdevice').val();
	var group = $('#group_run_report').val();
	var dev="";
	for(i=0;i<=assets_count;i++){
		if($("#ddcl-ac_reportdevice-i"+i).is(':checked')){
			dev+=$("#ddcl-ac_reportdevice-i"+i).val()+",";
		}
	}
	if(dev == ''){
		$("#alert_dialog").html('<?php echo $this->lang->line("Please select device"); ?>');
		$("#alert_dialog").dialog("open");
		return false;
	}
	$("#loading_top").css("display","block");	
	jQuery("#ac_report_grid").jqGrid('setGridParam',{postData:{group:group, sdate:sdate, edate:edate, device:dev, page:1}}).trigger("reloadGrid");
	
	return false;	
}
function cancel(){
	$('#ac_report_frm').html('');
	$('#ac_report_list_div').show();
}

function viewOnMap(){
	
	
	var device = $('#device').val();
	if(device == ""){
		alert('<?php echo $this->lang->line("Please select device"); ?>');
		return false;
	}
	
	$('#tabs').tabs('add', "live/device/window/current/id/"+device, '<?php echo $this->lang->line("View_on_Map"); ?>', 1);

	$("#divAjaxIndex").dialog('open');
	var start_date = $('#sdate').val();
	var end_date = $('#edate').val();
	$.post("<?php echo base_url(); ?>index.php/ac_report/trackOnMap", { device: device, start_date: start_date, end_date: end_date },
	 function(data) {
		if(data){
			var latArr = new Array();
			var lngArr = new Array();
			var htmlArr = new Array();
			var lat = data.lat;
			var lng = data.lng;
			var html = data.html;
			if(lat.length > 0){
				for(i=0; i<lat.length; i++){
					latArr.push(lat[i]);
					lngArr.push(lng[i]);
					htmlArr.push(html[i]);
				}
				var devText = $('#device option:selected').text();
				var distance = data.distance;
				var txt = devText + " Distance : " + distance + " KM";
				viewTrack(latArr,lngArr,htmlArr, txt);
				
			}else{
				alert("<?php echo $this->lang->line("No_Data_Found"); ?>");
			}
			$("#divAjaxIndex").dialog('close');
		}
	 }, 'json'
	);
	
}
</script>
<?php
	$timestamp=date("d.m.Y");
	$timestamp = strtotime("+2 day");
	$tomorrow=strftime( "%d.%m.%Y",$timestamp); 
?>
<div id="ac_report_list_div">
<form onsubmit="return searchac_report()">
<table width="100%" class="formtable" style="margin-bottom: 5px;">
	<tr>
		<td width="10%"><?php echo $this->lang->line("Start"); ?> :<br> <input type="text" name="sdate" id="ac_reportsdate" class="date text ui-widget-content ui-corner-all" style="width:80px" readonly="readonly"/></td>
		<td width="10%"><?php echo $this->lang->line("End"); ?> :<br> <input type="text" name="edate" id="ac_reportedate" class="date text ui-widget-content ui-corner-all" style="width:80px" readonly="readonly"/></td>
		<td width="15%"><?php echo $this->lang->line("Group"); ?> :<br> <select onchange="filterAssetsCombo(this.value,'ac_reportdevice')" style="width:175px !important;" name="group" id="group_run_report" class="select ui-widget-content ui-corner-all" ><option value=''><?php echo $this->lang->line("Please Select"); ?></option><?php echo $group; ?></select></td>
		<td width="20%"><?php echo $this->lang->line("Assets"); ?> :<br><select name="device" id="ac_reportdevice" class="select ui-widget-content ui-corner-all" multiple='multiple'></select></td>
		<td width="10%"><input type="submit" value="<?php echo $this->lang->line("view"); ?>"/></td>
	</tr>
</table>
</form>
<table id="ac_report_grid" class="jqgrid"></table>
<div id="ac_report_pager"></div>
<div id="ac_report_alert"></div>
</div>
<script type="text/javascript">
	<?php /* google analytic code. */ ?>
	var _gaq = _gaq || [];
	_gaq.push(['_setAccount', 'UA-37380597-1']);
	_gaq.push(['_trackPageview']);

	(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
</script>