$(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();
			}
		})
	}
	$("#borrowerLitigationForm .occurDate").datetimepicker({
	    language:  "zh-CN",
	    weekStart: 1,
	    todayBtn:  1,
	    autoclose: 1,
	    todayHighlight: 1,
	    startView: 2,
	    minView: 2,
	    forceParse: 0,
	    format: "yyyymmdd"
	});
	$("#borrowerLitigationForm .sentenceExecDate").datetimepicker({
	    language:  "zh-CN",
	    weekStart: 1,
	    todayBtn:  1,
	    autoclose: 1,
	    todayHighlight: 1,
	    startView: 2,
	    minView: 2,
	    forceParse: 0,
	    format: "yyyymmdd"
	});
	
	function lendingMoneyValidate(t,len){
		// 正数负数和小数
		if( /^(\-|\+)?\d+(\.\d+)?$/.test(t.val().split(',').join('')) ){
			t.parent().find("span.error").removeClass('shows').text('').hide();
		}else{
			t.parent().find("span.error").addClass('shows').text('请输入正确的数字').show();
			return false;
		};
		// 小数点后最多两位
		if( t.val().indexOf('.')>0 ){
			if( String(t.val()).split('.')[1].length<=2 ){
				t.parent().find("span.error").removeClass('shows').text('').hide();
			}else{
				t.parent().find("span.error").addClass('shows').text('小数点后最多两位小数').show();
				return false;
			};
		};
		//只能输入17位数,包括小数点和小数点后两位(即使没有输入小数也要自动补两位小数),不包括负号
		var decimalLengthFlag = false,changevalue = '',value = t.val();
		if( value.indexOf('-') != -1 ){//有负号
			changevalue = value.split(',').join('').substring(1,value.split(',').join('').length);
		}else{//没有负号
			changevalue = value.split(',').join('');
		};
		if( changevalue.length<=len-3 ){
			decimalLengthFlag = true;
		}else{
			if( changevalue.length > len ){
				decimalLengthFlag = false;
			}else{
				decimalLengthFlag = true;
				if( changevalue.substring(0,14).indexOf('.') == -1 ){//第15位之前没有小数点,第15位只能是小数点
					if( changevalue.charAt(14) == '.' ){
						 decimalLengthFlag = true;
					 }else{
						 decimalLengthFlag = false;
					 }
				}
			};
		};
		if( decimalLengthFlag ){
			t.parent().find("span.error").removeClass('shows').text('').hide();
		}else{
			t.parent().find("span.error").addClass('shows').text('超出金额可输入最大位数').show();
			return false;
		};
		return true;
	};
	//填值---开始
	if( JSON.stringify(custLawsuitData[operationRowNum]) != "{}" ){//判断是否保存过值
		$("#borrowerLitigationForm .lawNo").val(custLawsuitData[operationRowNum].lawNo);//诉讼记录流水号
		$("#borrowerLitigationForm .occurDate").val(custLawsuitData[operationRowNum].occurDate);//诉讼记录日期
		$("#borrowerLitigationForm .borrowerName").val(custLawsuitData[operationRowNum].borrowerName);//借款人名称
		$("#borrowerLitigationForm .plaintiffName").val(custLawsuitData[operationRowNum].plaintiffName);//起诉人名称
		$("#borrowerLitigationForm .currency").attr("value",custLawsuitData[operationRowNum].currency);//币种
		$("#borrowerLitigationForm .sentenceExecAmount").val(commaDelimiter((custLawsuitData[operationRowNum].sentenceExecAmount).toString()));//判决执行金额
		$("#borrowerLitigationForm .sentenceExecDate").val(custLawsuitData[operationRowNum].sentenceExecDate);//判决执行日期
		$("#borrowerLitigationForm .execResult").val(custLawsuitData[operationRowNum].execResult);//执行结果
		$("#borrowerLitigationForm .accusedReason").val(custLawsuitData[operationRowNum].accusedReason);//被起诉原因
	}
	
	getDictionaryItem('capitalCurrency','币种',$("#borrowerLitigationForm .currency"));
	
	if( $(".sentenceExecAmount").val()!='' ){
		$(".sentenceExecAmount").val(commaDelimiter($(".sentenceExecAmount").val()))
	}
	
	$(".sentenceExecAmount").on("keyup",function(){
		if( lendingMoneyValidate($(this),17) ){
			$(this).val(commaDelimiter($(this).val()))
		}
	})
	
	$(".borrowerLitigationDialog .addSure").removeClass("loadingCircle");
	
	$(".borrowerLitigationDialog .addSure").click(function(){
		$("#borrowerLitigationForm").submit();
	})
	$("#borrowerLitigationForm").validate({
		promptPosition : "bottomLeft",
		submitHandler : function(form) {
			if( $(".sentenceExecAmount").val()!='' ){
				$(".sentenceExecAmount").val($(".sentenceExecAmount").val().split(',').join(''));
			}
			$(".borrowerLitigationDialog .addSure").addClass("loadingCircle");
			$(".borrowerLitigationDialog").modal("hide");
			$(".borrowerLitigationDialog").on('hidden.zui.modal', function(){
				custLawsuitData[operationRowNum].lawNo=$("#borrowerLitigationForm .lawNo").val();//诉讼记录流水号
				custLawsuitData[operationRowNum].occurDate=$("#borrowerLitigationForm .occurDate").val();//诉讼记录日期
				custLawsuitData[operationRowNum].borrowerName=$("#borrowerLitigationForm .borrowerName").val();//借款人名称
				custLawsuitData[operationRowNum].plaintiffName=$("#borrowerLitigationForm .plaintiffName").val();//起诉人名称
				custLawsuitData[operationRowNum].currency=$("#borrowerLitigationForm .currency").val();//币种
				custLawsuitData[operationRowNum].sentenceExecAmount=$("#borrowerLitigationForm .sentenceExecAmount").val().split(',').join('');//判决执行金额
				custLawsuitData[operationRowNum].sentenceExecDate=$("#borrowerLitigationForm .sentenceExecDate").val();//判决执行日期
				custLawsuitData[operationRowNum].execResult=$("#borrowerLitigationForm .execResult").val();//执行结果
				custLawsuitData[operationRowNum].accusedReason=$("#borrowerLitigationForm .accusedReason").val();//被起诉原因
				console.log(custLawsuitData);
				updateListData('custLawsuitList',custLawsuitData[operationRowNum],operationRowNum);
			});
		},
		rules : {
//			'lawNo':{
//				required:true
//			},
			'occurDate':{
				required:true
			},
			'borrowerName':{
				required:true
			},
			'plaintiffName':{
				required:true
			},
			'currency':{
				required:true
			},
			'sentenceExecAmount':{
				required:true,
				decimalLengthValid:true
			},
			'sentenceExecDate':{
				required:true
			},
			'execResult':{
				required:true
			},
			'accusedReason':{
				required:true
			}
		},
		messages : {
//			'lawNo':{
//				required:'请填写诉讼记录流水号'
//			},
			'occurDate':{
				required:'请选择诉讼记录日期'
			},
			'borrowerName':{
				required:'请填写借款人名称'
			},
			'plaintiffName':{
				required:'请填写起诉人名称'
			},
			'currency':{
				required:'请选择币种'
			},
			'sentenceExecAmount':{
				required:'请填写判决执行金额'
			},
			'sentenceExecDate':{
				required:'请选择判决执行日期'
			},
			'execResult':{
				required:'请填写执行结果'
			},
			'accusedReason':{
				required:'请填写被起诉原因'
			}
		}
	})
})