<?php
	$uid = $this->session->userdata('usertype_id');
	$profile_id = $this->session->userdata('profile_id');
	
	if($uid==1)
		$data = array("Add","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",'21');
		$va1l ->where("del_date",NULL);
		$res_val = $va1l->get("mst_user_profile_setting");
		foreach($res_val ->result_array() as $row)
		{
			$data[] = $row['setting_name'];
			
		}
	
	}
	

?>
<?php
	 $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_maintenance_grid
{
	display:none !important; 
}
</style>
<script type="text/javascript">       
jQuery().ready(function (){ 
	jQuery(".date").datepicker({dateFormat:"<?php echo $js_date_format; ?>",changeMonth: true,changeYear: true});
	jQuery("input :button, input:submit, input:reset").button(); 
	jQuery("#maintenance_grid").jqGrid({
		url:"<?php echo site_url('maintenance/loadData'); ?>",
		datatype: "json",
		colNames:["<?php echo $this->lang->line("ID"); ?>",'<?php echo $this->lang->line("Date-Time"); ?>','<?php echo $this->lang->line("Asset_Name"); ?>','Type opération','Type de maintenance','<?php echo $this->lang->line("Start Date alert"); ?>','<?php echo $this->lang->line("End Date alert"); ?>'
		          ,'<?php echo $this->lang->line("Kms start alert"); ?>','<?php echo $this->lang->line("Kms end alert"); ?>'
		          ,'<?php echo $this->lang->line("Start Date alert (# hours)"); ?>','<?php echo $this->lang->line("End Date alert (# hours)"); ?>'
		          ,'<?php echo $this->lang->line("Cost"); ?>','<?php echo $this->lang->line("Status"); ?>'
		          ,'<?php echo $this->lang->line("Comment"); ?>'
		          ],
		colModel:[  
			{name:"id",index:"id",hidden:true, width:15, jsonmap:"id"},
			{name:"created",editable:false, index:"created",  align:"center", jsonmap:"created", formatter: 'date', formatoptions:{srcformat:"Y-m-d H:i:s",newformat:"<?php echo $date_format; ?> <?php echo $time_format; ?>"}},   
			{name:"assets_name",editable:true, index:"assets_name",  align:"center", jsonmap:"assets_name"},
			{name:"operation_type",editable:true, index:"operation_type",  align:"center", jsonmap:"operation_type"},
			{name:"operation_name",editable:true, index:"operation_name",  align:"center", jsonmap:"operation_name"},
			{name:"debut_alerte",editable:true, index:"debut_alerte", width:150, align:"center", jsonmap:"debut_alerte", formatter: 'date', formatoptions:{srcformat:"Y-m-d H:i:s",newformat:"<?php echo $date_format; ?> <?php echo $time_format; ?>"}},
			{name:"fin_alerte",editable:true, index:"fin_alerte", width:150, align:"center", jsonmap:"fin_alerte", formatter: 'date', formatoptions:{srcformat:"Y-m-d H:i:s",newformat:"<?php echo $date_format; ?> <?php echo $time_format; ?>"}},
			{name:"kms_debut_alerte",editable:true, index:"kms_debut_alerte",  align:"center", jsonmap:"kms_debut_alerte"},
			{name:"kms_fin_alerte",editable:true, index:"kms_fin_alerte",  align:"center", jsonmap:"kms_fin_alerte"},
			{name:"duree_debut_alerte",editable:true, index:"duree_debut_alerte",  align:"center", jsonmap:"duree_debut_alerte"},
			{name:"duree_fin_alerte",editable:true, index:"duree_fin_alerte",  align:"center", jsonmap:"duree_fin_alerte"},
			{name:"cout",editable:true, index:"cout", width:70, align:"center", jsonmap:"cout"},
			{name:"statut",editable:true, index:"statut",width:70, align:"center", jsonmap:"statut"},
			{name:"comments",editable:true, index:"comments",  align:"center", jsonmap:"comments"},
			], 
		rowNum:100,
		height: 'auto', 
		rownumbers: true,
		autowidth: true,
		shrinkToFit: false,
		rowList:[10,20,30,50,100],
		pager: jQuery("#maintenance_pager"),
		sortname: "id",
		loadComplete: function(){
			 $("#loading_top").css("display","none");
		},
		loadBeforeSend: function(){$("#loading_top").css("display","block");},
		viewrecords: true,
		multiselect: false, 
		sortorder: "desc",
		footerrow : true, 
		userDataOnFooter : true,
		caption:"<?php echo $this->lang->line("Maintenance List"); ?>",
		jsonReader: { repeatitems : false, id: "0" }
	});
	
	jQuery("#maintenance_grid").jqGrid("navGrid", "#maintenance_pager", {add:false, edit:false, del:false, search:false}, {}, {}, {}, {multipleSearch:false});
	<?php
	if(in_array('Export',$data)){
	?>
	jQuery("#maintenance_grid").jqGrid("navButtonAdd","#maintenance_pager",{caption:"<?php echo $this->lang->line("Export"); ?>",
		onClickButton:function(){
			var alert_sdate = $('#alert_sdate').val();
			var alert_edate = $('#alert_edate').val();
			var device = $('#device').val();
			qrystr ="/cmd/export?alert_sdate="+alert_sdate+"&alert_edate="+alert_edate+"&device="+device;
			document.location = "<?php echo site_url('commercial/loadData'); ?>"+qrystr;
		}
	});
	<?php } ?>

	<?php
			if(in_array('Add',$data)){
			?>
			jQuery("#maintenance_grid").jqGrid("navButtonAdd","#maintenance_pager",{caption:"<?php echo $this->lang->line('add'); ?>",
				onClickButton:function(){
					//$("#loading_dialog").dialog("open");
					$("#loading_top").css("display","block");
					$('#maintenance_list_div').hide();
					$('#maintenance_form_div').show();
					$('#maintenance_form_div').load('<?php echo site_url('/maintenance/add_mainteance/'); ?>');
				}
			});
			<?php } ?>
	
	 $("#loading_top").css("display","none");
});

function search_maintenance(){
	
	var alert_sdate = $('#alert_sdate').val();
	var alert_edate = $('#alert_edate').val();
	var device = $('#device').val();
	
	//$("#allpoints_list").flexOptions({params: [{name:'alert_sdate', value: alert_sdate},{name:'alert_edate',value:alert_edate},{name:'device',value:device}]}).flexReload();
	 $("#loading_top").css("display","block");
	jQuery("#maintenance_grid").jqGrid('setGridParam',{postData:{alert_sdate:alert_sdate, alert_edate:alert_edate, device:device, page:1}}).trigger("reloadGrid");
	return false;
}


</script>
<?php
	$timestamp = strtotime("-2 day");
	$tomorrow=date($date_format,$timestamp);
?>
<div id="maintenance_form_div" style="padding:10px;display:none;height:450px;">
</div>
<div id="maintenance_list_div">

<form onsubmit="return search_maintenance()">
<table width="100%">
	<tr>
		<td width="2%"><?php echo $this->lang->line("Start"); ?> : <input type="text" name="alert_sdate" id="alert_sdate" class="date text ui-widget-content ui-corner-all" style="width:110px;padding:5px;" value="<?php echo $tomorrow; ?>" readonly="readonly"/></td>
		<td width="2%"><?php echo $this->lang->line("End"); ?> : <input type="text" name="alert_edate" id="alert_edate" class="date text ui-widget-content ui-corner-all" style="width:110px;padding:5px;" value="<?php echo date($date_format); ?>" readonly="readonly"/></td>
		<td width="10%"><input type="submit" value="<?php echo $this->lang->line("view"); ?>"/></td>
</form>
<table id="maintenance_grid" class="jqgrid"></table>
</div>
<div id="maintenance_pager"></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>