$(function() {

	fetchTOP = function(){
		var _URL = HttpRest+"system/resource/parent.shtml"; 
		$.ajax({
			type: 'get', 
			url: _URL, 
			contentType:"application/json",
			success: function(data) { 
				if (data != null) {
					var h = "<option value='1'>------顶级目录------</option>";
					for ( var i = 1; i < data.length; i++) {
						h+="<option value='" + data[i].id + "'>"+ data[i].name + "</option>";
					}
					$("#pId").html(h);
				} else {
					layer.msg("获取菜单信息错误，请联系管理员！",{icon: 5});
				}
			}
		});
	};

	fetchTOP(); // 初始化上级菜单

	$("#submitBtn").click(function(){
		var name = $("#name").val();
		var resSign=$("#resSign").val();
		var url = $("#url").val();
		var level=$("#level").val();
		var rtype=$("#rtype").val();
		var pId = $("#pId").val();
		if(pId==''||pId=='0'){
			pId=1;
		}
		var open = '1';
		if($("#open").prop('checked')){
			open = '0';
		}
		var params ={
				name:name,
				rtype:rtype,
				resSign:resSign,
				url:url,
				level:level,
				open:open,
				pId:pId
		};

		var _URL = HttpRest+"system/resource/create.shtml"; 
		$.ajax({
			type: 'post', 
			url: _URL, 
			dataType: "json",
			data:$.toJSON(params),   
			contentType:"application/json",
			success: function(data) { 
				if(data.code=='01'){
					layer.confirm('添加成功！是否关闭窗口?', function(index) {
						parent.grid.loadData();
						parent.layer.close(parent.pageii);
						return false;
					});
				}else{
					layer.msg(data.msg,{icon: 5});
				}
			}
		});
	});

});