<?php //echo Category::PrintULTree();?>
<div id="mm" class="easyui-menu" style="width:120px;">
	<div onclick="append()" data-options="iconCls:'icon-add'">添加子结点</div>
	<div onclick="appendRoot()" data-options="iconCls:'icon-add'">添加根结点</div>
	<div onclick="remove()" data-options="iconCls:'icon-remove'">删除</div>
	<div class="menu-sep"></div>
	<div onclick="rename()" data-options="iconCls:'icon-edit'">改名</div>
	<div class="menu-sep"></div>
	<div onclick="expand()" data-options="iconCls:'icon-add'">展开</div>
	<div onclick="collapse()" data-options="iconCls:'icon-remove'">收缩</div>
</div>
<script type="text/javascript">
	$(function(){
		$('#tt').tree({
			url:'<?php echo $this->createUrl('mytree')?>',
			animate:true,
			lines:true,
			onClick: function(node){
				$(this).tree('toggle', node.target);
			},
			onContextMenu: function(e,node){
				e.preventDefault();
				$(this).tree('select',node.target);
				$('#mm').menu('show',{
					left: e.pageX,
					top: e.pageY
				});
			},
			onDblClick:function(node){
				$(this).tree('beginEdit',node.target)
			},
			onAfterEdit:function(node){
			if($.trim(node.text)==""){alert("目录名称不能为空！");$(this).tree('beginEdit',node.target).focus();return false;}
			$.ajax({
				type: 'GET',
				url: '<?php echo $this->createUrl('update')?>',
				data: {id:node.id,title:$.trim(node.text)},
				success: function(data){//alert(data);
				   if(data=="yes"){
					$.messager.alert('系统提示','更新名称成功','info');
					//window.location.reload();
					$('#tt').tree('reload');//因为不是json数据
				   }else $.messager.alert('系统提示','更新名称失败!','info');
			   
			   },
			  dataType: "text"
			});	
			
				//alert(node.text);
			},
			onCancelEdit:function(node){
				if($.trim(node.text)==""){alert("目录名称不能为空！");$(this).tree('beginEdit',node.target).focus();return false;}
			}
		});
	});
	function collapse(){
		var node = $('#tt').tree('getSelected');
		$('#tt').tree('collapse',node.target);
	}
	function expand(){
		var node = $('#tt').tree('getSelected');
		$('#tt').tree('expand',node.target);
	}
	function remove(){
		var node = $('#tt').tree('getSelected');
		
		$.messager.confirm('确认','确定要删除吗？',function(r){
			if(r){
				$.ajax({
					type: 'GET',
					url: '<?php echo $this->createUrl('delete')?>',
					data: {id:node.id},
					success: function(data){//alert(data);
					   if(data=="yes"){
						$.messager.alert('系统提示','删除成功','info');
						//window.location.reload();
						$('#tt').tree('reload');//因为不是json数据
					   }else $.messager.alert('系统提示','删除失败!','info');
				   
				   },
				  dataType: "text"
				});			
				$('#tt').tree('remove', node.target);
			}
		})
			
	}
	function rename(){
		var node = $('#tt').tree('getSelected');
		$('#tt').tree('beginEdit', node.target);
	}
	function append(){
		var node = $('#tt').tree('getSelected');
		$('#tt').tree('append',{
			parent: (node?node.target:null),
			data:[{
				text:'未命名',
				checked:true,
			}]
		});

		$.ajax({
			type: 'POST',
			url: '<?php echo $this->createUrl('create')?>',
			data: {id:node.id,root:'no'},
			success: function(data){//alert(data);
			   if(data=="yes"){
				$.messager.alert('系统提示','添加子目录成功','info');
				//window.location.reload();
				$('#tt').tree('reload');//因为不是json数据
			   }else $.messager.alert('系统提示','添加子目录失败!','info');
		   
		   },
		  dataType: "text"
		});	
		
	}
	function appendRoot(){
		//var node = $('#tt').tree('getSelected');
		$('#tt').tree('append',{
			//parent: (node?node.target:null),
			data:[{
				text:'未命名',
				checked:true
			}]
		});
		
		$.ajax({
			type: 'POST',
			url: '<?php echo $this->createUrl('create')?>',
			data: {root:'yes'},
			success: function(data){//alert(data);
			   if(data=="yes"){
				$.messager.alert('系统提示','添加根目录成功','info');
				//window.location.reload();
				$('#tt').tree('reload');//因为不是json数据
			   }else $.messager.alert('系统提示','添加根目录失败!','info');
		   
		   },
		  dataType: "text"
		});	
		
		//var node = $('#tt').tree('find', id);

//$('#tt').tree('select', node.target);

		//var node2 = $('#tt').tree('getNode',node.target);
		//$("#tt").tree('beginEdit',node.target);id++;

	}
	$(function(){
		$('#create_first_root').click(function() {
		var qq=$.trim($("#root_text").val()).length;
		  if(qq==0){
			alert("请输入结点名称！");$("#root_text").focus();
		  }else{
			$.ajax({
				type: 'POST',
				url: '<?php echo $this->createUrl('create')?>',
				data: {root:'first',title:$.trim($("#root_text").val())},
				success: function(data){//alert(data);
				   if(data=="yes"){
					$.messager.alert('系统提示','添加根目录成功','info');
					//window.location.reload();
					$('#tt').tree('reload');//因为不是json数据
				   }else $.messager.alert('系统提示','添加根目录失败!','info');
			   
			   },
			  dataType: "text"
			});	
		  }
		});	
	})
</script>
<ul id="tt"></ul>

<?php if($count<1) {?>
<input type="text" id="root_text" size="32" value=""/>
<input type="button" id="create_first_root" value="创建第一个结点" />
<?php }?>