//var treeNodes = [
//					{"id":"1","text":"SHH012000897654","parentid":"0","imageNum":"","url":"","children":[
//						{"id":"2","text":"报案","parentid":"1","imageNum":"","url":"","children":[
//							{"id":"31","text":"申请类","parentid":"2","imageNum":"20","url":"http://10.192.36.47:7003/ISEntry/UI/ImagesView.jpf?MainIndex=440000000010002&ScanNet=B010100&Type=614&SessionId=c4a5bfd772c30f42b4213405d09620d4","children":[]},
//							{"id":"23","text":"申请类1","parentid":"2","imageNum":"","url":"","children":[]},
//							{"id":"25","text":"申请类2","parentid":"2","imageNum":"","url":"","children":[]},
//							{"id":"26","text":"申请类3","parentid":"2","imageNum":"","url":"","children":[]},
//							{"id":"27","text":"申请类4","parentid":"2","imageNum":"","url":"","children":[]},
//							{"id":"28","text":"申请类5","parentid":"2","imageNum":"","url":"","children":[]}
//						]}
//					]}
//				];
$(function(){
	$("#1408698296650").height($('.yx_con_l').height());
	getImageDate();
//	initTree();
	$("li").find("ul").addClass("yx_l_ul2");
	$('.hitarea').click(function(){
		if($(this).hasClass("yx_l_icon_add")){
			$(this).addClass("yx_l_icon_re").removeClass("yx_l_icon_add");
		}else{
			$(this).addClass("yx_l_icon_add").removeClass("yx_l_icon_re");
		}
	});
});

//资料
var material = [];

var treeNodes = [
//					{"id":"","text":"","parentid":"0","imageNum":"","url":"","children":[
//						{"id":"","text":"","parentid":"1","imageNum":"","url":"","children":treeDate}
//					]}
				];

function createtree(d,url) {
    var list = '';
    var sdocName = '';
    $.each(d, function (i, n) {
        var cls = 'folder';
        if (!n.children){
            cls = 'file';
            if(n.child.length>0){
//            	list += '<li><b></b><a href="#" class="' + cls + '" id="'+n.sBusiTypeID+'" ref="' + n.sBusiTypeID + '" url="'+url+'?MainIndex='+n.sMainIndex+"&ScanNet="+n.sScanNetID+"&Type="+n.sDocTypeID+"&SessionId="+n.sessionId+'">' + n.sMainIndex + '</a><span>('+treeDate.length+')</span>';
            	
            	list += '<li><b></b><a href="#" class="' + cls + '" id="'+n.child[0].sImgID+'" ref="' + n.child[0].sImgID + '" url="'+url+'?MainIndex='+n.child[0].sMainIndex+"&BranchCode="+n.child[0].branchId+"&Type="+n.child[0].sDocTypeID+"&Page=1&SessionId="+n.child[0].sessionId+'">' + n.text + '</a><span></span>';
            }
        }else{
        	if(n.parentid==0){
        		if(n.text=="")
        			n.text = $(".claimNo").val();
        	}
//        	if(n.parentid==1){
//        		for(var treeEle in treeDate){
//        			sdocName = treeDate[treeEle].sDocTypeName;
//        		}
//        		if(sdocName!=undefined)
//        		n.text = sdocName;
//        	}
        	list += '<li><span class="' + cls + '" ref="' + n.id + '"  pid="'+n.parentid+'" id="'+n.id+'" url="'+n.url+'"><A href="#">' + n.text + '</A></span>';
        }
        if (n.children) {
            list += "<ul>";
            list += createtree(n.children,url);
            list += "</ul>";
        }

        list += "</li>";
    });
    return list;
}


function initTree(url){
	$('#product_tree').empty().append(createtree(treeNodes,url));
	$('body').data('ptree', treeNodes);
	$('#product_tree .file').click(function () {
		var id = $(this).attr("ref");
		var txt = $(this).text();
		$("#1408698296650").attr("src",$("#"+id).attr("url"));
		$('#product_tree span>a').removeClass('current');

		$(this).children('a').addClass('current');

		$('form').data("treeid", {"id":id,"txt":txt});

	});

	$('#product_tree span').contextMenu('myMenu1', {
		bindings: {
			'add': function(t) {
				var id = $(t).attr('id');
				var pid = $(t).attr('pid');
				var txt = $(t).children('a').text();
				alert('This node info:\n id:'+id+'\n 父ID：'+ pid+'\n文本：'+txt);
			 },
			 'edit': function(t) {
				alert('Trigger was '+t.id+'\nAction was Email');
			 },
			 'delete': function(t) {
				alert('Trigger was '+t.id+'\nAction was Delete');
			 }
		}
	});

	$('#product_tree').treeview({
		animated: "fast",
		collapsed: true,
		control: "#mm1"
	});
}



function getImageDate(){
	var claimNo = $(".claimNo").val();
	var flag = $(".flag").val();
	var branchid = $("#branchid").val();
	var casesource = $("#casesource").val();
	$.ajax({
		type:"POST",
		url:"findImageDate.do",
		data:{"flag":flag,"claimNo":claimNo,"branchid":branchid,"casesource":casesource},
		async: false,
		error :function(){
		},
		success :function(data){
			
//			treeDate.length=0;
//			for(var key in data.midmap.midmap){
//				treeDate.push(data.midmap.midmap[key]);
//			}
//			var url = data.midmap.imgUrl;
			var policyNo = "";
			var url = "";
			//资料类型名称
			var sdocName = "";
			//业务类型名称
			var sBusiTypeName = "";
			var sBusiTypeIDNum = "";
			
			for(var key in data.midmap){
				material = [];
				url = data.midmap[key].imgUrl;
				sBusiTypeIDNum = data.midmap[key].midmap.length;
				var json = {};
				for(var dateKey in data.midmap[key].midmap){
					json[data.midmap[key].midmap[dateKey].sDocTypeID] = data.midmap[key].midmap[dateKey];
//					treeDate.push(data.midmap[key].midmap[dateKey]);
					policyNo = data.midmap[key].midmap[dateKey].sMainIndex;
					sdocName = data.midmap[key].midmap[dateKey].sDocTypeName;
					sBusiTypeName = data.midmap[key].midmap[dateKey].sBusiTypeName;
				}
				for(var d in json){
					var treeDate = [];
					for(var dateKey in data.midmap[key].midmap){
						if(json[d].sDocTypeID==data.midmap[key].midmap[dateKey].sDocTypeID){
							treeDate.push(data.midmap[key].midmap[dateKey]);
							sdocName = data.midmap[key].midmap[dateKey].sDocTypeName;
						}
					}
					var mater = {"id":"","text":sdocName+"("+treeDate.length+")","parentid":"1","imageNum":treeDate.length,"url":url,"child":treeDate};
					material.push(mater);
				}
//				var a = {"id":"","text":policyNo,"parentid":"0","imageNum":"","url":"","children":[
//						                                                      						{"id":"","text":sdocName,"parentid":"1","imageNum":"","url":url,"children":treeDate}
//						                                                      						]};
				var a = {"id":"","text":policyNo,"parentid":"0","imageNum":"","url":"","children":[
	{"id":"","text":sBusiTypeName+"("+data.midmap[key].midmap.length+")","parentid":"1","imageNum":sBusiTypeIDNum,"url":url,"children":material}
	]};
				treeNodes.push(a);
			}
			initTree(url);
		}
	});
}