var index = parent.layer.getFrameIndex(window.name); //获取窗口索引

	$(document).ready(function(){
			layui.use(['form'], function(){
			  var form = layui.form;
			});
			$.ajax({
	            method : "post",
	            url : "/pqs9900/permission/getfirst",
	            dataType:"json",
	            success : function(data) {
	                $.each(data, function(i, item) {
	                	$("#first").append(
								"<option value='"+item.node+"'>"
										+ item.name + "</option>");
                        renderForm();
	                })
	            }
	        })
		
	});
	
	function renderForm(){
		layui.use(['form'], function(){
			  var form = layui.form;
			  form.render();
			  form.on('select(first)', function(data){
			  	  $('#second').html('');
				  var first =data.value; //得到被选中的值
				  if(first==''){
                      $("#second").append(
                          "<option value='"+''+"'>"
                          + '---------------'+ "</option>");
                      renderForm();
				  }else{
                      $.ajax({
                          method : "post",
                          url : "/pqs9900/permission/getsecond",
                          data : {
                              first:first
                          },
                          success : function(data) {
                              if(data.length==0){
                                  $("#second").append(
                                      "<option value='"+''+"'>"
                                      + '---------------'+ "</option>");
                                  renderForm();
                              }else{
                                  $("#second").append(
                                      "<option value='"+''+"'>"
                                      + '---------------'+ "</option>");
                                  $.each(data, function(i, item) {
                                      $("#second").append(
                                          "<option value='"+item.node+"'>"
                                          + item.name + "</option>");
                                      renderForm();
                                  })
                              }
                          }
                      })
				  }
				});
			});
	}
	$("#cancleAdd").click(function(){
		parent.layer.close(index);
	});
	
	
	$("#addPermission").click(function(){
		var first =$("#first").val();
        var second =$("#second").val();
		//资源描述
		var description=$("#permissionDescription").val();
		description=description.replace(/(^\s*)|(\s*$)/g, '');
		//资源名称
		var name=$("#permissionName").val();
		name=name.replace(/(^\s*)|(\s*$)/g, '');
		//资源路径
		var path=$("#permissionPath").val();
		path=path.replace(/(^\s*)|(\s*$)/g, '');
		var re = /^[\u4e00-\u9fa5a-z0-9]+$/;//只能输入汉字和英文字母
		if (!re.test(name)) {
			layer.tips('资源名非法', '#permissionName', {
				  tips: [1, 'red'] //还可配置颜色
				});
			$("#permissionName").val("");
			$("#permissionName").prop("placeholder","资源名由汉字、数字、字母组成");
		  } else {
			  if(path==""){
				  layer.tips('资源路径不可为空', '#permissionPath', {
					  tips: [1, 'red'] //还可配置颜色
					});
				$("#permissionPath").val("");
			  }else{
				  $.ajax({
					  url:'/pqs9900/permission/addPermission',
					  type:'POST',
					  dataType:"text",
					  data:{
							first:first,
							second:second,
							description:description,
							name:name,
							path:path
						},
					 success:function(data){
						//没有权限的请求
							if(data==300){
								location.href='/pqs9900/auth/unauthorized';
							//没有登陆的请求
							}else if(data==301){
								location.href='/pqs9900';
							}else{
								var info = $.parseJSON(data);
								if(info.code!=200){
									layer.msg(info.message, {icon: 5});
								}else{
									layer.msg(info.message, {
										  icon:6,
										  time: 1000 
										}, function(){
											parent.layer.close(index);
										}); 
								}
							}	
					 }  
				  }); 
			  }
		  }
	});
	
