<!-- 中信保长银保单管理-新增 -->
<style>
.newTabAddUpdateBox td.enter .viewDetailWord{
	width:0.35rem;
	text-align:center;
	top: 0.06rem;
  			color: #333 !important;
  			cursor:default;
  			font-style:normal;
}
.newTabAddUpdateBox td.enter .viewDetailWord:hover{
	text-decoration:none;
}
</style>
<div class="modal fade businessDialog" id="myModal-addWarranty" data-backdrop="static">
	<div class="modal-dialog" style="width:9rem;">
		<div class="modal-header">
			<button type="button" class="close" data-dismiss="modal">
				<span aria-hidden="true" style="font-size:0.18rem">×</span><span class="sr-only">关闭</span>
			</button>
			<span class="modal-title">新增</span>
		</div>
		<form class="form-horizontal" id="exportCreditInsuranceAddForm" autocomplete="off">
		<div class="modal-content" style="max-height:4.7rem;overflow-y:auto;overflow-x:hidden;">
				 <div class="newTabAddUpdateBox" style="position:static;border:none;padding-left:0.2rem;padding-right:0.2rem;padding-bottom:0;">
					<div class="row">
						<table>
							<tr>
								<td width="15%">
									<span class="required_fileds_star">保险单号</span>：
								</td>
								<td width="35%" class="enter">
									<input type="text" class="form-control" name="insurancePolicyNo" maxlength="20" th:value="${insurancePolicyNo}">
								</td>
								<td width="15%">
									<span class="required_fileds_star">综合险额度</span>：
								</td>
								<td width="35%" class="enter">
									<input type="text" class="form-control comprehensiveAmount" name="comprehensiveAmount" style="padding-right:0.65rem;">
									<i class="viewDetailWord showCurrencyUnit">CNY</i>
								</td>
							</tr>
							<tr>
								<td width="15%">
									<span class="required_fileds_star">币种</span>：
								</td>
								<td width="35%" class="enter">
									<div class="currencyVal hide" th:each="c,cs:${currencyTypes}">
										<div th:text="${c?.name}"></div>
										<div th:text="${c?.id}"></div>
										<div th:text="${c?.code}"></div>
									</div>
									<select class="form-control currencySelect" disabled></select>
								</td>
								<td width="15%">
									<span class="required_fileds_star">最高赔偿额</span>：
								</td>
								<td width="35%" class="enter">
									<input type="text" class="form-control maxCompensationAmount" name="maxCompensationAmount" style="padding-right:0.65rem;">
									<i class="viewDetailWord showCurrencyUnit">CNY</i>
								</td>
							</tr>
							<tr>
								<td width="15%">
									<span class="required_fileds_star">综合险生效日</span>：
								</td>
								<td width="35%" class="enter">
									<input type="text" class="form-control effectiveDateAdd" name="effectiveDateAdd" readOnly>
								</td>
								<td width="15%">
									<span class="required_fileds_star">综合险期限</span>：
								</td>
								<td width="35%" class="enter">
									<input type="text" class="form-control" name="timeLimit" style="padding-right:0.65rem;" maxlength="4">
									<i class="viewDetailWord">天</i>
								</td>
							</tr>
							<tr>
								<td width="15%">
									<span class="required_fileds_star">综合险到期日</span>：
								</td>
								<td colspan="3" class="enter">
									<input type="text" class="form-control expiringDateAdd nowrite" name="expiringDateAdd" readOnly>
								</td>
								<!-- <td width="15%">
									<span class="required_fileds_star">综合险余额</span>：
								</td>
								<td width="35%" class="enter">
									<input type="text" class="form-control" style="padding-right:0.65rem;">
									<i class="viewDetailWord showCurrencyUnit">CNY</i>
								</td> -->
							</tr>
						</table>
					</div>
				</div>
			
		</div>
		<div class="modal-footer">
			<button class="btn btn-default btn-white btn-info colorbtn saveAdd" style='margin-bottom:0;'>确认</button>
			<button class="btn btn-default btn-white btn-info canclebtn" data-dismiss="modal" style='margin-bottom:0;'>取消</button>
		</div>
		</form>
	</div>
</div>
<script>
	// 保险单号有值时不可修改
	if( $('[name=insurancePolicyNo]').val()=='' ){
		$('[name=insurancePolicyNo]').removeClass('nowrite');
		$('[name=insurancePolicyNo]').prop('readOnly',false);
	}else{
		$('[name=insurancePolicyNo]').addClass('nowrite');
		$('[name=insurancePolicyNo]').prop('readOnly',true);
	};

	// 填充币种选项值
	$(".currencyVal").each(function(i,ele){
		$(".currencySelect").append('<option code="'+$(ele).find('div').eq(2).text()+'" value="'+$(ele).find('div').eq(1).text()+'">'+$(ele).find('div').eq(0).text()+'</option>')
	});
	$(".currencySelect").find('option[code=USD]').prop('selected',true);
	$(".currencySelect").change(function(){
		$(".showCurrencyUnit").text($(this).find('option:selected').attr('code'))
	});
	$(".currencySelect").change();
	
	// 初始化日期
	function initDate(selector){
		$(selector).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"
			}
		);
	};
	initDate('.effectiveDateAdd');
	
	// 金额输入添加分隔符
	$(".comprehensiveAmount,.maxCompensationAmount").keyup(function(){
		$(this).val(commaDelimiter($(this).val()))
	});
	
	// 计算综合险到期日
	function caculateExpiringEndDate(){
		if( /^\+?[1-9]\d*$/.test($('[name=timeLimit]').val()) ){//输入的期限符合规则
			if( $('.effectiveDateAdd').val()!='' && $('[name=timeLimit]').val()!='' ){
				$('.expiringDateAdd').val(caculateDeadlineDate($('.effectiveDateAdd').val(),$('[name=timeLimit]').val()));
			}else{
				$('.expiringDateAdd').val('');
			}
		}else{
			$('.expiringDateAdd').val('');
		}
	};
	$('[name=effectiveDateAdd]').change(function(){
		caculateExpiringEndDate();
		if( $(this).val()!='' ){
			$(this).removeClass('error');
			$("#effectiveDateAdd-error").hide();
		}
	});
	$('[name=timeLimit]').keyup(function(){
		caculateExpiringEndDate()
	});
	
	// 确认新增
	$(".saveAdd").click(function(){
		$("#exportCreditInsuranceAddForm").validate({
			 promptPosition : "bottomLeft",
			 submitHandler : function(form) {
				 // 综合险额度大于等于最高赔偿额
				 if( parseFloat($("[name=comprehensiveAmount]").val().split(',').join('')) < parseFloat($("[name=maxCompensationAmount]").val().split(',').join('')) ){
					new $.zui.Messager('最高赔偿额不能大于综合险额度, 请修改后重新保存', {
					    type: 'danger'
					}).show();
					return; 
				 };
				$.ajax({
					url:'/inkassonPolicy/eci/doAdd',
					type:'post',
					data:{
						'insurancePolicyNo':$('[name=insurancePolicyNo]').val(),
						'comprehensiveAmount':$("[name=comprehensiveAmount]").val().split(',').join(''),
						'currencyType.id':$(".currencySelect option:selected").attr('value'),
						'maxCompensationAmount':$("[name=maxCompensationAmount]").val().split(',').join(''),
						'effectiveDate':$('[name=effectiveDateAdd]').val(),
						'timeLimit':$('[name=timeLimit]').val(),
						'expiringDate':$('[name=expiringDateAdd]').val()
					},
					success:function(data){
						if( data.success ){
							new $.zui.Messager('新增成功', {
							    type: 'success'
							}).show();
							$("#myModal-addWarranty").modal('hide')
							$('#myModal-addWarranty').on('hidden.zui.modal', function() {
								$(".yk_rightcontent").load('/inkassonPolicy/list?ename=exportCreditInsuranceCorp')
							})
						}else{
							new $.zui.Messager('新增失败:'+data.msg, {
							    type: 'danger'
							}).show();
						}
					},
					error:function(status){
						new $.zui.Messager('新增时出错:'+status, {
						    type: 'danger'
						}).show();
					}
				})
			 }, 
			rules:{
				'insurancePolicyNo':{
					required:true,
					passwordFormValid:true,
					maxlength:20
				},
				'effectiveDateAdd':{
					required:true
				},
				"timeLimit":{
					required:true,
					numberValid2:true
				},
				'comprehensiveAmount':{
					required : true,
					decimalLengthValid:true,
					decimalNumberValid2:true,
					decimalPointValid:true
				},
				'maxCompensationAmount':{
					required : true,
					decimalLengthValid:true,
					decimalNumberValid2:true,
					decimalPointValid:true
				}
			},
			messages : {
				'insurancePolicyNo':{
					required:'请输入保险单号',
					maxlength:'最多可输入20个字符'
				},
				'effectiveDateAdd':{
					required:'请输入综合险生效日'
				},
				"timeLimit":{
					required:'请输入综合险期限'
				},
				'comprehensiveAmount':{
					required : '请输入综合险额度'
				},
				'maxCompensationAmount':{
					required : '请输入最高赔偿额'
				}
			}
		}); 
	});
	
</script>