var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var winId	= $.getUrlParam("winId");
var obj = top.$("#"+winId).panel("options").data;
$(function(){
	initDefault();	
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	if(obj.deviceType=="videoOnline"){
		if(obj.type=="1"){
			sParam.isonline = 1;
			sParam.currentstate = 1;
		}else{
			sParam.isonline = 0;
			sParam.currentstate = 3;
		}
	}else if(obj.deviceType=="videoIntact"){
		if(obj.type=="1"){
			sParam.vqdstate = 1;
			sParam.currentstate = 1;
		}else{
			sParam.vqdstate = 0;
			sParam.currentstate = 5;
		}
	}else if(obj.deviceType=="recordIntact"){
		if(obj.type=="1"){
			sParam.recordstate = 1;
			sParam.currentstate = 1;
		}else{
			sParam.recordstate = 0;
			sParam.currentstate = 6;
		}
	}
	sParam.LIKEchannelname = $("#LIKEchannelname").textbox("getValue");
	sParam.order = "order by currentstatetime desc ";
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    }/*,{
        text	:	'增加',
        iconCls	:	'icon-add',
        handler	:	doAdd
    },{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	doEdit
    },{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	doDelete
    }*//*,{
        text	:	'模板',
        iconCls	:	'icon-export-excel',
        handler	:	doTemplate
    },{
        text	:	'导入',
        iconCls	:	'icon-update',
        handler	:	doImport
    },{
        text	:	'导出',
        iconCls	:	'icon-download',
        handler	:	doExport
    }*/];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:true,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywvideocurrentstateAction!listYwvideocurrentstateByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'currentstateid',title:'currentstateid',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
 			{field:'channelname',title:'通道名称',hidden:false,width:120},
			{field:'videonum',title:'设备编号',hidden:false,width:120},
			{field:'channelnum',title:'通道编号',hidden:false,width:120},
			{field:'videocatalog',title:'监控行政目录',hidden:true,width:120},
			{field:'currentstatename',title:'当前状态',hidden:false,width:120},
			{field:'currentstatetime',title:'检测时间',hidden:true,width:120},
			{field:'isonline',title:'是否在线',hidden:true,width:120},
			{field:'playfail',title:'播放失败',hidden:true,width:120},
			{field:'networkerror',title:'网络异常',hidden:true,width:120},
			{field:'vqcstate',title:'抽帧状态',hidden:true,width:120},
			{field:'vqcfailuretype',title:'抽帧故障类型',hidden:true,width:120},
			{field:'vqcfailurecause',title:'抽帧故障原因',hidden:true,width:120},
			{field:'vqctaskid',title:'抽帧任务id',hidden:true,width:120},
			{field:'videostatus',title:'监控状态',hidden:true,width:120},
			{field:'videofailurecause',title:'故障原因',hidden:true,width:120},
			{field:'vqdstate',title:'质量诊断状态',hidden:true,width:120},
			{field:'nosignalstatus',title:'无信号检测状态',hidden:true,width:120},
			{field:'imgclearstatus',title:'图像清晰检测状态',hidden:true,width:120},
			{field:'imgcontraststatus',title:'图像对比检测状态',hidden:true,width:120},
			{field:'imgcolorstatus',title:'颜色检测状态',hidden:true,width:120},
			{field:'imglightstatus',title:'亮度检测状态',hidden:true,width:120},
			{field:'videofreezestatus',title:'视频冻结检测状态',hidden:true,width:120},
			{field:'videojitterstatus',title:'镜头抖动检测状态',hidden:true,width:120},
			{field:'videoupheavalstatus',title:'视频剧变检测状态',hidden:true,width:120},
			{field:'videocodeerrorstatus',title:'编码异常检测状态',hidden:true,width:120},
			{field:'videonoisestatus',title:'噪声雪花检测状态',hidden:true,width:120},
			{field:'videorollstripestatus',title:'滚动条纹检测状态',hidden:true,width:120},
			{field:'vqdfailuretype',title:'质量诊断故障类型',hidden:true,width:120},
			{field:'vqdfailurecause',title:'质量诊断故障原因',hidden:true,width:120},
			{field:'vqdtaskid',title:'质量诊断任务id',hidden:true,width:120},
			{field:'recordstate',title:'录像状态',hidden:true,width:120},
			{field:'recordfailuretype',title:'录像故障类型',hidden:true,width:120},
			{field:'recordfailurecause',title:'录像故障原因',hidden:true,width:120},
			{field:'recordtaskid',title:'录像任务id',hidden:true,width:120},
			{field:'videolabelstatus',title:'标记状态',hidden:true,width:120},
			{field:'videolabeltime',title:'标记时间',hidden:true,width:120},
			{field:'videolabelaccount',title:'标记人员',hidden:true,width:120},
			{field:'createaccount',title:'创建人员',hidden:true,width:120},
			{field:'createtime',title:'创建日期',hidden:true,width:120},
			{field:'modifyaccount',title:'修改人员',hidden:true,width:120},
			{field:'modifytime',title:'修改日期',hidden:true,width:120},
			{field:'remarks',title:'备注',hidden:true,width:120},
			{field:'extend1',title:'扩展1',hidden:true,width:120},
			{field:'extend2',title:'扩展2',hidden:true,width:120},
			{field:'extend3',title:'扩展3',hidden:true,width:120},
			{field:'extend4',title:'扩展4',hidden:true,width:120}
		]]
	});
}
//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideocurrentstateAction!delete_Ywvideocurrentstate.do',
			data:{
				currentstateid	:	selRows.currentstateid

			},
			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].currentstateid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywvideocurrentstateAction!batch_delete_Ywvideocurrentstate.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(!$.isNull(flag)){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//查询
function doSearch(){	
	$('#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/ywvideocurrentstate/ywvideocurrentstate_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/ywvideocurrentstate/ywvideocurrentstate_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	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.XGTJcurrentstateid = jq('#currentstateid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideocurrentstateAction!update_Ywvideocurrentstate.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:'ywvideocurrentstateAction!insert_Ywvideocurrentstate.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/ywvideocurrentstate_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywvideocurrentstateAction!count_Ywvideocurrentstate.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 = 'ywvideocurrentstateAction!exp_Ywvideocurrentstate.do?'+downParam;
	document.excelForm.submit(); 
}
