//发送过滤js
var yxyFilter_currpage=1;//当前页
var yxyFilter_tolpage=1;//总页数
var yxyFilter_filetypeid=0;//文件类别id
var yxyFilter_pageNum=10;//每页显示的条数
var yxyFilter_leiList="";//类别变量
var yxyFilter_AddressID;//地址ID
var yxyFilter_ready=0;//是不是初始化是查询所有否则根据类别查
var yxyFilter_isesites=0;//是否存在
var yxyFilter_initaddress="";//初始地址
var before_submit_address = "";//提交编辑过滤地址前的数据
$(document).ready(function(){ 
	//1.查询发送过滤类别
		findFilter();
	//2.查询发送过滤地址
		findFilterAddress(1);
	//3.根据分组查询
	$("#yxyFilter_info").change(function(){
		yxyFilter_filetypeid=$("#yxyFilter_info").val();
		yxyFilter_ready=1;
		findFilterAddress(1);
	});

});

//查询发送过滤类别
function findFilter(){
	$.ajax({
		type:"post",
		cache: false,
		async:false,
		type: "post", 
		url: "filter_findSendFilterAll.action", 
		dataType:"json",
		error: function() {alert("异常");}, 
		success: function(msg) {
			var w= new Array();
			yxyFilter_leiList=msg;
			var html='';
			html+='<option value=0>默认分组</option>';
			var htmls='<option id="filterID0" value=0>默认分组</option>';
			//迭代类别
			if(msg!=null&&msg!=""){
				$.each(msg,function(s,row){
					htmls+='<option id=filterID'+row.filterid+' value='+row.filterid+'>'+row.filtername+'</option>';		
					html+='<option value='+row.filterid+'>'+row.filtername+'</option>';	
					//类别管理
					var q= new Array();
					q[0]=(s+1)+"";
					q[1]='<span id=nameLast'+row.filterid+'>'+row.filtername+'<span>';
					var fiterDate=row.filterdate.split(" ");
					q[2]='<span title='+fiterDate[1]+'>'+fiterDate[0]+'</span>';
					q[3]='<a href="javascript:void(0);" onclick="edit_fiterType('+row.filterid+',\''+row.filtername+'\')">编辑</a>|<a href="javascript:void(0);" onclick="del_fiterType('+row.filterid+')">删除</a>|<a href="javascript:void(0);" onclick="edit_fiterType('+row.filterid+')">清空</a>';
				   	w[s]=q;
				});
				$("#yxyFilterMangeDiv").lgytable({
					tbinfo:'分组管理',//"草稿邮件",
					tbpageshow:false,
					tbtitle:['编号','分组名', '建立时间','操作'], 
					tbwidth:['30','100','100', '100'],
					tbcon:[{"tbcon":w,"dqpage":0,"tolpage":0,"tolcount":0}]
				});
			}			
			$("#yxyFilter_info").html(html);
			$("#yxyFilter_List").html(html);
			$("#yxyFilter_editinfo").html(htmls);
		}
	});	
}
//查询发送过滤地址
function findFilterAddress(currpage){
	$.ajax({
		type:"post",
		cache: false,
		async:false,
		type: "post", 
		url: "filter_findFilterAddressByFilterID.action", 
		dataType:"json",
		data:{
			currentPage:currpage,
			pageSize:yxyFilter_pageNum,
			filterID:yxyFilter_filetypeid,
			isready:yxyFilter_ready
		},
		error: function() {alert("异常");}, 
		success: function(msg) {
			$("#countdiv").show();
			columnsShow(msg);
		}
	});	
}

//模糊查询地址列表
function findFilterAddressLike(){
	$.ajax({
		type:"post",
		cache:false,
		async:false,
		type: "post", 
		url: "filter_findFilterAddressLike.action", 
		dataType:"json",
		data:{
			findValue:$("#findFilterValue").val()
		},
		error: function() {alert("异常");}, 
		success: function(msg) {
			$("#countdiv").hide();
			columnsShow(msg);
		}
	});
}
//列表迭代
function columnsShow(msg){
	var w= new Array();
	$.each(msg.yxysendfilteraddresslist,function(index,row){
		//查询所属分组
		var feizu="默认分组";				
		$.each(yxyFilter_leiList,function(s,c){
			if(c.filterid==row.filterid){
				feizu=c.filtername;
				return false;
			}				
		});
		
		var q= new Array();
		q[0]='<input type="checkbox" class="Filter_checkbox" name="Filter_checkbox" id="Filter_checkbox" value='+row.filteraddrid+'>|center';
		q[1]=row.filteraddress;
		var timerstr=(row.filteraddrdate).split(" ");
		q[2]='<span title='+timerstr[1]+'>'+timerstr[0]+'</span>';
		q[3]=feizu;
		q[4]=row.filteraddrreason;
		q[5]='<a href="javascript:void(0);" onClick="editFilterAddress(\''+row.filteraddrid+'\',\''+row.filteraddrreason+'\',\''+row.filteraddress+'\',\''+row.filterid+'\')";>编辑</a>';
	   w[index]=q;
	});
	var joinhtml='<select id=yxyFilter_skips>';
	for(var p=0;p<msg.pageBean.totalPage;p++){
		if((p+1)==yxyFilter_currpage){
			joinhtml+='<option selected=selected>'+(p+1)+'</option>';
		}else{
			joinhtml+='<option>'+(p+1)+'</option>';
		}
		
	}
	joinhtml+='</select>';
	
	var joinhtml1='<select id=yxyFilter_page>';
	for(var o=50;o<=200;o=o+50){
		if(o==yxyFilter_pageNum){
			joinhtml1+='<option selected=selected>'+o+'</option>';
		}else{
			joinhtml1+='<option>'+o+'</option>';
		}
	}
	joinhtml1+='</select>';
	$("#yxyFilter_list").lgytable({
		tbinfo:'过滤地址',
		tbtitle:['<input type="checkbox" id="Filter_checkeAll" class="checkeAll" name="Filter_checkbox1" onClick="Filter_checkeAll()" value="yes"/>|center','过滤地址', '添加时间','所属分组', '过滤理由','操作'], 
		tbwidth:['50','200','100', '100', '200','100'],
		tbcon:[{"tbcon":w,"dqpage":msg.pageBean.currentPage,"tolpage":msg.pageBean.totalPage,"tolcount":msg.pageBean.totalRecord}],
		homepage:function(){
			findFilterAddress(1);
		},
		prepage:function(){
			yxyFilter_currpage=msg.pageBean.currentPage;
			if(yxyFilter_currpage>1){
				findFilterAddress(yxyFilter_currpage-1);
				yxyFilter_currpage--;
			}
		},
		nextpage:function(){
			yxyFilter_currpage=msg.pageBean.currentPage;
			if(yxyFilter_currpage<msg.pageBean.totalPage){
				findFilterAddress(yxyFilter_currpage+1);
				yxyFilter_currpage++;
			}
		},
		lastpage:function(){
			findFilterAddress(msg.pageBean.totalPage);
		},
		tbsetnumtxt:joinhtml1,
		tbsetnum:function(){//每页显示量
			yxyFilter_pageNum=$("#yxyFilter_page").val();
			findFilterAddress(1);
		},
		tbsetpagetxt:joinhtml,
		tbsetpage:function(){//跳转到某页
			yxyFilter_currpage=$("#yxyFilter_skips").val();
			findFilterAddress(yxyFilter_currpage);
		}
	});		
}

//添加过滤分组类别
function yxyFilter_AddFilter(){
	//判断是否输入了分组名
	if($("#yxyFilter_AddName").val()==null||$("#yxyFilter_AddName").val()==""){
		alert("请输入分组名");
	}else{
		$.ajax({
		type:"post",
		cache: false,
		async:false,
		type: "post", 
		url: "filter_addFilter.action", 
		dataType:"json",
		data:{
			filterName:$("#yxyFilter_AddName").val()
		},
		error: function() {alert("异常");}, 
		success: function(msg) {
			if(msg==1){
				findFilter();
				upseccess("添加成功");
				yxy_confirmTerm('yxyFilter_adddiv');
			}else{
				alert("添加失败!");
				yxy_confirmTerm('yxyFilter_adddiv');
			}
		}
	});
  }		
}

//添加过滤地址提交
function yxyFilter_addrcommit(){
	if($("#yxyFilter_addAddr").val()==null||$("#yxyFilter_addAddr").val()==""){
		alert("请输入过滤地址!");
	}else{
		$.ajax({
		type:"post",
		cache: false,
		async:false,
		type: "post", 
		url: "filter_addFilterAddressPi.action", 
		dataType:"json",
		data:{
			addFilterAddress:$("#yxyFilter_addAddr").val(),
			filterreason:$("#yxyFilterReason").val(),
			filterID:$("#yxyFilter_List").val()
		},
		error: function() {alert("异常");}, 
		success: function(msg) {
			if(msg.result==1){
				var addSuccessNum=msg.resultList[0]-(msg.resultList[1]+msg.resultList[2]);
				$("#yxyFileter_alert").html("添加成功:"+addSuccessNum+"条,总共有:"+msg.resultList[0]+"条,其中重复:"+msg.resultList[1]+"条,不合法:"+msg.resultList[2]+"条");
				$("#yxyFileter_alert").show();
				//upseccess();
				findFilterAddress(yxyFilter_currpage);
				
			}else{
				alert("添加失败!");
			}
		}
	});
	}
	
}

//手动自动切换
function yxyFilter_addrbutton(value){
	if(value==0){
		$("#yxyFilter_handadd").show();
		$("#yxyFilter_importdiv").hide();
	}else{
		$("#yxyFilter_importdiv").show();
		$("#yxyFilter_handadd").hide();
	}
}

//批量导入地址
function yxyFilter_importAddress(){
	if($("#yxyFilter_addr").val()==null||$("#yxyFilter_addr").val()==""){
		alert("请选择需要导入的文件");
	}else{
		//定义
		 var option={
		   url: 'filter_addFilterAddressFile.action',
		   dataType:'json',
		   data:{
			   addrFileName:$("#yxyFilter_addr").val(),
			   filterID:$("#yxyFilter_List").val(),
			   filterreason:$("#yxyFilter_iContext").val()
			},
			async:false,
	    	cache: false,
			complete: function(){
			yxyloadpage('sendFilter.jsp');
			},
		   success: function(msg){
			   if(msg.result==1){
				   var successNum=msg.resultList[0]-(msg.resultList[1]+msg.resultList[2]);
				   $("#yxyFileter_alert").html("总共有:"+msg.resultList[0]+"条,成功导入:"+successNum+"条,不合法有:"+msg.resultList[1]+"条,重复有:"+msg.resultList[0]+"条.");
				$("#yxyFileter_alert").show();
				   //upseccess();
				}else{
					upseccess("导入异常!")
				}
		   }    				
		}
	$("#yxyFilter_importForm").ajaxSubmit(option);
	}
}
//全选全不选
function Filter_checkeAll(){
	if($("#Filter_checkeAll").val()=="yes"){
		$("#Filter_checkeAll").attr("value","no");
		//全选
		$("input[name='Filter_checkbox']").each(function(){
			$(this).attr("checked","checked");
		});		
	}else if($("#Filter_checkeAll").val()=="no"){
		$("#Filter_checkeAll").attr("value","yes");
		//全不选
		$("input[name='Filter_checkbox']").each(function(){
			$(this).attr("checked",false);
		 });			
	}
}

//选择checkbox
function Filter_chooseCheckbox(){	
	var Addr_checkObj= $(".Filter_checkbox");
	$.each(Addr_checkObj,function(s,row){
		if($(row).attr("checked")=="checked"){		
			yxyFilter_AddressID+=$(this).val()+',';		
		}						 
	});
}

//删除地址
function delFilterAddress(){
	//选择要删除的地址
	yxyFilter_AddressID="";
	Filter_chooseCheckbox();
	if(yxyFilter_AddressID==null||yxyFilter_AddressID==""){
		alert("请选择要删除的地址!");
	}else{
		var a= confirm("你确定删除该地址?"); 
		if(a==true){			
			$.ajax({
				type:"post",
				cache: false,
				async:false,
				type: "post", 
				url: "filter_delFilterAddressID.action", 
				dataType:"json",
				data:{
					addressID:yxyFilter_AddressID
				},
				error: function() {alert("异常");}, 
				success: function(msg) {
					if(msg==1){
						findFilterAddress(yxyFilter_currpage);
					}else{
						alert("删除失败!");
					}
				}
			});
		}else{
			return ;
		}
	}
}

//编辑地址
function editFilterAddress(addressID,context,address,filterID){
	yxyFilter_initaddress=address;
	before_submit_address = addressID + "###" + address + "###" + context + "###" + filterID;
	//弹出层
	yxy_tipsWindown("编辑过滤地址","yxyFilter_editAddressdiv",400);
	$("#yxyFilter_AddressName").val(address);
	$("#yxyFilter_AddressBody").val(context);
	$("#yxyFilter_addressID").val(addressID);
	//查询所属分组			
	$.each(yxyFilter_leiList,function(s,c){
		if(c.filterid==filterID){
			$("#filterID"+c.filterid).attr("selected","true");
			return false;
		}				
	});
}

//编辑提交
function editFilterAddressCommit(div){
	//判断提交的数据是否正确
	if($("#yxyFilter_AddressName").val()==""||$("#yxyFilter_AddressBody").val()==""){
		alert("请输入正确的数据");
	}else{
			//判断是否存在
			filterisexites();
			if(yxyFilter_isesites==1){
				$.ajax({
					type:"post",
					cache: false,
					async:false,
					type: "post", 
					url: "filter_updateFilterAddress.action", 
					dataType:"json",
					data:{
						addFilterAddress:$("#yxyFilter_AddressName").val(),
						filterreason:$("#yxyFilter_AddressBody").val(),
						filterID:$("#yxyFilter_editinfo").val(),
						addressID:$("#yxyFilter_addressID").val(),
						before_submit_address:before_submit_address,
						after_submit_address:$("#yxyFilter_addressID").val()+"###"+$("#yxyFilter_AddressName").val()+"###"+$("#yxyFilter_AddressBody").val()+"###"+$("#yxyFilter_editinfo").val()
					},
					error: function() {alert("异常");}, 
					success: function(msg) {
						if(msg==1){
							findFilterAddress(yxyFilter_currpage);
							upseccess("编辑成功");
							yxy_confirmTerm(div);
							
						}else if(msg==2){
							upseccess("修改的地址不合法!");
						}else if(msg==3){
							upseccess("修改的地址已存在!");
						}else if(msg==0){
							upseccess("编辑异常!");
						}
					}
				});
			}else{
				upseccess("验证未通过!");
			}
			
	}
}

//判断编辑地址是否存在
function filterisexites(){	
	if(yxyFilter_initaddress==$("#yxyFilter_AddressName").val()){
		yxyFilter_isesites=1;
		$("#yxyFilter_addris").html("");
	}else{
		
		$.ajax({
			type:"post",
			cache: false,
			async:false,
			type: "post", 
			url: "filter_findbyaddress.action", 
			dataType:"json",
			data:{
				addFilterAddress:$("#yxyFilter_AddressName").val(),
			},
			error: function() {alert("异常");}, 
			success: function(msg) {
				if(msg==1){//可用
					yxyFilter_isesites=1;
					$("#yxyFilter_addris").html("");
				}else{//不可用
					yxyFilter_isesites=0;
					$("#yxyFilter_addris").html("地址已存在!");
				}
			}
		});
	}
	
}

//过滤分组管理
function FiterOpen(){
	$.weeboxs.open('#yxyFilterMangeDiv', {
		title:"过滤分组管理",
        onopen:function() {

			findFilter2();
		},
		onok:function(box){
			
			box.close();//增加事件方法后需手动关闭弹窗
		},
		width:500,
		height:200,
		showButton:false,
		oncancel:function(box){
			box.close();//增加事件方法后需手动关闭弹窗
		}
	});
}

//编辑过滤分组类别框
function edit_fiterType(id,filername){
	$.weeboxs.open('#yxyFilterEditDiv', {
		title:"过滤分组编辑",
        onopen:function() {
			$("#editTypeName").val(filername);
		},
		onok:function(box){
			comitFilter(id,filername);
			box.close();//增加事件方法后需手动关闭弹窗
		},
		width:300,
		height:50,
		oncancel:function(box){
			box.close();//增加事件方法后需手动关闭弹窗
		}
	});
	
}
//编辑过滤分组类别提交
function comitFilter(id,oldFilterName){
	$.ajax({
		type:"post",
		cache: false,
		async:false,
		type: "post", 
		url: "filter_editFilter.action", 
		dataType:"json",
		data:{
			filterID:id,
			filterName:$("#editTypeName").val(),
			oldFilterName:oldFilterName
		},
		error: function() {alert("异常");}, 
		success: function(msg) {
			
			if(msg==1){
				//$("#nameLast"+id).html($("#editTypeName").val());
				findFilter2();
				//findFilter();
				//FiterOpen();
			}else{
				alert("编辑失败!");
			}
		}
	});
}

//查询发送过滤类别(要第二次重新加载才行)
function findFilter2(){
	$.ajax({
		type:"post",
		cache: false,
		async:false,
		type: "post", 
		url: "filter_findSendFilterAll.action", 
		dataType:"json",
		error: function() {alert("异常");}, 
		success: function(msg) {
			var w= new Array();
			yxyFilter_leiList=msg;
			var html='';
			html+='<option value=0>默认分组</option>';
			var htmls='<option id="filterID0" value=0>默认分组</option>';
			//迭代类别
			if(msg!=null&&msg!=""){
				$.each(msg,function(s,row){
					htmls+='<option id=filterID'+row.filterid+' value='+row.filterid+'>'+row.filtername+'</option>';		
					html+='<option value='+row.filterid+'>'+row.filtername+'</option>';	
					//类别管理
					var q= new Array();
					q[0]=(s+1)+"";
					q[1]='<span id=nameLast'+row.filterid+'>'+row.filtername+'<span>';
					var fiterDate=row.filterdate.split(" ");
					q[2]='<span title='+fiterDate[1]+'>'+fiterDate[0]+'</span>';
					q[3]='<a href="javascript:void(0);" onclick="edit_fiterType('+row.filterid+',\''+row.filtername+'\')">编辑</a>|<a href="javascript:void(0);" onclick="del_fiterType('+row.filterid+')">删除</a>|<a href="javascript:void(0);" onclick="clear_fiterType('+row.filterid+')">清空</a>';
				   	w[s]=q;
				});
				$(".dialog-content").lgytable({
					tbinfo:'分组管理',//"草稿邮件",
					tbpageshow:false,
					tbtitle:['编号','分组名', '建立时间','操作'], 
					tbwidth:['30','100','100', '100'],
					tbcon:[{"tbcon":w,"dqpage":0,"tolpage":0,"tolcount":0}]
				});
			}			
			$("#yxyFilter_info").html(html);
			$("#yxyFilter_List").html(html);
			$("#yxyFilter_editinfo").html(htmls);
		}
	});	
}

//删除过滤分组
function del_fiterType(id){
	var a= confirm("确定删除?"); 
	if(a==true){
		$.ajax({
			type:"post",
			cache: false,
			async:false,
			type: "post", 
			url: "filter_delFilter.action", 
			dataType:"json",
			data:{
				filterID:id,
			},
			error: function() {alert("异常");}, 
			success: function(msg) {
				if(msg==1){
					findFilter2();
				}else{
					alert("操作失败!");
				}
			}
		});
	}
}

//清空过滤分组类别
function clear_fiterType(id){
	var a= confirm("确定清空该类别下的地址?"); 
	if(a==true){
		$.ajax({
			type:"post",
			cache: false,
			async:false,
			type: "post", 
			url: "filter_clearFilter.action", 
			dataType:"json",
			data:{
				filterID:id,
			},
			error: function() {alert("异常");}, 
			success: function(msg) {
				if(msg==1){
					findFilterAddress(yxyFilter_currpage);
					alert("操作成功!");
				}else{
					alert("操作失败!");
				}
			}
		});
	}
}