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();
		$("#addAttributes").bind("click",showAttributes);
		resolveAttributesJson();
		listAttributes(selectedAttr);
		$("#update").bind("click",save);
		$("#editPayMethod").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:i18ns("Entity.payMethod.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="+$("#id").val();
			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(!$("#editPayMethod").valid())
			return;
		var updatingUrl = $("#updatingUrl").val();
		$.msg.confirm(i18n['Page.Dialog.title'],i18n['Page.title.updateSure'],function(){},{buttons:
			IFA.generateJson(i18n['ConditionMap.ok'],function(){
				$.post(updatingUrl,$("#editPayMethod").serialize()+"&_method=put",function(){
					$.msg.alert(i18n['Page.Dialog.title'],i18n['Entity.Operation.Save.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 urlStr = new $.Template($("#searchUrl").val());
		var param = {id:$("#id").val()};
		var queryUrl = urlStr.expand(param);
		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(data){return 1;},setChecked:function(data){
					  		return data.isNeed==1?true:false;
					  	}
					 },
	  		        {
	  		        	type:'sort',id:'sort',useSecRoot:true
	  		        },
	  		        {
	  		        	type:'hidden',id:"attributeId",useSecRoot:true,render:function(data){
	  		        		return data.id;
	  		        	}
	  		        },
	  		      {
	  		        	type:'hidden',id:"id",useSecRoot:true,render:function(data){
	  		        		return data.extraId;
	  		        	}
	  		        },
	  		        {
	  					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();
	  						}
	  					}]
	  				}
	  				
	  			],callback:function(){
	  				var allChecked = true;
	  				$("#attributeList").find("td :checkbox").each(function(index,item){
	  					if(!item.checked){
	  						allChecked = false;
	  					}
	  				});
	  				if(allChecked){
	  					$("#attributeList").find("th :checkbox").attr("checked",true);
	  				}
	  			}
	  			
	  		
			
		});
}

/*
 *显示属性列表 
 */


function showAttributes(){
	var url = $("#attributeSelectIndexUrl").val();
	var rv = window.showModalDialog(url,selectedAttr,"dialogWidth=800px;dialogHeight=500px");
	$.extend(selectedAttr,rv);
	listAttributes(selectedAttr);
}

function resolveAttributesJson(){
	var str = $("#attributesJson").val();
	var pattern = /\"attributeId\":(\d+)/g;
	var rs ;
	while(rs =pattern.exec(str) ){
		selectedAttr[rs[1]] = rs[1];
	}
}
	
});