$(function(){
	function getDictionaryItem(paramName,name,ele,url){
		$.ajax({
			type:"get",
			url:url+paramName,
			data:{

			},
			success:function(data){
//				console.log(data);
				if( data ){
					ele.empty();
					for (var i=0; i<data.data.length; i++ ) {
						ele.append('<option value="' + data.data[i].id + '">' + data.data[i].name + '</option>')
					};
					i=null;
					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();
			}
		})
	}

	$("[name=entOthType]").change(function () {
		if($(this).val() !== '') {
			getDictionaryItem($('[name=entOthType] option:selected').attr('code'),'身份类型',$("#seniorExecutiveInfoForm .type"),'/creditEntBusiness/relative/doFindType?code=');
			getDictionaryItem($('[name=entOthType] option:selected').attr('code'),'关联人证件类型',$("#seniorExecutiveInfoForm .certType"),' /creditEntBusiness/relative/doFindCertType?code=');
			//出资人
			if($('[name=entOthType] option:selected').text() == '主要出资人') {
				$(".flagTr").show();
				$(".relFlagTr").hide();
			}else if($('[name=entOthType] option:selected').text() == '企业关联关系') {
				$(".relFlagTr").show();
				$(".flagTr").hide();
			}else {
				$(".relFlagTr").hide();
				$(".flagTr").hide();
			}
		}else {
			$("#seniorExecutiveInfoForm .type").empty();
			$("#seniorExecutiveInfoForm .certType").empty();
			$(".relFlagTr").hide();
			$(".flagTr").hide();
		}
	})
	$("[name=entOthType]").change()
	
	$(".seniorExecutiveInfoDialog .addSure").removeClass("loadingCircle");

	$("[name=flag]").keyup(function () {
		if($(this).val() < 5) {
			$(".flagError").text('请填写出资比例大于5%的出资人。').addClass('shows').show();
		}else {
			$(".flagError").removeClass('shows').hide();
		}
	})



	function addUpdateSubmit(e) {
		$("#seniorExecutiveInfoForm").validate({
			promptPosition : "bottomLeft",
			submitHandler : function(form) {
				if($('[name=entOthType] option:selected').text() == '主要出资人') {
					if($('[name=flag]').val() == '') {
						$(".flagError").text('请输入出资人比例。').addClass('shows').show();
						return false;
					}
				}

				var postdata = {},url='';
				postdata['entOthType.id'] = $('[name=entOthType] option:selected').val();
				postdata['type.id'] = $('[name=type] option:selected').val();
				postdata['idType.id'] = $('[name=idType] option:selected').val();
				postdata['name'] = $('[name=name]').val();
				postdata['certType.id'] = $('[name=certType] option:selected').val();
				postdata['certId'] = $('[name=certId]').val();

				if($('[name=entOthType] option:selected').text() == '主要出资人') {
					postdata['flag'] = parseFloat($('[name=flag]').val()).toFixed(2);
				}else if($('[name=entOthType] option:selected').text() == '企业关联关系') {
					postdata['flag'] = $('[name=relFlag]:checked').val();
				}
				if($('[name=addUpdate]').attr('id') == 'add') {
					url = '/creditEntBusiness/relative/doAddCreditEntRelative'
					postdata['entBusInfo.id'] = $('[name=custId]').val();
				}else {
					url = '/creditEntBusiness/relative/doUpdateCreditEntRelative'
					postdata['id'] = $('[name=id]').val();
					postdata['entBusInfo.id'] = $('[name=custId]').val();
				}


				$(".seniorExecutiveInfoDialog .addSure").addClass("loadingCircle");
				$.ajax({
					url: url,
					type: 'post',
					data: postdata,
					success : function(e) {
						if (e && e.success == true) {
							new $.zui.Messager('保存成功', {
								type : 'success',
								time : 1500
							}).show();
							$(".seniorExecutiveInfoDialog .addSure").removeClass("loadingCircle");
							$(".seniorExecutiveInfoDialog").modal("hide");
							$(".seniorExecutiveInfoDialog").on('hidden.zui.modal', function(){
								//刷新表格
								$('#enterpriseSeniorExecutiveList').trigger('reloadGrid');
							});
						}else {
							new $.zui.Messager('保存失败:'+e.msg, {
								type : 'danger',
								time : 1500
							}).show();
							$(".seniorExecutiveInfoDialog .addSure").removeClass("loadingCircle");
						}
					}
				})
			},
			rules : {
				'entOthType':{
					required:true
				},
				'type':{
					required:true,
				},
				'idType':{
					required:true
				},
				'name':{
					required:true,
				},
				'certType':{
					required:true,
				},
				'certId':{
					required:true,
				},

			},
			messages : {
				'entOthType' : {
					required : "请选择关联关系"
				},
				'type':{
					required:'请选择身份类型',
				},
				'idType':{
					required:'请选择身份类别'
				},
				'name':{
					required:'请填写关联人名称',
				},
				'certType':{
					required:'请选择关联人证件类型',
				},
				'certId':{
					required:'请填写关联人证件号码'
				},

			}
		})
	}
	addUpdateSubmit()

	$(".seniorExecutiveInfoDialog .addSure").click(function(){
		addUpdateSubmit();
	})
})