define(function(require) {
	var $ = require('jquery');
	require('grid')($);
	require('sort')($);
	require('validation')($);
	require('uriTemplate')($);
	require('../../../styles/cim/jquery-ui-1.8.14.custom.css');
	require('../../../styles/cim/validation.css');
	require('../../../styles/cim/cim-grid.css'); 
	var IFA = require('util');
	var selectedAttr={};
	$(function(){
		var checkingUrl = $("#checkingUrl").val();
		$("#save").bind("click",save);
		listAttributes(selectedAttr);
		$("#addAttributes").bind("click",showAttributes);
		$("#newPayMethod").validate({
			rules:{
				name:{
					required:true,
					maxlength:50,
					checkExisting:true
				},
				value:{
					required:true,
					digits:true,
					checkExisting:true
				},
				mainType:{
					required:true
				},
				descs:{
					maxlength:200
				}
			},
			messages:{
				name:{
					required:i18ns("Entity.payMethod.required"),
					maxlength:i18ns("Entity.payMethod.maxlength50")
				},
				value:{
					required:i18ns("Entity.payMethod.required"),
					//digits:'只能为数字'
				},
				mainType:{
					required:i18ns("Entity.payMethod.select")
				},
				descs:{
					maxlength:i18ns("Entity.payMethod.maxlength200")
				}
			}
		});
		
		$.validator.addMethod("checkExisting",function(value,element,param){
			var data =$(element).attr("name")+"="+value+"&id=";
			var result = false;
			$.ajax({
				url:checkingUrl,
				data:data,
				type:'post',
				async:false,
				success:function(response){
					if(!response.existing){
						result= true;
					}
				}
			});
			return result;
		},i18ns("Entity.payMethod.beenUsed"));
	});
	
	function save(){
		if(!$("#newPayMethod").valid())
			return;
		var creationUrl = $("#creationUrl").val();
		$.msg.confirm(i18n['Page.Dialog.title'],i18n['Page.title.CreateConfirm'],function(){},{buttons:
			IFA.generateJson(i18n['ConditionMap.ok'],function(){
				$.post(creationUrl,$("#newPayMethod").serialize(),function(){
					$.msg.alert(i18n['Page.Dialog.title'],i18n['Page.title.Create.Success'],function(){},{buttons:IFA.generateJson(i18n['ConditionMap.ok'],
					function(){
						$(this).dialog('close');
						location.href=$("#indexUrl").val();
					}
					)});
				});
				$(this).dialog('close');
			},
			i18n['ConditionMap.cancel'],function(){
				$(this).dialog('close');
			}
			)});
	}
	
	function listAttributes(p){
			var queryUrl = $("#searchUrl").val();
			var grid = $("#attributeList").grid({
				url:queryUrl,
				root:"attributes",  
		  		method:'GET',
		  		isREST:true,
		  		paging:false,
		  		pageSize:50,
		  		pageTheme:'Theme1',
		  		sortable:true,
		  		secRootName:'attributes',
		  		filter:function(data){
		  		   return (p[''+data.id]?false:true)||(data.available?false:true);
		  		},
		  		fields:[
						{
						  	type:'checkbox',id:'isNeed',useSecRoot:true,render:function(){return 1;}
						 },
		  		        {
		  		        	type:'sort',id:'sort',useSecRoot:true
		  		        },
		  		        {
		  		        	type:'hidden',id:"attributeId",useSecRoot:true,render:function(data){
		  		        		return data.id;
		  		        	}
		  		        },
		  		        {
		  					name:i18n['Entity.payMethod.attribute.showName'],id:"showName",style:"width:20%"
		  				},
		  				{
		  					name:i18n['Entity.payMethod.attribute.regexs'],id:"regexs",style:"width:20%"
		  				},
		  				{
		  					name:i18n['Entity.payMethod.attribute.domType'],id:"domType",style:"width:20%",render:function(data){
		  						return data.domType==0?"INPUT":"TEXTAREA";
		  					}
		  				},
		  				{
		  					name:i18n['Entity.Operation'],style:"width:20%",operation:[{
		  						text:i18n['Entity.page.Delete'],action:function(data){
		  							delete selectedAttr[""+data.id];
		  							this.refresh();
		  						}
		  					}]
		  				}
		  				
		  			]
		  			
		  		
				
			});
	}
	
	/*
	 *显示属性列表 
	 */
	
	
	function showAttributes(){
		var url = $("#attributeSelectIndexUrl").val();
		var rv = window.showModalDialog(url,selectedAttr,"dialogWidth=800px;dialogHeight=500px");
		$.extend(selectedAttr,rv);
		listAttributes(selectedAttr);
	}
	
});