var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
$(function(){
	initTime();
	initTableGrid();
});
//查询条件
function getQueryParams(){
	var sParam = {};
	
	var tableparam = "";
//	sParam.faultdevicetype = $('#faultdevicetype').combobox("getValue");
	sParam.LIKEfaultdevicename = $('#LIKEfaultdevicename').textbox("getValue");
//	sParam.faultgrade = $('#faultgrade').combobox("getValue");
	
	
	
	var LIKEtime1 = $('#jobreporttimeStart').datetimebox("getValue");	
	var LIKEtime2 = $('#jobreporttimeEnd').datetimebox("getValue");	
	if(!(LIKEtime1==null||LIKEtime1==""||LIKEtime1==undefined)){
		tableparam += " AND jobreporttime >= '" +LIKEtime1 + "'";
	}
	if(!(LIKEtime2==null||LIKEtime2==""||LIKEtime2==undefined)){
		tableparam += " AND jobreporttime <= '" +LIKEtime2 + "'";
	}
	sParam.tableparam = tableparam;
	sParam.flag = "all";
	sParam.order = "order by jobreporttime desc ";
	return sParam;
}
//加载列表
var toolbar = [{
		text	:	'查询',
	    iconCls	:	'icon-search',
	    handler	:	doSearch
	},{
		text : 'Excel导出',
		iconCls : 'icon-export-excel',
		handler : doExport
	}];
function initTableGrid(){	
	$("#tableGrid").datagrid(  {
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywrepairjobinfoAction!listYwrepairjobinfoByPage.do',
		columns : [[
		            {
		            	field: 'A',
		            	checkbox:'true',
		            	width:30
		            },
		            {field:'jobnum',title:'工单编号',hidden:false,width:120},
		            {field:'jobprocessstatename',title:'工单状态',hidden:false,width:120},
		            {field:'faultgradename',title:'告警等级',hidden:true,width:120},
		            {field:'faultdevicetypename',title:'故障设备类型',hidden:false,width:120},
		            {field:'faultdevicename',title:'故障设备名称',hidden:false,width:120},
		            {field:'faultdeviceid',title:'故障设备编号',hidden:false,width:120},
		            {field:'channelnum',title:'通道编号',hidden:false,width:120},
		            {field:'faulttypename',title:'故障类型',hidden:false,width:120},
		            {field:'faultdescription',title:'故障描述',hidden:false,width:120},
		            {field:'jobreporttime',title:'上报时间',hidden:false,width:120},
		            
		            
		            {field:'jobid',title:'操做',hidden:true,width:120, 
		            	formatter:function showClick(value,record,num){ 
		            		if(record.jobprocessstate==904){
		            			return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px,5px;" class="l-btn l-btn-small" onclick="handleOrder(\''+value+'\',\'handel\')" group="" id=""><span class="l-btn-left"><span class="l-btn-text">取消挂起</span></span></a>';
//		            			return '<button href="javascript:void(0);" class="ahref" onclick="handleOrder(\''+value+'\',\'cancel\')">取消挂起</button>';
		            		}else{
		            			return '<a href="javascript:;" style="background: #67c23a;color:white;padding: 2px,5px;" class="l-btn l-btn-small" onclick="handleOrder(\''+value+'\',\'handel\')" group="" id=""><span class="l-btn-left"><span class="l-btn-text">受理</span></span></a>';
//		            			return '<button href="javascript:void(0);" class="ahref" onclick="handleOrder(\''+value+'\',\'handel\')">工单处理</button>';
		            		}
						}
		            }
		            ]]
	});
	
}
//工单处理
function handleOrder(jobid,type){
	windowId = top.$.createWin({
		title	: 	'工单受理',
		width	:  	870,
		height	: 	700,
		data    :   {jobid:jobid,type:type},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/repairmanage/acceptWorkOrder/ywrepairaccept.jsp"
	 });
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywrepairjobinfoAction!delete_Ywrepairjobinfo.do',
			data:{
				jobid	:	selRows.jobid

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].jobid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywrepairjobinfoAction!batch_delete_Ywrepairjobinfo.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(!$.isNull(flag)){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//查询
function refresh(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	650,
		height	: 	300,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/ywrepairjobinfo/ywrepairjobinfo_show.jsp"
	 });
}
//修改
function doUpdate(index){
	toEdit=true;
	var selRows = $('#tableGrid').datagrid('getRows')[index];
	windowId = top.$.createWin({
		title	: 	'修改信息',
		width	:  	650,
		height	: 	300,
		data   	:   selRows,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/ywrepairjobinfo/ywrepairjobinfo_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	
//	var t1=window.setInterval(function(){
//		var f = top.$("#"+windowId).panel("options").data.flag;
//    	if(top.$("#"+windowId).panel("options").data.flag){
//    		window.clearInterval(t1);
//    		$.showMsg("操作成功！");
//    		top.$("#"+windowId).dialog('close');
//    		doSearch();
//    	}
//    }, 500);
	
	setTimeout(function(){
		doSearch();
	},1000);
	
	/*var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	console.log(model);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	if(toEdit){//修改
		model.XGTJjobid = jq('#jobid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywrepairjobinfoAction!update_Ywrepairjobinfo.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywrepairjobinfoAction!insert_Ywrepairjobinfo.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}*/
}
//打开修改
function doEdit(){
	var selIndex = $('#tableGrid').datagrid('getRowIndex', $("#tableGrid").datagrid('getSelected'))
	if(selIndex==-1){
		$.showMsg('请先选择一行! ');
		 return;
	}else{
		doUpdate(selIndex);
	}		
}
//模板下载
function doTemplate(){
	var baseUrl = $.proPath();
	var downFile = baseUrl+"/down/down.xls";
	window.location.href= downFile;
}
//导入
function doImport(){
	toEdit=false;
	windowId = top.$.createWin({
		 id		:	$.uniqueId(),
		 title	: 	'导入',
		 width	:  	400,
		 height	: 	130,
		 url	:	sysUrl+"/im/xx/ywrepairjobinfo_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	model.page=1;
	model.rows=10;
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywrepairjobinfoAction!listYwrepairjobinfoByPage.do',
		success : function(value) {
			var total = value.total;
			if(total>0){				
				countExport(value);
			}else if(total==0){
				$.showMsg("暂无数据！");
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
// 导出传参条件
function countExport(totalSize) {
	var limit = 5000;
	if (totalSize > 5000) {
		top.$.dlg.confirm('记录过多支持前5000条,确定要将记录导出Excel为表格吗?',function(){ 
			execExport();
		});		
	} else {
		top.$.dlg.confirm('确认导出?',function(){ 
			execExport();
		})
	}
}
// 调用后台
function execExport() {
	var model = getQueryParams();
	var downParam = "";
	
	var LIKEfaultdevicename = model.LIKEfaultdevicename;
	var tableparam = model.tableparam;
	var flag = model.tableparam;
	var order = model.order;
	
	downParam = "LIKEfaultdevicename="+LIKEfaultdevicename+"&tableparam="+tableparam+"&flag="+flag+"&order="+order;

	document.excelForm.action = 'ywrepairjobinfoAction!exp_Ywrepairjobinfo.do?'+downParam;
	document.excelForm.submit(); 
}
function initTime(){
	$("#jobreporttimeStart").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#jobreporttimeEnd").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	/*$("#jobreporttimeStart_feedback").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#jobreporttimeEnd_feedback").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	$("#jobreporttimeStart_confirm").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#jobreporttimeEnd_confirm").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	$("#jobreporttimeStart_delay").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#jobreporttimeEnd_delay").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	$("#jobreporttimeStart_reject").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#jobreporttimeEnd_reject").datetimebox("setValue",$.getTodayEnd("23:59:59"));
	$("#jobreporttimeStart_end").datetimebox("setValue",$.getTodayStart(0,"00:00:00"));
	$("#jobreporttimeEnd_end").datetimebox("setValue",$.getTodayEnd("23:59:59"));*/
}
function initSelect(){
	var ywDeviceTypeArr = [
	                       {"id":"","text":"全部","selected":true},
	                       {"id":"1","text":"监控"},
	                       {"id":"2","text":"服务器"},
	                       {"id":"3","text":"应用系统"},
	                       {"id":"4","text":"存储系统"}
	                       
	];
	var ywAlarmGradeTypeArr = [
	                       {"id":"","text":"全部","selected":true},
	                       {"id":"1","text":"重要"},
	                       {"id":"2","text":"次要"},
	                       {"id":"3","text":"一般"},
	                       {"id":"4","text":"提示"}
	                       
	                       ];
	/*$.ajax({
		url	: "xtclAction!query_Xtcl_task.do",
		type : "POST",
		dataType : "JSON",
		data : {csdl:"ywDeviceStatus"},
		async : false,
		success : function(res){
			if(res){
				for(i in res){
					cacheStatusObj[res[i].csdm+""] = res[i].csmc;
					ywDeviceStatusArr.push({"id":res[i].csdm,"text":res[i].csmc})
				}
			}
		}
	});*/
	$("#faultdevicetype").combobox( "loadData", ywDeviceTypeArr);
	$("#faultgrade").combobox( "loadData", ywAlarmGradeTypeArr);
/*	$("#faultdevicetype_accept").combobox( "loadData", ywDeviceTypeArr);
	$("#faultgrade_accept").combobox( "loadData", ywAlarmGradeTypeArr);
	$("#faultdevicetype_feedback").combobox( "loadData", ywDeviceTypeArr);
	$("#faultgrade_feedback").combobox( "loadData", ywAlarmGradeTypeArr);
	$("#faultdevicetype_confirm").combobox( "loadData", ywDeviceTypeArr);
	$("#faultgrade_confirm").combobox( "loadData", ywAlarmGradeTypeArr);
	$("#faultdevicetype_delay").combobox( "loadData", ywDeviceTypeArr);
	$("#faultgrade_delay").combobox( "loadData", ywAlarmGradeTypeArr);
	$("#faultdevicetype_reject").combobox( "loadData", ywDeviceTypeArr);
	$("#faultgrade_reject").combobox( "loadData", ywAlarmGradeTypeArr);
	$("#faultdevicetype_end").combobox( "loadData", ywDeviceTypeArr);
	$("#faultgrade_end").combobox( "loadData", ywAlarmGradeTypeArr);*/
}
