$(function(){
	function getDictionaryItem(paramName,name,ele){
		$.ajax({
			type:"get",
			url:'/orgbaseInfo/doFindDictCode',
			data:{
				'paramName':paramName
			},
			success:function(data){
//				console.log(data);
				if( data ){
					ele.empty().append('<option value="">请选择</option>');
					for(var i in data){
						ele.append('<option value="'+i+'">'+data[i]+'</option>');
					}
					if( ele.attr("value")!='' ){//有值
						ele.find("option").each(function(){
							if( $(this).val()==ele.attr("value") ){
								$(this).prop("selected",true);
							}
						})
					}
				}
			},
			error:function(status){
				new $.zui.Messager(name+'数据获取失败', {
					type : 'danger',
					time : 1500
				}).show();
			}
		})
	}
	getDictionaryItem('affiliatedType','关联类型',$("#relationEnterpriseForm .associationType"));
	getDictionaryItem('regNoType','登记注册号类型',$("#relationEnterpriseForm .regNoType"));
	
	$(".relationEnterpriseDialog .addSure").removeClass("loadingCircle");
	
	$(".relationEnterpriseDialog .addSure").click(function(){
		$("#relationEnterpriseForm").submit();
	})
	$("#relationEnterpriseForm").validate({
		promptPosition : "bottomLeft",
		submitHandler : function(form) {
			$(".relationEnterpriseDialog .addSure").addClass("loadingCircle");
			
			$(form).ajaxSubmit({
				success : function(e) {
					if (e && e.success == true) {
						new $.zui.Messager('保存成功', {
							type : 'success',
							time : 1500
						}).show();
						$(".relationEnterpriseDialog .addSure").removeClass("loadingCircle");
						$(".relationEnterpriseDialog").modal("hide");
						$(".relationEnterpriseDialog").on('hidden.zui.modal', function(){
							//刷新表格
							$('#relationEnterpriseList').trigger('reloadGrid');
						});
					}else {
						new $.zui.Messager('保存失败:'+e.msg, {
							type : 'danger',
							time : 1500
						}).show();
						$(".relationEnterpriseDialog .addSure").removeClass("loadingCircle");
					}
				}
			})
		},
		rules : {
			'affiliatedType':{
				required:true
			},
			'affiliatedEntName':{
				required:true,
				minlength:2,
				maxlength:45
			},
			'regNoType':{
				required:true
			},
			'regNo':{
				required:true,
				maxlength:20
			}
		},
		messages : {
			'affiliatedType':{
				required:'请选择关联类型'
			},
			'affiliatedEntName':{
				required:'请填写关联企业名称',
				minlength:'最少填写2个字符',
				maxlength:'最多填写45个字符'
			},
			'regNoType':{
				required:'请选择登记注册号类型'
			},
			'regNo':{
				required:'请填写登记注册号码',
				maxlength:'最多填写20个字符'
			}
		}
	})
})