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.XGTJfaultdevicename = $('#LIKEfaultdevicename').textbox("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 = "check";
	sParam.order = "order by feedbacktime desc ";
	return sParam;
}
//加载列表
var toolbar = [{
	text	:	'查询',
    iconCls	:	'icon-search',
    handler	:	doSearch
}];
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:130},
		            {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:true,width:120},
		            {field:'channelnum',title:'通道编号',hidden:true,width:120},
		            {field:'faulttypename',title:'故障类型',hidden:true,width:120},
		            {field:'faultdescription',title:'故障描述',hidden:false,width:220},
		            {field:'jobreportcompanyname',title:'上报单位',hidden:false,width:120},
		            {field:'jobreporttime',title:'上报时间',hidden:false,width:130},
		            {field:'jobid',title:'操作',hidden:false,width:120, 
		            	formatter:function showClick(value,record,num){ 
		            		return '<a href="javascript:;" style="background: #67c23a;color:white;" class="l-btn l-btn-small" onclick="handleOrder(\''+value+'\',\''+record.faultdeviceid+'\',\''+record.faultdevicename+'\',\''+record.channelnum+'\',\''+record.jobprocessstatename+'\')" group="" id=""><span class="l-btn-left"><span class="l-btn-text">验收</span></span></a>';
						}
		            }
		            ]]
	});
}
//工单处理
function handleOrder(jobid,faultdeviceid,faultdevicename,channelnum,jobprocessstatename){
	windowId = top.$.createWin({
		title	: 	'工单验收确认',
		width	:  	950,
		height	: 	880,
		data    :   {jobid:jobid},
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:confirmWorkOrder
		}],
		url	:	sysUrl+"/yw/repairmanage/checkWorkOrder/ywrepaircheck.jsp?faultdeviceid="+faultdeviceid+"&channelnum="+channelnum+"&faultdevicename="+encodeURI(faultdevicename)+"&jobStatus="+encodeURI(jobprocessstatename)
	 });
}
function confirmWorkOrder(){
	setTimeout(function(){
		doSearch();
	},500);
}
//删除
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,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		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,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/ywrepairjobinfo/ywrepairjobinfo_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	/*setTimeout(function(){
		
	},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,
		onClose:function(){
			var win		=	$.getTopWindowById(windowId);
			win.unloadWindow();
		},
		 url	:	sysUrl+"/im/xx/ywrepairjobinfo_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywrepairjobinfoAction!count_Ywrepairjobinfo.do',
		success : function(value) {
			if(!$.isNull(value)){
				countExport(value);
			}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 = "";
	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"));
}
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);*/
}
