(function(KDApi,$,_){
	function MyComponent(model){
		this._setModel(model);
	}

	var isUpdate = false;
	var searchData;
	var currentIndex;
	MyComponent.prototype = {
		_setModel: function(model) {
			this.model = model;
		},
		init:function(props){
			console.log('-----init',this.model,props)
			isUpdate = false; 
			setHtml(this.model,props,isUpdate);
		},
		update:function(props){
			console.log('-----update',this.model,props)
			isUpdate = true;  
			if(props.data){
				if(props.data.changeModel){
					setHtml(this.model,props,isUpdate);
				}else{
					setPageValue(this.model,props);
					setFloatPageValue(this.model,props);
					hidemodel(props);
				reloadtree(props.data); //加载树
			}  	
		}
	},
	destoryed:function(){
		console.log('-----destoryed',this.model)
	}

}     
var setHtml = function(model,props,isUpdate){  		
	var popsData = {};
	if (props != null && props.data != null) {
		popsData = props.data;			
	}
	KDApi.loadFile("./jstree.min.js", model, function(){
		KDApi.loadFile("./style.css", model, function(){
			KDApi.loadFile("./nice-select.css", model, function(){
				KDApi.loadFile("./jquery.nice-select.js", model, function(){
					KDApi.loadFile("./jquery.nice-select.min.js", model, function(){
						KDApi.loadFile("./themes/default/style.min.css", model, function(){
							KDApi.loadFile("./themes/default/tree.css", model, function(){
								KDApi.templateFilePath("./html/task.html", model).then(
									function(result) {
										model.dom.innerHTML = result;						
										initTree(model, popsData);
										setStyle(model, props);
										setPageValue(model,props);
										setFloatPageValue(model,props);
										hidemodel(props);
									// 绑定DOM操作事件
									initEvent(model, props)
								}
								);
							});
						});
					});
				});
			});
		});
	});  
} 

function setPageValue(model, props){
	$("#submitspan").html((props.data && props.data.submit) ? props.data.submit : '0');
	$("#unsubmitspan").html(props.data.unsubmit ? props.data.unsubmit : '0');
	$("#allspan").html(props.data.all ? props.data.all : '0');
	$("#willoverspan").html(props.data.willover ? props.data.willover : '0');
	$("#overspan").html(props.data.over ? props.data.over : '0');
}

function setFloatPageValue(model, props){
	$("#billamount").text(props.data.billamount ? props.data.billamount : '0');
	$(".input1").val(props.data.model ? props.data.model : '');
}

function setStyle(model, props){
		 $(".floatbox").hide();   //默认隐藏
         $('select').niceSelect();  //下拉框样式
         $(".taskaccount").hover(function(){
         	$(".floatbox").show();
            var value=$(this).attr("id"); //获取鼠标悬停的id的值
            setSelectValue(value);
            model.invoke('hover', {"taskstatus":value});
        },function(){
        	$(".floatbox").hide();
        });
         $(".floatbox").hover(function(){
            $(".floatbox").show();  //当鼠标在树形界面时也保持显示
        },function(){
        	$(".floatbox").hide();
        });
     }

     function setSelectValue(selectId){
     	if(selectId=="submit"){
     		$("select").val("1");
     	}else if(selectId=="unsubmit"){
     		$("select").val("0");
     	}else if(selectId=="all"){
     		$("select").val("2");
     	}else if(selectId=="willover"){
     		$("select").val("3");
     	}else if(selectId=="over"){
     		$("select").val("4");
     	}
     	$('select').niceSelect('update');
     }

	// DOM节点操作函数
	var initEvent = function (model, props) {
		$('.imgdiv', model.dom).click(function () {
      model.invoke('modelclick', {"model":"model"});   //体系点击
  });
		$('.input2', model.dom).keyup(function(event){
			 	if(event.keyCode ==13){    
		//搜索时键盘回车触发
		$('#jstree_demo_div').jstree(true).search($('.input2').val());
		  		 //model.invoke('keyenter', {"value":$('.input2').val()});
	 	}
});
		$('#left', model.dom).click(function () {
    	//搜索前一个
    	//model.invoke('click', {"search":"pre"});
    	
    	if(searchData){
    		if(currentIndex>0){
    		currentIndex=currentIndex-1;
    		var selectNodeid="#"+searchData[currentIndex];
    		var selected=searchData[currentIndex];
    		$("#jstree_demo_div").jstree("deselect_all", true);
        		// id是选中的节点id，然后后面的一个参数 true表示的是不触发默认select_node.change的事件 
    		$('#jstree_demo_div').jstree('select_node', selected ,true);
			// if(selectNodeid){
			// 	if($(selectNodeid).offset().top>600){
			// 		scrollTo(selectNodeid,300);
			// 	}
			// }
    		}
    		
    	}

    })
		$('#right', model.dom).click(function () {
    	//搜索后一个
    	//model.invoke('click', {"search":"next"});
    	if(searchData){
    		if(currentIndex<searchData.length-1){
    		currentIndex=currentIndex+1;
    		var selectNodeid="#"+searchData[currentIndex];
    		var selected=searchData[currentIndex];
    		$("#jstree_demo_div").jstree("deselect_all", true);
        		// id是选中的节点id，然后后面的一个参数 true表示的是不触发默认select_node.change的事件 
    		$('#jstree_demo_div').jstree('select_node', selected ,true);
			// if(selectNodeid){
			// 	if($(selectNodeid).offset().top>600){
			// 		scrollTo(selectNodeid,300);
			// 	}
			// }
    		}
    		
    	}
    })

	}
	function reloadtree(props){
		if($('#jstree_demo_div').jstree(true).settings && props['treeData']){
			$('#jstree_demo_div').jstree(true).settings.core.data=props['treeData'];
			$('#jstree_demo_div').jstree(true).refresh();
			$('#jstree_demo_div').bind("refresh.jstree", function (event, data) {
				$('#jstree_demo_div').jstree("open_all");
        	//setNodeColor(props);
        	setSelectedNode(props)
        });
		}else{
			//setNodeColor(props);
			setSelectedNode(props)
		}						
	}

	function setSelectedNode(props){
		if(props && props.selectedNode){
			$("#jstree_demo_div").jstree("deselect_all", true);
        // id是选中的节点id，然后后面的一个参数 true表示的是不触发默认select_node.change的事件 
    		$('#jstree_demo_div').jstree('select_node', props.selectedNode ,true);
			// var selectNodeid="#"+props.selectedNode;
			// if(selectNodeid){
			// 	scrollTo(selectNodeid,300);
			// }		
		}	
	}

function scrollTo(ele, speed){
	if(!speed) speed = 300;
	if(!ele){
		$("#jstree_demo_div").animate({scrollTop:0},speed);
	}else{
		if(ele.length>0 && $(ele).offset()) $("#jstree_demo_div").animate({scrollTop:$(ele).offset().top-300},speed);
	}
	return false;
}

function hidemodel(props){
	if(props && props.data.hidemodel){
		$(".basedata").hide();
	}
}

function setNodeColor(props){
	if(props && props.searchNode){
		var searchNodeid="#"+props.searchNode+'_anchor';
		if(props.searchNodeColor){
			var color=props.searchNodeColor;
			$(searchNodeid).css("color", color);
		}else
		$(searchNodeid).css("color", 'red');
	}
}
	// function resolveData(type,arr){		
	// 	for(var i=0;i<arr.length;i++){
	// 		if(type===arr[i].type){
	// 			arr[i]['state'] = {
	// 				//'opened': true,
	// 				'selected': true
	// 			}			
	// 			break;
	// 		}else{
	// 			if(arr[i].children){
	// 				resolveData(type,arr[i].children)
	// 			}
	// 		}
	// 	}				
	// }

	var initTree = function(model,props){ 
    	//加载树
    	var treeData = props['treeData'];
		var type = props['selected'];//data.node.original.type;			
		// if(type){
		// 	resolveData(type,treeData);
		// 	model.invoke('changeData',{"number":type});
		// }	
		
		var level = 2;
		$(
			function () {
				$('#jstree_demo_div').jstree(
				{
					'plugins' : ['state','search'],
					'core': {
						'themes': {
							'icons': false,
							'dots': false
						},
						'data': treeData,
						'state': {'opened':true,},
						'multiple': false,
					},
				}
				);
				
			}
			);

		// $('#jstree_demo_div').on("changed.jstree", function (e, data) {
		// 	//        
		// 	var node = data.selected;
		// 	var opened = $('#jstree_demo_div').jstree(true).is_open(node);
		// 	if (opened) {
		// 		//$('#jstree_demo_div').jstree(true).close_node(node)
		// 	} else {
		// 		//$('#jstree_demo_div').jstree(true).open_node(node)
		// 	}        
		// });

		$('#jstree_demo_div').on("activate_node.jstree",function(node, event){        
			//点击节点 调用服务端更新页面
			var currentNode = event.node;
			model.invoke('clickNode',{"currentNode":currentNode});  
		//	if($('#jstree_demo_div').jstree(true).is_leaf(event.node.id)){
				$(".floatbox").hide();
		//	} 
		}).on('hover_node.jstree', function (node, event) { //鼠标移上事件
			var currentNode = "#"+event.node.id;
			if(!$(currentNode).attr('title')){
				$(currentNode).attr("title",event.node.text);
			}

		}).on('search.jstree', function (nodes, str, res) {
			if (str.nodes.length!=0) {
            	//console.log(nodes);
            var selectNodeid="#"+str.res[0];
            currentIndex=0;
            searchData=str.res;
            $("#jstree_demo_div").jstree("deselect_all", true);
        		// id是选中的节点id，然后后面的一个参数 true表示的是不触发默认select_node.change的事件 
    		$('#jstree_demo_div').jstree('select_node', str.res[0] ,true);
			if(selectNodeid){
				scrollTo(selectNodeid,300);
			}
		}
		}).on('clear_search.jstree', function (nodes, str, res) {
            searchData=null;
			currentInde=0;
		});

		$('#jstree_demo_div').on("ready.jstree", function (e, data) {
			//控制箭頭展示
			$("#jstree_demo_div li[aria-level="+level+"] .jstree-anchor").addClass("no-arrow");
			$('#jstree_demo_div').jstree().open_all();
		});
	}  
	
	KDApi.register('ebtasktree', MyComponent)
})(window.KDApi,jQuery);
