define(function(require){
	var $ = require('jquery');
	require('uriTemplate')($);
	require('validation')($);
	var IFA=require('util');
	require('dialog')($);
	require("../../../styles/cim/jquery-ui-1.8.14.custom.css");
	$(document).ready(function(){
		
		$('#saveRole').bind('click',save);
		$("#resultForm").validate({
			rules: {
			name:{
			required:true,
			regex:/^[A-Z0-9\_]{1,20}$/,
			checkName:true
				},
			alias:{
			required:true,
			rangelength:[1,20]
					},
			remark:{
			rangelength:[0,200]
				}   
		    }
		});
		$('#cancelRole').bind('click',index);
		$.validator.addMethod("checkName", function(value,element,param) {
		    var name=value.trim();
		    var flag = true;
		    var hiddenUrl = $('#checkNameUrl').val();
		    var urlStr = new $.Template(hiddenUrl);
		  	var urlParam = {"name":name};
		  	var url = urlStr.expand(urlParam);
		  	  $.ajax({
	   			  url: url+".json",
	    		  addidvalue:false,
	    		  async: false,
				  dataType:"json",
				  success:function(response){
					if(response.flag == true){
						//角色name已存在
						flag=false;
					}
				  }
				  });
	    		return flag;
		}, 'System.Role.Code.InUse');
	});
	function save(){
		if($("#resultForm").valid()){
		   var array = tree.getCheckedNodes();
		   var ids = array.join(",");
		   if(ids!=""){
			   if(ids==0){
				   $.msg.alert('提示信息','角色必选');
			   }
			   else{
				  ids=ids.substring(2);
				  $("#nodeIds").val(ids);
			      var url = $('#createUrl').val();
			      if($("#resultForm").valid()){
			    	  $.ajax({
				      type:"post",
				      url : url+".json",
				      data:$("#resultForm").serialize(),
				      success:function(response) {
					      if(response.status=="success"){
				    		  $.msg.alert('提示信息','创建成功',index);
						  }
			    	  	},
			   	      error : function() {
				    	  $.msg.alert('提示信息','系统出错',index);
			    	  	}
			    	  });	
			      }
			 }
		}else{
			$.msg.alert('提示信息','请选择角色');
			}
		}
	}
	function index(){
		var indexUrl = $('#indexUrl').val();
		location.replace(indexUrl);
	}
});