$(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();
			}
		})
	}
	$("#relationEnterpriseForm .updateDate").datetimepicker({
	    language:  "zh-CN",
	    weekStart: 1,
	    todayBtn:  1,
	    autoclose: 1,
	    todayHighlight: 1,
	    startView: 2,
	    minView: 2,
	    forceParse: 0,
	    format: "yyyymmdd"
	});
	//填值---开始
	if( JSON.stringify(affiliatedData[operationRowNum]) != "{}" ){//判断是否保存过值
		$("#relationEnterpriseForm .associationType").attr("value",affiliatedData[operationRowNum].affiliatedType);//关联类型的id
		$("#relationEnterpriseForm .affiliatedEntName").val(affiliatedData[operationRowNum].affiliatedEntName);//关联企业名称
		$("#relationEnterpriseForm .regNoType").attr("value",affiliatedData[operationRowNum].regNoType);//登记注册号类型
		$("#relationEnterpriseForm .regNo").val(affiliatedData[operationRowNum].regNo);//登记注册号码
		$("#relationEnterpriseForm .updateDate").val(affiliatedData[operationRowNum].updateDate);//更新日期
	}
	//填值---结束
	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");
			$(".relationEnterpriseDialog").modal("hide");
			$(".relationEnterpriseDialog").on('hidden.zui.modal', function(){
				affiliatedData[operationRowNum].affiliatedType=$("#relationEnterpriseForm .associationType").val();//关联类型的id
				affiliatedData[operationRowNum].affiliatedTypeCN=$("#relationEnterpriseForm .associationType").find("option:selected").text();//关联类型name
				affiliatedData[operationRowNum].affiliatedEntName=$("#relationEnterpriseForm .affiliatedEntName").val();//关联企业名称
				if( $("#relationEnterpriseForm .regNoType").val()!='' ){
					affiliatedData[operationRowNum].regNoType=$("#relationEnterpriseForm .regNoType").val();//登记注册号类型
					affiliatedData[operationRowNum].regNoTypeCN=$("#relationEnterpriseForm .regNoType").find("option:selected").text();//登记注册号类型name
				}else {
					affiliatedData[operationRowNum].regNoType='';
					affiliatedData[operationRowNum].regNoTypeCN='';
				}
				affiliatedData[operationRowNum].regNo=$("#relationEnterpriseForm .regNo").val();//证件/登记注册号码
				affiliatedData[operationRowNum].updateDate=$("#relationEnterpriseForm .updateDate").val();//更新日期
				//console.log(affiliatedData);
				updateListData('orgAffiliatedList',affiliatedData[operationRowNum],operationRowNum);
			});
		},
		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个字符'
			}
		}
	})
})