$(function(){
//	持股比例限制改为0-100的整数
	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>');
					}
					var test='';
					if( ele.attr("value")!='' ){//有值
						ele.find("option").each(function(){
							if( $(this).val()==ele.attr("value") ){
								test=$(this).text();
								$(this).prop("selected",true);
							}
						})
						if( name=='股东类型' ){
							if( test=='机构' ){
								$(".stockHolderRelevance").find(".required_fileds_star").eq(0).text('登记注册号类型');
								$(".stockHolderRelevance").find(".required_fileds_star").eq(1).text('登记注册号码');
								getDictionaryItem('regNoType','登记注册号类型',$("#importantShareHolderForm .documentType"));
							}else if( test=='自然人' ){
								$(".stockHolderRelevance").find(".required_fileds_star").eq(0).text('证件类型');
								$(".stockHolderRelevance").find(".required_fileds_star").eq(1).text('证件号码');
								getDictionaryItem('certType','证件类型',$("#importantShareHolderForm .documentType"));
							}
						}
					}
				}
			},
			error:function(status){
				new $.zui.Messager(name+'数据获取失败', {
					type : 'danger',
					time : 1500
				}).show();
			}
		})
	}
	
	getDictionaryItem('stockHolderType','股东类型',$("#importantShareHolderForm .stockHolderType"));
	
	$("#importantShareHolderForm").delegate(".stockHolderType","change",function(){
		$("#importantShareHolderForm .documentType").val('');
		$("#importantShareHolderForm .identificationNumber").val('');
		if( $(this).find("option:selected").text()=='机构' ){
			$(".stockHolderRelevance").find(".required_fileds_star").eq(0).text('登记注册号类型');
			$(".stockHolderRelevance").find(".required_fileds_star").eq(1).text('登记注册号码');
			getDictionaryItem('regNoType','登记注册号类型',$("#importantShareHolderForm .documentType"));
		}else if( $(this).find("option:selected").text()=='自然人' ){
			$(".stockHolderRelevance").find(".required_fileds_star").eq(0).text('证件类型');
			$(".stockHolderRelevance").find(".required_fileds_star").eq(1).text('证件号码');
			
			getDictionaryItem('certType','证件类型',$("#importantShareHolderForm .documentType"));
		}
	})
	
	
	$("#importantShareHolderForm").delegate(".stockHoldingRatio","keyup",function(){
		if( $(this).val() ){
			if( /^[0-9]*$/.test($(this).val()) ){
				if( $(this).val()*1>100 ){
					$(this).parent().find("span.error").addClass("shows").text("请输入大于等于0小于等于100的整数").show();
				}else {
					$(this).parent().find("span.error").removeClass("shows").hide();
				}
			}else {
				$(this).parent().find("span.error").addClass("shows").text("请输入数字").show();
			}
		}else {
			$(this).parent().find("span.error").removeClass("shows").hide();
		}
	})
	
	if( $(".stockHoldingRatio").val() ){
		$(".stockHoldingRatio").val(parseInt($(".stockHoldingRatio").val()))
	}
	
	$(".importantShareHolderDialog .addSure").removeClass("loadingCircle");
	
	$(".importantShareHolderDialog .addSure").click(function(){
		$("#importantShareHolderForm").submit();
	})
	$("#importantShareHolderForm").validate({
		promptPosition : "bottomLeft",
		submitHandler : function(form) {
			if( !$("#importantShareHolderForm").find("span.error").hasClass("shows") ){
				$(".importantShareHolderDialog .addSure").addClass("loadingCircle");
				$(".hiddenStockHoldingRatio").val( ($(".stockHoldingRatio").val()/100).toFixed(4) );
				$(form).ajaxSubmit({
					success : function(e) {
						if (e && e.success == true) {
							new $.zui.Messager('保存成功', {
								type : 'success',
								time : 1500
							}).show();
							$(".importantShareHolderDialog .addSure").removeClass("loadingCircle");
							$(".importantShareHolderDialog").modal("hide");
							$(".importantShareHolderDialog").on('hidden.zui.modal', function(){
								//刷新表格
								$('#importantShareHolderList').trigger('reloadGrid');
							});
						}else {
							new $.zui.Messager('保存失败:'+e.msg, {
								type : 'danger',
								time : 1500
							}).show();
							$(".importantShareHolderDialog .addSure").removeClass("loadingCircle");
						}
					}
				})
			}
		},
		rules : {
			'stockHolderType':{
				required:true
			},
			'stockHolderName':{
				required:true,
				minlength:2,
				maxlength:45
			},
			'certType':{//暂时先按必填处理
				required:true
			},
			'certNo':{
				required:true,
				minlength:1,
				maxlength:20
			},
			'stockHoldingRatioStr':{
				required:true
			}
		},
		messages : {
			'stockHolderType':{
				required:'请选择股东类型'
			},
			'stockHolderName':{
				required:'请填写股东名称',
				minlength:'最少需输入2个字符',
				maxlength:'最多只能输入45个字符'
			},
			'certType':{//暂时先按必填处理
				required:'请选择证件/登记注册号类型'
			},
			'certNo':{
				required:'请填写证件/登记注册号码',
				minlength:'最少需输入1个字符',
				maxlength:'最多只能输入20个字符'
			},
			'stockHoldingRatioStr':{
				required:'请填写持股比例'
			}
		}
	})
})