$(function(){
	// 选择进口方
	var allcompanyli = $("#warrantyAddForm .allcompany li").clone();
	$("#warrantyAddForm #companyName").click(function(){
		$("#warrantyAddForm .entCustomerListBox ul").empty().append(allcompanyli);
		$("#warrantyAddForm .entCustomerListBox").show();
		$("#warrantyAddForm .entCustomerListBox input").val('').focus();
		return false;
	});
	$("body").on("click",function(){
		$("#warrantyAddForm .entCustomerListBox").hide();
	});
	$("#warrantyAddForm .entCustomerListBox input").click(function(){
		return false;
	});
	$("#warrantyAddForm .entCustomerListBox input").keyup(function(){
		$("#warrantyAddForm .entCustomerListBox ul").empty();
		if( $(this).val()=='' ){
			$("#warrantyAddForm .entCustomerListBox ul").append(allcompanyli);
		}else{
			for( var i=0;i<$("#warrantyAddForm .allcompany li").length;i++ ){
				if( $("#warrantyAddForm .allcompany li").eq(i).text().indexOf($(this).val())>=0 ){
					$("#warrantyAddForm .entCustomerListBox ul").append($("#warrantyAddForm .allcompany li").eq(i).clone());
				};
			};
			i = null;
			if( $("#warrantyAddForm .entCustomerListBox ul").find('li').length<=0 ){
				$("#warrantyAddForm .entCustomerListBox ul").append('<li class="notcompanyli" style="background-color:#f0f0f3;">未找到相关选项! "'+$(this).val()+'"</li>')
			}
		};
	});
	$("#warrantyAddForm .entCustomerListBox").on("click","li.notcompanyli",function(){
		return false;
	})
	$("#warrantyAddForm .entCustomerListBox").on("click","li.companyli",function(){
		$("#warrantyAddForm #companyName").val($(this).text());
		$("#warrantyAddForm #companyId").val($(this).attr('value'));
		$("#warrantyAddForm .companyNum").val($(this).attr("num"));
		$("#warrantyAddForm .countryName").val($(this).attr("differentCountry"));
		$("#warrantyAddForm .countryCode").val($(this).attr("countryCode"));
		$("#warrantyAddForm .entCustomerListBox").hide();
		
		return false;
	});
	// 选择进口方结束
	
	// 选择币种
	$("#warrantyAddForm option[code=USD]").prop('selected',true);
	$('#warrantyAddForm .showCurrencyUnit').text('USD');
	$("#warrantyAddForm").find(".currencyType option").each(function(){
		if( $(this).attr('code') != "USD" ){
			$(this).remove();
		}
	});
	
	//日期初始化
	$("#warrantyAddForm").find(".effectiveDate").datetimepicker({
		weekStart: 1,
	    autoclose: 1,
	    todayHighlight: 0,
	    startView: 2,
	    minView:2,
	    forceParse: 0,
	    startDate:'1970-01-01',
	    endDate:'2116-12-31',
	    format: "yyyy-mm-dd"
	});
	
	//自动计算限额到期日
	function caculateCreditExpirationDate(){
		var testday = /^[1-9]\d*$/;
		if( testday.test($("#warrantyAddForm .timeLimit").val())　 ){//输入的期限符合规则
			if( $("#warrantyAddForm .effectiveDate").val()!='' && $("#warrantyAddForm .timeLimit").val()!='' ){
				$("#warrantyAddForm .expiringDate").val(caculateDeadlineDate($("#warrantyAddForm .effectiveDate").val(),$("#warrantyAddForm .timeLimit").val()))
			}else{
				$("#warrantyAddForm .expiringDate").val('');
			}
		}else{
			$("#warrantyAddForm .expiringDate").val('');
		}
	};
	
	$("#warrantyAddForm .timeLimit").keyup(function(){
		caculateCreditExpirationDate();
	});
	$("#warrantyAddForm .effectiveDate").change(function(){
		caculateCreditExpirationDate();
	});
	
	//限额余额自动计算    限额余额=限额额度-实占限额额度
	$("#warrantyAddForm .limitAmount").keyup(function(){
		$(this).val(commaDelimiter($(this).val()));
		$('#warrantyAddForm .limitAmountTrue').val($(this).val().split(',').join(''));
		$('#warrantyAddForm .limitAmountTrue').keyup();
		$("#warrantyAddForm .limitBalance").val($(this).val());
		$('#warrantyAddForm .limitBalanceTrue').val($(this).val().split(',').join(''))
	});
	
	// 限额生效日有值时去掉验证效果
	$("#warrantyAddForm .effectiveDate").change(function(){
		if( $("#warrantyAddForm .effectiveDate").val()!='' ){
			$("#warrantyAddForm .effectiveDate").removeClass('error');
			$("#warrantyAddForm #effectiveDate-error").hide();
		}
	});
	
	$("#myModal-addImporter").find(".colorbtn").click(function(){
		$("#warrantyAddForm").submit();
	})
	
	$("#warrantyAddForm").validate({
		promptPosition : "bottomLeft",
		submitHandler : function(form) {
			if( $('#warrantyAddForm #companyName').val()=='' ){
				new $.zui.Messager('请选择进口方', {
					type : 'danger'
				}).show();
				return;
			};
			$(form).ajaxSubmit({
				success : function(e) {
					if (e && e.success == true) {
						new $.zui.Messager('保存成功', {
							type : 'success'
						}).show();
						$("#myModal-addImporter").modal('hide');
						$('#myModal-addImporter').off('hidden.zui.modal').on('hidden.zui.modal', function() {
							$("#customerList2").trigger('reloadGrid');
						});
					} else {
						new $.zui.Messager('保存失败:'+e.msg, {
							type : 'danger'
						}).show();
					}
				}
			});
		},
		rules : {
			'foreignEnt.id' : {//进口方
				required : true
			},
			'limitAmount':{//限额额度
				required : true,
				positiveNumberValid:true,
				decimalPointValid:true,
				decimalLengthValid:true
			},
			'currencyType.id':{//币种
				required : true
			},
			'effectiveDate':{//限额生效日
				required : true
			},
			'timeLimit':{//限额闲置期
				required : true,
				jobNumberValid:true,
				maxlength:"4"
			},
//			'expiringDate':{//限额到期日
//				required : true
//			},
			'oaBilling':{//OA账期
				required : true,
				jobNumberValid:true,
				maxlength:"4"
			}
		},
		messages : {
			'foreignEnt.id' : {//进口方
				required : "进口方不能为空",
			},
			'limitAmount':{//限额额度
				required : '限额额度不能为空'
			},
			'currencyType.id':{//币种
				required : '币种不能为空'
			},
			'effectiveDate':{//限额生效日
				required:'限额生效日不能为空'
			},
			'timeLimit':{//限额闲置期
				required:'限额闲置期不能为空',
				maxlength:'最大输入4位数'
			},
//			'expiringDate':{//限额到期日
//				required : '限额到期日不能为空'
//			},
			'oaBilling':{//OA账期
				required : 'OA账期不能为空',
				maxlength:'最大输入4位数'
			}
		}
	});
	jQuery.validator.addMethod("positiveNumberValid", function(value, element) {
		//不允许负数的-->/^[0-9]+([.]{1}[0-9]+){0,1}$/
	    return this.optional(element) || (/^[0-9]+([.]{1}[0-9]+){0,1}$/.test(value.split(',').join('')));
	}, "请输入正确的数字");
	
	
})