var windowId,proName = window.location.pathname.split("/")[1],sysUrl = "http://"+window.location.host+"/"+proName,toEdit;
var channelsetid = $.getUrlParam("channelsetid");
$(function(){
	initDefault();	
	initXzNameTree();
});
//默认
function initDefault(){
	initGrid();
}
//查询条件
function getQueryParams(){
	var sParam = {};
	sParam.channelsetid = channelsetid;
	sParam.LIKEchannelname = $('#LIKEchannelname').textbox("getValue");
	var xzbh = $('#xzmc').textbox("getValue");
	if(xzbh!="-1" || xzbh!=""){
		sParam.xzbh = $('#xzmc').textbox("getValue");
	}
	sParam.order = "order by createTime desc ";
	return sParam;
}
//加载列表
function initGrid(){	
	var toolbar = [/*{
    	text    :	'刷新',
        iconCls : 	'icon-refresh',
        handler : 	function(){location.reload(true);}
    },*/{
    	text	:	'查询',
        iconCls	:	'icon-search',
        handler	:	doSearch
    },{
        text	:	'增加',
        iconCls	:	'icon-add',
        handler	:	doAdd
    }/*,{
        text	:	'修改',
        iconCls	:	'icon-cut',
        handler	:	doEdit
    }*/,{
        text	:	'删除',
        iconCls	:	'icon-delete',
        handler	:	doDelete_batch
    },{
        text	:	'视频模式',
        iconCls	:	'icon-timeMode',
        handler	:	setVideoModel
    },{
        text	:	'录像模式',
        iconCls	:	'icon-actMode',
        handler	:	setRecodeModel
    },{
        text	:	'抽取录像时间',
        iconCls	:	'icon-extract',
        handler	:	setTimeRecode
    }/*,{
        text	:	'开启',
        iconCls	:	'icon-start',
        handler	:	dostart_batch
    },{
        text	:	'关闭',
        iconCls	:	'icon-end',
        handler	:	doend_batch
    }*/];
	$('#tableGrid').datagrid({
		toolbar : toolbar,
		singleSelect:false,
		rownumbers:true,
		queryParams:getQueryParams(),
		url : 'ywvideochannelsetlistAction!listYwvideochannelsetlistByPage.do',
		columns : [[
		    {
				field: 'A',
				checkbox:'true',
				width:30
			},
		    {field:'channellistid',title:'channellistid',hidden:true,width:120, 
				formatter:function showClick(value,record,num){ 
					return "<a  href='javascript:void(0);' class='ahref' onclick='doUpdate("+num+")'>"+value+"</a>";
				}
 			},
			{field:'channelsetid',title:'通道集合id',hidden:true,width:120},
			{field:'channelname',title:'通道名称',hidden:false,width:120},
			{field:'videonum',title:'设备编号',hidden:false,width:120},
			{field:'channelnum',title:'通道编号',hidden:false,width:120},
			{field:'videomodelname',title:'抽取视频模式',hidden:false,width:120},
			{field:'recordxdays',title:'抽取录像时间',hidden:false,width:120},
			{field:'recordmodelname',title:'抽取录像模式',hidden:false,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 initXzNameTree(){
	$.ajax({
		 type:'post',
		 async:false,
		 dataType : 'json',
		 //url:'ptxzsAction!query_Ptxzs.do'
		 url:'menuConfigAction!query_Ptxzs.do'
	 }).done(function(data){
		 $.each(data,function(i,o){
				o.state 	='closed';
				o.treeId 	= o.xzbh;
				o.name 		= o.xzmc;
				o.treeType 	= "xz";
				o.id		= o.xzbh;
				o.pid		= o.sjxzbh;
				o.start 	= 0; //iconCls
			}) 
			var xzfz = {};
		 xzfz.treeType 	= "xz";
				xzfz.id		= "-1";
				xzfz.pid		= "-1";
				xzfz.start 	= 0; //iconCls
		 	xzfz.xzbh = "-1";
		 	xzfz.xzmc = "全部分组";
		 	xzfz.state = "open";
		 	xzfz.treeId = "-1";
		 	xzfz.sjxzbh = "-1";
		 	xzfz.name = "全部分组";
			data.push(xzfz);
				 $('#xzmc').combotree({
				        checkbox:false,
				    	border:false,
				    	parentField:"pid",  
				    	textFiled:"name",  
				    	idFiled:"id",
				    	panelWidth:148,
				    	data:data,
						onLoadSuccess:function(node,data){
							if(data){
								//$('#LIKExzName').combotree("setValue",data[0].xzId).combotree("setText", data[0].xzName);
//								$('#LIKExzId').val(data[0].xzId);
//								$('#LIKExzName').val(data[0].xzName);
//								initDefault();
							}
						},
						onClick: function(node){
//							$('#xzbh').val(node.xzbh);
//							$('#xzmc').val(node.xzmc);
						}
				});
	 });
}

//删除
function doDelete(){
	var selRows = $('#tableGrid').datagrid('getSelected'); 
	if(!selRows){
		$.showMsg('请先选择记录! ');
		return;
	}
	top.$.dlg.confirm('确认删除?',function(){
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideochannelsetlistAction!delete_Ywvideochannelsetlist.do',
			data:{
				channellistid	:	selRows.channellistid

			},
			success:function(flag){
				if(flag == "success"){
					$.showMsg("操作成功！");
					doSearch();
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	});
}
//批量删除
function doDelete_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	 if(selRows===null || !selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择一行! ');
		 return;
	 }else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].channellistid);
		}
		top.$.dlg.confirm('你确认要删除吗?',function(){ 
		    $.ajax({
				 type:'post',
				 dataType : 'json',
				 url:'ywvideochannelsetlistAction!batch_delete_Ywvideochannelsetlist.do',
				 data:{keyword	:	keyword.join(",")},
				 success:function(flag){
				 	if(flag=="success"){
						$.showMsg("操作成功！");
					 	doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			 });
		}); 
	}
}
//批量开启
function dostart_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	if(selRows===null || !selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].channellistid);
		}
		top.$.dlg.confirm('你确认要开启吗?',function(){ 
			$.ajax({
				type:'post',
				dataType : 'json',
				url:'ywvideochannelsetlistAction!batch_start_Ywvideochannelsetlist.do',
				data:{keyword	:	keyword.join(",")},
				success:function(flag){
					if(flag=="success"){
						$.showMsg("操作成功！");
						doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			});
		}); 
	}
}
//批量关闭
function doend_batch(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	var keyword = [];
	if(selRows===null || !selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择一行! ');
		return;
	}else{
		for(var i=0; i<selRows.length; i++){
			keyword.push(selRows[i].channellistid);
		}
		top.$.dlg.confirm('你确认要关闭吗?',function(){ 
			$.ajax({
				type:'post',
				dataType : 'json',
				url:'ywvideochannelsetlistAction!batch_end_Ywvideochannelsetlist.do',
				data:{keyword	:	keyword.join(",")},
				success:function(flag){
					if(flag=="success"){
						$.showMsg("操作成功！");
						doSearch();
					}else{
						$.showMsg("操作失败！");
					}
				}
			});
		}); 
	}
}
//查询
function doSearch(){	
	$('#tableGrid').datagrid('load',getQueryParams());
}
//增加
function doAdd(){
	toEdit=false;
	windowId = top.$.createWin({
		title	: 	'增加信息',
		width	:  	1050,
		height	: 	600,
		data    : channelsetid,
		buttons:  [{
			text:$.fn.textbox.defaults.ok,
			handler:saveNodeInfo
		}],
		url	:	sysUrl+"/yw/videomanage/videochannelset/ywvideochannelsetlist/ywvideochannelsetlist_show.jsp"
	 });
}
//设置视频模式
function setVideoModel(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	if(!selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择记录! ');
		return;
	}else{
		 windowId = top.$.createWin({
			 title	: 	'设置视频模式',
			 width	:  	400,
			 height	: 	150,
			 buttons:  [{
				 text:$.fn.textbox.defaults.ok,
				 handler:updateVideoModel
			 }],
			 url	:	sysUrl+"/yw/videomanage/videochannelset/ywvideochannelsetlist/ywsetvideomodel.jsp"
		 });
	 }
}
function updateVideoModel(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
//	var model   =   $.getFormValues("childform",windowId);
	var videomodel = '0';
	var keyword =[];
	var drawLiveMode = win.$('input[name="drawLiveMode"]:checked');
	if(drawLiveMode.length > 0){
		videomodel = drawLiveMode.val();
	}else{
      return;
	}
	var rows = $('#tableGrid').datagrid('getChecked'); 
	if(!rows || rows.length<1 || rows==[]){
		$.showMsg('请先选择记录! ');
		return;
	}
	for(var i=0; i<rows.length; i++){
		keyword.push(rows[i].channellistid);
	}
	
	$.ajax({
		 type:'post',
		 dataType : 'json',
		 url:'ywvideochannelsetlistAction!update_batch_videomodel.do',
		 data:{channelsetid:channelsetid,videomodel:videomodel,keyword	:	keyword.join(",")},
		 success:function(flag){
		 	if(flag=="success"){
				$.showMsg("操作成功！");
				top.$("#"+windowId).dialog('close');
			 	doSearch();
			}else{
				$.showMsg("操作失败！");
			}
		}
	 });
}
//设置录像模式
function setRecodeModel(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	if(!selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择记录! ');
		return;
	}else{
		windowId = top.$.createWin({
			title	: 	'设置视频模式',
			width	:  	400,
			height	: 	150,
			buttons:  [{
				text:$.fn.textbox.defaults.ok,
				handler:updateRecordModel
			}],
			url	:	sysUrl+"/yw/videomanage/videochannelset/ywvideochannelsetlist/ywsetrecordmodel.jsp"
		});
	}
}
function updateRecordModel(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
//	var model   =   $.getFormValues("childform",windowId);
	var keyword =[];
	var recordmodel = "0";
	if(!win.$('#childform').form('enableValidation').form('validate')){
		return;
	}
	var drawRecodeMode = win.$('input[name="drawRecodeMode"]:checked');
	if(drawRecodeMode.length > 0){
		recordmodel = drawRecodeMode.val();
	}else{
		return;
	}
	var rows = $('#tableGrid').datagrid('getChecked'); 
	if(!rows || rows.length<1 || rows==[]){
		$.showMsg('请先选择记录! ');
		return;
	}
	for(var i=0; i<rows.length; i++){
		keyword.push(rows[i].channellistid);
	}
	
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'ywvideochannelsetlistAction!update_batch_recordmodel.do',
		data:{channelsetid:channelsetid,recordmodel:recordmodel,keyword	:	keyword.join(",")},
		success:function(flag){
			if(flag=="success"){
				$.showMsg("操作成功！");
				top.$("#"+windowId).dialog('close');
				doSearch();
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
//设置视频抽取时间
function setTimeRecode(){
	var selRows = $('#tableGrid').datagrid('getChecked'); 
	if(!selRows || selRows.length<1 || selRows==[]){
		$.showMsg('请先选择记录! ');
		return;
	}else{
		windowId = top.$.createWin({
			title	: 	'设置视频抽取时间',
			width	:  	400,
			height	: 	150,
			buttons:  [{
				text:$.fn.textbox.defaults.ok,
				handler:updateRecordDays
			}],
			url	:	sysUrl+"/yw/videomanage/videochannelset/ywvideochannelsetlist/ywsetrecordday.jsp"
		});
	}
}
function updateRecordDays(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
//	var model   =   $.getFormValues("childform",windowId);
	var keyword =[];
	var recodeBeforDays = win.$('#drawRecodeBeforDays').textbox("getValue");
	if(!recodeBeforDays){
		return;
	}
	var rows = $('#tableGrid').datagrid('getChecked'); 
	if(!rows || rows.length<1 || rows==[]){
		$.showMsg('请先选择记录! ');
		return;
	}
	for(var i=0; i<rows.length; i++){
		keyword.push(rows[i].channellistid);
	}
	
	$.ajax({
		type:'post',
		dataType : 'json',
		url:'ywvideochannelsetlistAction!update_batch_recorddays.do',
		data:{channelsetid:channelsetid,recordxdays:recodeBeforDays,keyword	:	keyword.join(",")},
		success:function(flag){
			if(flag=="success"){
				$.showMsg("操作成功！");
				top.$("#"+windowId).dialog('close');
				doSearch();
			}else{
				$.showMsg("操作失败！");
			}
		}
	});
}
//修改
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/videomanage/videochannelset/ywvideochannelsetlist/ywvideochannelsetlist_show.jsp"
	});
}
//保存
function saveNodeInfo(){
	var win		=	$.getTopWindowById(windowId);
	var jq		=	win.$;
	var model   =   $.getFormValues("childform",windowId);
	if(!win.$('#childform').form('enableValidation').form('validate')){
      return;
	}
	if(toEdit){//修改
		model.XGTJchannellistid = jq('#channellistid').val();

		//model.checkparam = " and xh='"+model.xh+"' ";
		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideochannelsetlistAction!update_Ywvideochannelsetlist.do',
			data:model,
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
	}else{
		/*console.log(win);
		console.log("进入=========");
		var dat = win.getSelected();
		console.log("dat:",dat);*/
		
		var frame   =   top.$("#"+windowId).children("iframe")[0];
//		console.log('frame:',frame);
		var winf     =   frame.contentWindow||frame.window;
//		console.log('winf:',winf);
		var tar = winf.frames[0];
//		console.log('tar:',tar);
		var rows = tar.getSelected();
		console.log('rows:',rows);

		$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideochannelsetlistAction!batch_add_Ywvideochannelsetlist.do',
			data:{channelsetid:channelsetid,param:JSON.stringify(rows)},
			success:function(value){
				if(value=="success"){
					$.showMsg("操作成功！");
					top.$("#"+windowId).dialog('close');
					doSearch();
				}else if(value=="same"){
					$.showMsg("已存在！");
				}else{
					$.showMsg("操作失败！");
				}
			}
		});
		
		/*setTimeout(function(){doSearch();},1000);
		top.$("#"+windowId).dialog('close');*/
		/*$.ajax({
			type:'post',
			dataType : 'json',
			url:'ywvideochannelsetlistAction!insert_batch_Ywvideochannelsetlist.do',
			data:insertParam,
			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/ywvideochannelsetlist_imp.jsp"
	 });
}
//导出
function doExport(){
	var model = getQueryParams();
	$.ajax({
		type : 'post',
		dataType : 'json',
		data : model,
		url : 'ywvideochannelsetlistAction!count_Ywvideochannelsetlist.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 = 'ywvideochannelsetlistAction!exp_Ywvideochannelsetlist.do?'+downParam;
	document.excelForm.submit(); 
}

