/*过滤*/
var filterFlag = false;
$("#filter ul li").click(function(d){
	var isShow = $(this).data("show");
	$(this).data("show",!isShow);
	if(isShow){
		$(this).addClass("fil");
	}else{
		$(this).removeClass("fil");
	}
	//计算需要显示的类型
	var typeArr = [];
	$("#filter ul li").each(function(){
		if(!$(this).data("show")){
			typeArr.push($(this).data("type"));
		}
	});
	graph.para.filterType = typeArr;
	graph.mouseoutNode();
});
$("#filter [data-switch]").click(function(){
	if(filterFlag){
		return;
	}
	filterFlag = true;
	if(!$(this).data("switch")){
		$("#filter").css("background-color","rgb(36, 38, 44)");
		$("#filter").animate({width:"250px"},500,function(){
			$(this).find("ul").show();
			filterFlag = false;
		});
		$(this).data("switch",true);
	}else{
		$("#filter ul").hide();
		$("#filter").animate({width:"36px"},500,function(){
			filterFlag = false;
			$("#filter").css("background-color","#4059b4");
		});
		$(this).data("switch",false);
	}
});
function resetFilter(){
	$("#filter ul li").data("show",true).removeClass("fil");
	graph.para.filterType.splice(0,graph.para.filterType.length);
	if($("#filter [data-switch]").data("switch")){
		$("#filter [data-switch]").click();
	}
};

//筛选的js部分  
//全选
$(".check_all").click(function(){
	if(this.checked){
		$(".check_box").prop("checked",true);
	}else{
		$(".check_box").prop("checked",false);
	}		
});
//判断是否全选
$("#filterCon").delegate(".check_box","click",function(){
	var n=0;
	$(".check_box").each(function(){
		if(this.checked){
			n++;
		}
	});
	if(n==$(".check_box").size()){
		$(".check_all").prop("checked",true);
	}else{
		$(".check_all").prop("checked",false);
	}
});
$("#filterExtend .confirm_btn").click(function(){
    var checked=[];
	$(".check_box").each(function(){
	   if(this.checked){
		  checked.push($(this).data("linkid"));	   
		}
	})
	if(checked.length==0){
		if(!confirm("是否不选择！")){
			return;
		}
	}
	var graphData = $("#filterExtend").data("data");
	var newNodes = [], newLinks = [], newNodeIds = [];
	for(var i=0;i<graphData.links.length;i++){
		if(checked.indexOf(graphData.links[i].id)>=0){
			newLinks.push(graphData.links[i]);
			if(newNodeIds.indexOf(graphData.links[i].source)<0){
				newNodeIds.push(graphData.links[i].source);
			}
			if(newNodeIds.indexOf(graphData.links[i].target)<0){
				newNodeIds.push(graphData.links[i].target);
			}
		}
	}
	for(var i=0;i<graphData.nodes.length;i++){
		if(newNodeIds.indexOf(graphData.nodes[i].id)>=0){
			newNodes.push(graphData.nodes[i]);
		}
	}
	graph.processExtendNodes({nodes:newNodes,links:newLinks});
	$("#filterExtend").hide();
});
$("#filterExtend .delete_btn").click(function(){
	$("#filterExtend").hide();
});
_.MessageManager.on("showFilter",function($ev,result){
	//构建node的id和name映射
	var id2obj = new Object();
	var data = result.data;
	var thisNode = result.thisNode;
	var oldData = result.oldData;
	
	for(var i=0;i<data.nodes.length;i++){
		id2obj[data.nodes[i].id] = data.nodes[i];
	}
	for(var i=0;i<oldData.nodes.length;i++){
		id2obj[oldData.nodes[i].id] = oldData.nodes[i];
	}
	id2obj[thisNode.id] = thisNode;
	var copyLink = graph.clone(data.links); 
	for(var i=0;i<copyLink.length;i++){
		copyLink[i].srcName = id2obj[copyLink[i].source].name;
		copyLink[i].tarName = id2obj[copyLink[i].target].name;
		copyLink[i].thisNode = thisNode;
		copyLink[i].otherType = thisNode.id==copyLink[i].source?id2obj[copyLink[i].target].type:id2obj[copyLink[i].source].type;
		for(var j=0;j<copyLink[i].rel.length;j++){
			copyLink[i].rel[j] = linkNameMap[copyLink[i].rel[j]]!=undefined?linkNameMap[copyLink[i].rel[j]].cnName:"未知";
		}
	}
	var ht = baidu.template("filterCon_template",{link:copyLink});	
	$("#filterCon").html(ht);
	$("#filterExtend .filter_titleCon").html(thisNode.name);
	$("#filterExtend .check_all").prop("checked",false);
	$("#filterExtend").show().data("data",result.data);
});

/*标记*/
var markEnable = 1;
$("#markStatus .confirm_btn").click(function(){
	if(markEnable == 0){
		return;
	}
	markEnable = 0;
	var type = $("#markStatus .markCon").data("choose")+"";
	var content = $("#markReason").val().trim();
	var id = $("#markStatus .filter_titleCon").data("id");
	if(type=="" || type==undefined){
		markEnable = 1;
		alert("请选择状态！");
		return;
	}
	if(type!=10){
		if(content==""){
			markEnable = 1;
			alert("请输入判定理由！");
			return;
		}
	}
	if(content.length>2000){
		markEnable = 1;
		alert("判定理由不能大于2000个字！");
		return;
	}
	$.ajax({
		type:"POST",
		url:urlPath+"/graph/mark.do",
		data:{id:id,markStatus:type,markMess:content},
		dataType:"json",
		success:function(result){
			if(result.status=="succ"){
				graph.markStatus(id,type,content);
			}
			alert(result.mess);
			$("#markStatus").hide();
		},
		error:function(){
			markEnable = 1;
		},
		complete:function(){
			markEnable = 1;
		}
	})	
});
$("#markStatus .delete_btn").click(function(){
	markEnable = 1;
	$("#markStatus").hide();
});
$("#markStatus").delegate(".options span","click",function(){
	var type = $(this).data("type");
	$(this).addClass("active1").siblings().removeClass("active1");
	$("#markStatus .markCon").data("choose",type)
	if(type!=10){
		$("#markStatus .optFor .star").show();
	}else{
		$("#markStatus .optFor .star").hide();
	}
});
_.MessageManager.on("showMarkCon",function($ev,result){
	//清空弹出框
	$("#markStatus .markCon span").removeClass("active1");
	$("#markStatus .markCon").data("choose","");
	$("#markStatus textarea").val("");
	$("#markStatus .filter_people").html("");
	
	$("#markStatus .filter_titleCon").html(result.data.name).data("id",result.data.id);
	if(result.data.markUser!=undefined){
		$("#markStatus .filter_people").html('('+result.data.markUser+')');
		$("#markStatus .filter_title").attr("title",result.data.name+'('+result.data.markUser+')');
	}
	var status = ["10","11","12"];
	var markStatus = result.data.markStatus;
	if(status.indexOf(markStatus)>=0){
		$("#markStatus .markCon span[data-type='"+markStatus+"']").click();
		if(result.data.markMess!=""){
			$("#markStatus textarea").val(result.data.markMess);
		}
	}
	$("#markStatus").show();
});

/*过滤两年内的案件和保单*/
var dateFormat = "yy-mm-dd",
	from = $("#minY").datepicker({
		dateFormat:dateFormat,
		changeMonth:true,
		changeYear:true
	}).on("change",function(){
		to.datepicker("option","minDate",getDate(this));
		$("#preSet span").removeClass("active");
	}),
	to = $("#maxY").datepicker({
		dateFormat:dateFormat,
		changeMonth:true,
		changeYear:true
	}).on("change",function(){
		from.datepicker("option","maxDate",getDate(this));
		$("#preSet span").removeClass("active");
	});
function getDate(element){
	var date;
	try{
		date = $.datepicker.parseDate(dateFormat,element.value);
	}catch(error){
		date = null;
	}
	return date;
}

$("#preSet span").click(function(e){
	e.stopPropagation();
	if($(this).hasClass("active")){

	}else{
		var thisTime = new Date();
		var dayNum = $(this).data("day");
		var minTime = thisTime.getTime()-1000*60*60*24*dayNum;
		var minDate = new Date(minTime);
		
		$(this).siblings().removeClass();
		$(this).addClass("active");
		
		from.datepicker("setDate",minDate);
		to.datepicker("setDate",thisTime);
		from.datepicker("option","maxDate",thisTime);
		to.datepicker("option","minDate",minDate);
	}
});
$("#choSet .btn").click(function(e){
	e.stopPropagation();
	if($(this).hasClass("btn-cancel")){
		$("#choSet input").val("");
		$("#preSet span").removeClass("active");
		from.datepicker("option","maxDate",null);
		to.datepicker("option","minDate",null);
		cancelFilter();
	}else{
		var flag = 1;
		var message = "";
		var maxY = $("#choSet #maxY").val();
		var minY = $("#choSet #minY").val();
		if(maxY=="" || minY==""){
			flag =2;
			message = "日期不能为空！";
		}
		var maxTime = new Date(maxY);
		var minTime = new Date(minY);
		if(maxTime<=minTime){
			flag =3;
			message = "终止时间必须大于起始时间！";
		}
		if(flag==1){
			goFilter(maxTime.getTime(),minTime.getTime());
		}else{
			alert(message);
		}
	}
});
$("#timeFilter").click(function(){
	if($("#timeFilter").data("enable")==0){
		$("#timeFilter").css("background-color","rgb(36, 38, 44)");
		$("#timeFilterCon2").css("opacity",0).show().animate({opacity:1},500);
		$("#timeFilter").data("enable",1);
		//$("#timeInput input").val(($("#timeInput input").data("value")||2015));
	}else{
		$("#timeFilter").css("background-color","#4059b4");
		$("#timeFilterCon2").hide();
		$("#timeFilter").data("enable",0);
	}
});
$("#timeFilterCon2").click(function(e){
	e.stopPropagation();
});
function goFilter(maxT,minT){
	graph.para.timeFilter = true;
	graph.timeFilter(maxT,minT);
	graph.filterselect();
	
	_.MessageManager.fire("getStatic");
	graph.refreshInfoDetail(false);
	graph.mouseoutNode();
}
function cancelFilter(){
	clearTime();
	_.MessageManager.fire("getStatic");
	graph.refreshInfoDetail(false);
	graph.mouseoutNode();
}
function clearTime(){
	graph.para.timeFilter = false;
	graph.para.maxT = 0;
	graph.para.minT = 0;
	graph.recoverTimeFilter();
}
function resetTimeFilter(){
	$("#preSet span").removeClass("active");
	from.datepicker("option","maxDate",null);
	to.datepicker("option","minDate",null);
	
	$("#choSet input").val("");
	clearTime();
	$("#preSet span").removeClass("active");		
}
/*
$("#timeInput span").click(function(e){
	e.stopPropagation();
	if($(this).data("id")=="y"){
		var rule = /^\+?[1-9][0-9]*$/;
		var value = $("#timeInput input").val();
		var y = parseInt(value);
		if(rule.test(y)){
			var nowY = (new Date()).getFullYear();
			if(nowY<=y){
				$("#timeInput em").html("只能输入历史年份！");
			}else{
				graph.para.timeFilter = true;
				graph.timeFilter(nowY-y);
				graph.filterselect();
				
				_.MessageManager.fire("getStatic");
				graph.refreshInfoDetail(false);
				graph.mouseoutNode();
				
				$("#timeInput em").html("");
				$("#timeInput input").data("value",y).val(y);
			}
		}else{
			$("#timeInput em").html("请输入正整数！");
		}
	}else{
		clearTime();
		
		_.MessageManager.fire("getStatic");
		graph.refreshInfoDetail(false);
		graph.mouseoutNode();
	}	
});
*/

/*展开模式*/
$("#extendMode").click(function(){
	if($(this).data("mode")==0){
		$(this).data("mode",1);
		graph.para.extendMode = 1;
		$(this).css("background-color","rgb(36, 38, 44)").attr("title","开启抖动");
	}else{
		$(this).data("mode",0);
		graph.para.extendMode =0;
		$(this).css("background-color","#4059b4").attr("title","开启抖动");
	}
});

//单击点击复制内容
var clipboard = new Clipboard("body .cocyCon");
//复制成功执行的回调，可选
clipboard.on('success', function(e) {
	var left = getLeft(e.trigger)-28;
	var top = getTop(e.trigger)+27;
	$(".tooltips1").remove();
	$("body").append('<div class="tooltips1">复制成功! </div>');
	$(".tooltips1").css({top:top,left:left});
	setTimeout(function(){
		$(".tooltips1").remove();
	},1000);
});

//复制失败执行的回调，可选
clipboard.on('error', function(e) {
	var left = getLeft(e.trigger)-28;
	var top = getTop(e.trigger)+27;
	$(".tooltips1").remove();
	var flag = Clipboard.isSupported();
	var status = flag==false?"浏览器版本不支持！":"复制失败！"
	$("body").append('<div class="tooltips1">'+status+'</div>');
	$(".tooltips1").css({top:top,left:left});
	setTimeout(function(){
		$(".tooltips1").remove();
	},1000);
});
//框选标注
	//选项卡
$("#selectLabel2").delegate(".options span","click",function(){
	$(this).addClass("active1").siblings().removeClass("active1");
	var selectType2 = $(this).parent().find(".active1").data("type");
	//$("#selectLabel2 #markReason2").val("");
	if(selectType2==11 || selectType2==12){
		$("#selectLabel2 .optFor .star").css("display","block");
	}else{
		$("#selectLabel2 .optFor .star").css("display","none");
	}	
});

//框选标注点击确定
$("#selectLabel2 .confirm_btn").click(function(){
	var selectedIDs = [];
	var markStatus = $("#selectLabel2 .options .active1").data("type")+"";
	var markMess = $("#selectLabel2 #markReason2").val()+"";
	if(markStatus!=undefined){
		if(markStatus=="11" || markStatus=="12"){
			if(markMess==""){
				alert("请输入判定该类型的理由！");
				return false;
			}
		}
	}else{
		alert("请选择判定类型！");
		return false;
	}
	
	$("#selectedTabCon2 tr").each(function(){
		selectedIDs.push($(this).data("id"));
	});
	
	$.ajax({
		type:"POST",
		url:urlPath+"/baseInfo/mark2.do",
		data:{
			id:selectedIDs.join(","),
			markStatus:markStatus,
			markMess:markMess
		},
		success:function(result){
			if(result.status=="succ"){
				for(var i=0;i<selectedIDs.length;i++){				
					graph.markStatus(selectedIDs[i],markStatus,markMess);
				}
			}
			alert(result.mess);
			$("#selectLabel2").css("display","none");	
		}
	});
	$("#selectLabel2").css("display","none");	
})
//点击取消是执行
$("#selectLabel2 .delete_btn").click(function(){
	$("#selectLabel2").css("display","none");
});
//点击框选标注出现弹框
_.MessageManager.on("selectLabel",function($ev,data1){
	$("#selectLabel2").show();		
	var ht = baidu.template("selectLabel2_template",{data1:data1.markSelect});
	$("#selectedTabCon2").html(ht);
	$("#selectLabel2 .options span").removeClass("active1");
	$("#selectLabel2 #markReason2").val("");
	$('#selectedTabCon2').perfectScrollbar();

});


function getTop(e){
	var offset = e.offsetTop-e.scrollTop;
	if(e.offsetParent!=null){
		offset+=getTop(e.offsetParent);
	}
	return offset;
}
function getLeft(e){
	var offset = e.offsetLeft-e.scrollLeft;
	if(e.offsetParent!=null){
		offset+=getLeft(e.offsetParent);
	}
	return offset;
}


/*案件图像*/
_.MessageManager.on("getCaseImg",function($ev,result){
	var branchComCode = "9010100";
	var attrs = result.data.attrs;
	for(attr_index in attrs) {
//			cnname:"标的车车架号"
//			name:"daja#CASE_TARGET_CAR_VIN"
//			type:"string"
//			value:"LFPM4ACP3C1A52583"
		if (attrs[attr_index].name == "daja#CASE_UNDERWIRTING_BRANCH_CODE") {
			branchComCode = attrs[attr_index].value;
			break;
		}
	}	
	var userCode = $("#userCode").val();
	var userName = $("#userName").val();
	var userInfo = $("#userInfo").val();
	userInfo = JSON.parse(userInfo);
	if(userInfo.branchComCode==undefined){
		alert("没有权限!");
		return false;
	}
	userInfo.branchComCode = userInfo.branchComCode=="null"?"9010100":userInfo.branchComCode;
	userInfo.branchComName = userInfo.branchComName=="null"?"太保总公司":userInfo.branchComName;
	var xml = getCaseImgXml(userCode,userName,branchComCode,userInfo.branchComCode,userInfo.branchComName,userInfo.empType,result.data.name);
	//console.info(xml);
	console.log("http://10.190.48.90:7001/SunECM/servlet/RouterServlet?format=xml&code=ECM0002&xml="+xml);
	window.open("http://10.190.48.90:7001/SunECM/servlet/RouterServlet?format=xml&code=ECM0002&xml="+xml);
});
function getCaseImgXml(usercode,username,deptcode,comcode,orgname,rolecode,dataId){
	var xml = '<?xml version="1.0" encoding="UTF-8"?>'
			+'<root>'
			+ '<BASE_DATA>'
			+ '<USER_CODE>'+usercode+'</USER_CODE>'
			+ '<USER_NAME>'+username+'</USER_NAME>'
			+ '<ORG_CODE>'+deptcode+'</ORG_CODE>'
			+ '<COM_CODE>'+deptcode+'</COM_CODE>'
			+ '<ORG_NAME>'+orgname+'</ORG_NAME>'
			+ '<ROLE_CODE>cpic</ROLE_CODE>'
			+ '</BASE_DATA><META_DATA>'
			+ '<BATCH>'
			+ '<APP_CODE>CLC</APP_CODE>'
			+ '<APP_NAME>车险理赔</APP_NAME>'
			+ '<BUSI_NO>'+dataId+'</BUSI_NO>'
			+ '</BATCH>'
			+ '<SYSTEM_ID>1</SYSTEM_ID>'
			+ '</META_DATA>'
		    + '</root>';
	return xml;
}