<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>
<c:set var="contextPath" value="${pageContext.request.contextPath}"></c:set>
  
<%@include file="/WEB-INF/page/header.jsp" %>
<script>changeMenu(3,3);//左边菜单active</script>
<div class="row wrapper border-bottom white-bg page-heading">
	<div class="col-lg-10">
		<h2>资料设置</h2>
		<ol class="breadcrumb">
			<li>主页</li>
			<li><a>资料设置</a></li>
			<li class="active"><strong>设置保费比例</strong></li>
		</ol>
	</div>
	<div class="col-lg-2">
		<div style="padding-top:25px">
		<c:if test="${sessionScope.SESSION_COMPANY.canApply}">
			<button class="btn btn-primary btn-lg" type="button" onclick="submitAll()">提交全部页面</button>
		</c:if>
		<c:if test="${sessionScope.SESSION_COMPANY.companyState==1}">
			认证申请中
		</c:if>
		</div>
	</div>
</div>

<div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-md-12">
				<div class="ibox float-e-margins">
					<div class="ibox-content lightBoxGallery">
					<form class="form-horizontal">
						<div class="form-group">
							<label class="col-sm-2 control-label"> 最低保费</label>
							<div class="col-sm-3 text-left">
								<input type="text" name="minFee" class="form-control" placeholder="输入最低保费">
							</div>
							<div class="col-sm-7 text-left" id="minFeeAfterDiv">
								<small>（非必填）不设置即代表无最低保费限制</small>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						<div class="form-group stylePc1" style="display:none;">
							<label class="col-sm-2 control-label"> 标的上限</label>
							<div class="col-sm-3 text-left">
								<input type="text" name="minBd" class="form-control" placeholder="输入最低标的">
							</div>
							<div class="col-sm-7 text-left">
								<small>最低保费的标的上限</small>
							</div>
						</div>
						<div class="hr-line-dashed stylePc1" style="display:none;"></div>
						
						<div class="form-group stylePcSel" style="display:none;">
							<label class="col-sm-2 control-label"> 列表样式</label>
							<div class="col-sm-3 text-left">
								<select class="form-control" id="stylePcSel">
									<option value="0">样式一(默认)</option>
									<option value="1">样式二(自定义费率)</option>
								</select>
							</div>
							<div class="col-sm-7 control-label text-left">
								<input type="hidden" name="stylePc" class="form-control" id="stylePcInputHid">
							</div>
						</div>
						<div class="hr-line-dashed stylePcSel" style="display:none;"></div>
						<!-- 
						<div class="form-group stylePc0">
							<label class="col-sm-10 control-label">
								<h3 class="text-left">设置标的区间对应的保费比例</h3>
							</label>
							<div class="col-sm-2">
								<button class="btn btn-danger" type="button" onclick="$('#mod-add').modal('toggle');">新增区间</button>
							</div>
						</div>
						<div class="hr-line-dashed stylePc0"></div>
						<div id="qjList" class="stylePc0"></div>
						
						<div class="form-group stylePc1" style="display:none;">
							<label class="col-sm-10 control-label">
								<h3 class="text-left">设置标的区间对应的保费区间</h3>
							</label>
							<div class="col-sm-2">
								<button class="btn btn-danger" type="button" onclick="$('#mod-add1').modal('toggle');">新增区间</button>
							</div>
						</div>
						<div class="hr-line-dashed stylePc1"></div>
						<div id="qjList1" class="stylePc1"></div>
						-->
						<div class="form-group">
							<label class="col-sm-12 control-label">
								<h3 class="text-left">设置标的类型对应的比例系数</h3>
							</label>
						</div>
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">第一类风险标的</label>
							<div class="col-sm-10">
								<div class="row">
									<div class="col-sm-2 text-right">
										<p class="form-control-static">房产</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S101" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">银行账户类</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S102" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">土地使用权</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S103" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
								</div>
								<br>
								<div class="row">
									<div class="col-sm-2 text-right">
										<p class="form-control-static">非上市公司股权</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S104" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">应收债权</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S105" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">采矿权</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S106" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
								</div>
								
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">第二类风险标的</label>
							<div class="col-sm-10">
								<div class="row">
									<div class="col-sm-2 text-right">
										<p class="form-control-static">车辆</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S201" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">船舶</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S202" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">不易腐朽损坏货物</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S203" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
								</div>
								<br>
								<div class="row">
									<div class="col-sm-2 text-right">
										<p class="form-control-static">机器设备</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S204" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">商标专利</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S205" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									
								</div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">第三类风险标的</label>
							<div class="col-sm-10">
								<div class="row">
									<div class="col-sm-2 text-right">
										<p class="form-control-static">上市公司股票</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S301" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">非固定收益基金账户</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S302" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-sm-2 text-right">
										<p class="form-control-static">易腐朽损坏货物</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="S303" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
								</div>
							</div>
						</div>
						
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<label class="col-sm-12 control-label">
								<h3 class="text-left">设置诉讼阶段对应的折扣</h3>
							</label>
						</div>
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<label class="col-sm-2 control-label">&nbsp;</label>
							<div class="col-sm-10">
								<div class="row">
									<div class="col-md-2 text-right">
										<p class="form-control-static">诉讼前</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="suitBefore" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
									<div class="col-md-2 text-right">
										<p class="form-control-static">诉讼中</p>
									</div>
									<div class="col-sm-2">
										<div class="input-group"><input type="text" name="suitAfter" class="form-control" placeholder="无"><span class="input-group-addon">%
                                        </span></div>
									</div>
								</div>
							</div>
						</div>
						<div class="hr-line-dashed"></div>
						<div class="form-group">
							<div class="col-sm-12 text-center">
								<button class="btn btn-primary" type="button" id="saveAll">保存本页面</button>
							</div>
						</div>
						</form>
					</div>
				</div>
			</div>
		</div>

</div>

<div id="mod-add" class="modal fade" tabindex="-1">
  <div class="modal-dialog" style="max-width: 400px;">
      <div class="modal-content">
      	<div class="modal-header">
      			<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					新增区间
				</div>
	      	</div>
        <div class="modal-body">
        <form role="form" class="form-horizontal">
        	<input type="hidden" name="paramId">
        	<div class="form-group">
        		<label class="col-sm-4 control-label">标的区间（始）</label>
				<div class="col-sm-4">
        			<input type="text"  name="add-start" class="form-control">
        		</div>
        	</div>
        	<div class="form-group">
        		<label class="col-sm-4 control-label">标的区间（终）</label>
				<div class="col-sm-4">
        			<input type="text"  name="add-end" class="form-control">
        		</div>
        	</div>
        	<div class="form-group">
        		<label class="col-sm-4 control-label">保费比率</label>
	        	<div class="col-md-4">
	        		<div class="input-group">
	        			<input type="text" name="add-rat" class="form-control">
	        			<span class="input-group-addon">%</span>
	        		</div>
	            </div>   
            </div>                      
        </form>
      	</div>
      	<div class="modal-footer">
      		<button class="btn  btn-primary" type="button" id="mod-add-submit"><strong><i class="fa fa-floppy-o bigger-160"></i>确定</strong></button>
               <button class="btn " data-dismiss="modal"><i class="fa fa-share bigger-160"></i> 取消</button>
		</div>
      </div>
  </div>
</div>

<div id="mod-add1" class="modal fade" tabindex="-1">
  <div class="modal-dialog" style="width: 600px;">
      <div class="modal-content">
      	<div class="modal-header">
      			<div class="table-header">
					<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
						<span class="white">&times;</span>
					</button>
					新增区间
				</div>
	      	</div>
        <div class="modal-body">
        <form role="form" class="form-horizontal">
        	<input type="hidden" name="companyId">
        	<div class="form-group">
        		<label class="col-sm-2 control-label no-padding">标的区间(始)</label>
				<div class="col-sm-4">
        			<input type="text"  name="add-bd-start" class="form-control">
        		</div>
        		<label class="col-sm-2 control-label no-padding">标的区间(终)</label>
				<div class="col-sm-4">
        			<input type="text"  name="add-bd-end" class="form-control">
        		</div>
        	</div>
        	<div class="form-group">
        		<label class="col-sm-2 control-label no-padding">费率1(始)</label>
				<div class="col-sm-4">
        			<div class="input-group">
	        			<input type="text" name="add-rat1-start" class="form-control">
	        			<span class="input-group-addon">%</span>
	        		</div>
        		</div>
        		<label class="col-sm-2 control-label no-padding">费率1(终)</label>
				<div class="col-sm-4">
        			<div class="input-group">
	        			<input type="text" name="add-rat1-end" class="form-control">
	        			<span class="input-group-addon">%</span>
	        		</div>
        		</div>
        	</div>
        	<div class="form-group">
        		<label class="col-sm-2 control-label no-padding">费率2(始)</label>
				<div class="col-sm-4">
        			<div class="input-group">
	        			<input type="text" name="add-rat2-start" class="form-control">
	        			<span class="input-group-addon">%</span>
	        		</div>
        		</div>
        		<label class="col-sm-2 control-label no-padding">费率2(终)</label>
				<div class="col-sm-4">
        			<div class="input-group">
	        			<input type="text" name="add-rat2-end" class="form-control">
	        			<span class="input-group-addon">%</span>
	        		</div>
        		</div>
        	</div>
        	<div class="form-group">
        		<label class="col-sm-2 control-label no-padding">费率3(始)</label>
				<div class="col-sm-4">
        			<div class="input-group">
	        			<input type="text" name="add-rat3-start" class="form-control">
	        			<span class="input-group-addon">%</span>
	        		</div>
        		</div>
        		<label class="col-sm-2 control-label no-padding">费率3(终)</label>
				<div class="col-sm-4">
        			<div class="input-group">
	        			<input type="text" name="add-rat3-end" class="form-control">
	        			<span class="input-group-addon">%</span>
	        		</div>
        		</div>
        	</div>                   
        </form>
      	</div>
      	<div class="modal-footer">
      		<button class="btn  btn-primary" type="button" id="mod-add1-submit"><strong><i class="fa fa-floppy-o bigger-160"></i>确定</strong></button>
               <button class="btn " data-dismiss="modal"><i class="fa fa-share bigger-160"></i> 取消</button>
		</div>
      </div>
  </div>
</div>

<%@include file="/WEB-INF/page/footer.jsp" %>

    
<script>
var bfDiffData = '';
var minBdData = '';
var minFeeAfterDiv0 = '<small>（非必填）不设置即代表无最低保费限制</small>';
var minFeeAfterDiv1 = '<div class="row stylePc1">'
						+ '<label class="col-sm-2 control-label"> 增加保费 </label>'
						+ '<div class="col-sm-6 text-left">'
						+ '	<input type="text" name="bfDiff" class="form-control" placeholder="输入每档增加保费">'
						+ '</div>'
						+ '<div class="col-sm-4 text-left">'
						+ '</div>'
					+ '</div>';

function checkDouble(s){
	var reg=/\d+(\.\d+)?/;
	return !reg.test(s);
}

function deleteQJ(id){
	if (confirm("确定删除吗?")) {
	$.getJSON('${contextPath}/companyParam/deleteParamQJ?id='+id,function(result){
		if(result.success){
			loadQJ();
		}
	});
	}
}

function deleteQJ1(id){
	if (confirm("确定删除吗?")) {
	$.getJSON('${contextPath}/companyParam/deleteParamQJ1?ids='+id,function(result){
		if(result.success){
			loadQJ1();
		}
	});
	}
}

//标的区间数据
function loadQJ(){
	$.getJSON('${contextPath}/companyParam/getParamQJ',function(result){
		var sec='';
		$.each(result, function(i, v){
			sec+='<div class="form-group">';
			sec+='<label class="col-sm-2 control-label">区间'+(i+1)+'</label><div class="col-sm-10"><div class="row">';
			sec+='<div class="col-sm-3"><input name="'+v.id+'-start" type="text" class="form-control" value="'+v.startValue+'"></div>';
			var end = v.endValue;
			if(end==null) end='';
			sec+='<div class="col-sm-3"><input name="'+v.id+'-end" type="text" class="form-control" value="'+end+'" placeholder="无上限"></div>';
			sec+='<div class="col-sm-2"><div class="input-group"><input name="'+v.id+'-rat" type="text" class="form-control" value="'+v.rat+'" placeholder="输入保费比例 "><span class="input-group-addon">%</span></div></div>';
			sec+='<div class="col-sm-2"><button class="btn btn-white" onclick="deleteQJ(\''+v.id+'\')" type="button">删除</button></div>';
			sec+='</div></div></div>';
        	sec+='<div class="hr-line-dashed"></div>';
		 });
		$("#qjList").html(sec);
	});
}

//标的区间数据
function loadQJ1(){
	$.getJSON('${contextPath}/companyParam/getParamQJ1',function(result){
		var html = '';
		var htmlC = '';
		var ids = '';
		var arr = null;
		$.each(result, function(i, v){ 
			arr = new Array();
			htmlC = '';
			$.each(v.rats, function(j, r){
				htmlC += '<div class="form-group">';
				htmlC += '	<label class="col-sm-2 control-label">费率区间'+(j+1)+'</label>';
				htmlC += '	<div class="col-sm-10">';
				htmlC += '		<div class="row">';
				htmlC += '			<div class="col-sm-3"><div class="input-group"><input name="'+r.id+'-rs" type="text" class="form-control" value="'+r.ratStart+'" placeholder="输入费率比例 "><span class="input-group-addon">%</span></div></div>';
				htmlC += '			<div class="col-sm-3"><div class="input-group"><input name="'+r.id+'-re" type="text" class="form-control" value="'+r.ratEnd+'" placeholder="输入费率比例 "><span class="input-group-addon">%</span></div></div>';
				htmlC += '			<div class="col-sm-6"></div>';
				htmlC += '		</div>';
				htmlC += '	</div>';
				htmlC += '</div>';
				htmlC += '<div class="hr-line-dashed"></div>';
				arr.push(r.id);
			});
			if(arr.length > 0){
				ids = arr.join();
				html += '<div class="form-group">';
				html += '	<label class="col-sm-2 control-label">标的区间'+(i+1)+'</label>';
				html += '	<div class="col-sm-10">';
				html += '		<div class="row">';
				html += '			<div class="col-sm-3"><input name="bdRatIdsS'+ids+'" type="text" class="form-control" value="'+v.bdStart+'"></div>';
				html += '			<div class="col-sm-3"><input name="bdRatIdsE'+ids+'" type="text" class="form-control" value="'+v.bdEnd+'" placeholder="无上限"></div>';
				html += '			<div class="col-sm-2"><button class="btn btn-white" onclick="deleteQJ1(\''+ids+'\')" type="button">删除</button></div>';
				html += '			<div class="col-sm-4"></div>';
				html += '		</div>';
				html += '	</div>';
				html += '</div>';
				html += '<div class="hr-line-dashed"></div>';
				html += htmlC;
			}
		 });
		$("#qjList1").html(html);
	});
}

//最小保费，标的折扣，诉讼前后折扣赋值
function loadParam(){
	$.getJSON('${contextPath}/companyParam/getParam',function(result){
		$("input[name='minFee']").val(result.minFee);  
		var v = result.suitBefore;
		if(v == 0.0){
			v = '';
		}
		$("input[name='suitBefore']").val(v);
		v = result.suitAfter;
		if(v == 0.0){
			v = '';
		}
		$("input[name='suitAfter']").val(v);
		$("input[name='paramId']").val(result.id);
		var pc = result.pc;
		var b = true;
		if(pc > 0){
			bfDiffData = result.minBd;
			minBdData = result.bfDiff;
			var stylePc = result.stylePc;
			$('#stylePcSel').val(stylePc);
			$('#stylePcInputHid').val(stylePc);
			$('.stylePcSel').show();
			if(1 == stylePc){
				b = false;
				$('.stylePc1').show();
				$('.stylePc0').hide();
				$('#minFeeAfterDiv').html(minFeeAfterDiv1);
				setBdData();
			}
			$("input[name='companyId']").val(result.cId);
			loadQJ1();
		}
		if(b){
			$('.stylePc1').hide();
		}
	});
	
	$.getJSON('${contextPath}/companyParam/getParamBD',function(result){
		$.each(result, function(i, v){
			$("input[name='"+v.bdCode+"']").val(v.discount);  
		 });
	});
	
	loadQJ();
}

function setBdData(){
	$("input[name='minBd']").val(bfDiffData);
	$("input[name='bfDiff']").val(minBdData);
}

function submitAll(){
	$.getJSON('${contextPath}/company/submitAll',function(result){
		if(result.success){
			alert('提交成功！');
			location.reload();
		}else
			alert(result.message);
	});
}

function submitModAdd1(){
	var obj = {'add-bd-start':['标的区间(始)','bds'],'add-rat1-start':['费率1(始)','r1s'],'add-rat1-end':['费率1(终)','r1e'],
			'add-rat2-start':['费率2(始)','r2s'],'add-rat2-end':['费率2(终)','r2e'],'add-rat3-start':['费率3(始)','r3s'],'add-rat3-end':['费率3(终)','r3e']};
	var data = {};
	var v = null;
	var a = null;
	for(var key in obj){
		v = $('input[name="' + key +'"]').val();
		a = obj[key];
		if(v.length < 1){
			alert(a[0] + '不能为空');
			return;
		}
		if(checkDouble(v)){
			alert(a[0] + '只能为浮点数');
			return;
		}
		data[a[1]] = v;
	}
	v = $('input[name="add-bd-end"]').val();
	if(v.length > 0 && checkDouble(v)){
		alert('标的区间(终)只能为浮点数');
		return;
	}
	if(data['r1e'] != data['r2s']){
		alert('费率1(终)必须等于费率2(始)');
		return;
	}
	if(data['r2e'] != data['r3s']){
		alert('费率2(终)必须等于费率3(始)');
		return;
	}
	a = [['费率1(始)','r1s'],['费率1(终)','r1e'],['费率2(始)','r2s'],['费率2(终)','r2e'],['费率3(始)','r3s'],['费率3(终)','r3e']];
	for(var i=0;i<3;i++){
		if(parseFloat(data[a[i*2][1]]) > parseFloat(data[a[i*2 + 1][1]])){
			alert(a[i*2][0] + '必须小于' + a[i*2 + 1][0] );
			return;
		}
	}
	
	data['bde'] = v;
	$.ajax({
		url : "${contextPath}/companyParam/addParamQJ1",
		data : data,
		type : 'POST',
		dataType : 'json',
		success : function(data) {
			if(data.success){
				loadQJ1();
				$("#mod-add1").modal("toggle");
			}else{
				alert(data.message);
			}
		},
	});
}

jQuery(function($) {
		loadParam();
		
		$('#mod-add-submit').on('click', function() {
			var startValue = $("input[name='add-start']").val();
			var endValue = $("input[name='add-end']").val();
			var rat = $("input[name='add-rat']").val();
			if(startValue==''||rat==''){
				alert('请输入标的和保费比率');
				return;
			}
	   		
			$.ajax({
				url : "${contextPath}/companyParam/addParamQJ",
				data : {
					paramId : $("input[name='paramId']").val(),
					startValue : startValue,
					endValue : endValue,
					rat : rat,
				},
				type : 'POST',
				dataType : 'json',
				success : function(data) {
					if(data.success){
						loadQJ();
						$("#mod-add").modal("toggle");
					}else{
						alert(data.message);
					}
				},
			});
		});
		
		$('#saveAll').on('click', function() {
	   		var map ={};
	   		$("input").each(function(i){
	   			map[$(this).attr("name")]=$(this).val();
	   		});
			$.ajax({
				url : "${contextPath}/companyParam/saveAll",
				data : map,
				type : 'POST',
				dataType : 'json',
				success : function(data) {
					if(data.success){
						alert("保存成功");
						location.reload();
					}else
						alert(data.message);
					
				}
			});
		});
		
		$('#stylePcSel').change(function(){
			var v = $(this).children('option:selected').val();
			var html = minFeeAfterDiv1;
			if(1 == v){
				$('.stylePc1').show();
				$('.stylePc0').hide();
			}else{
				$('.stylePc0').show();
				$('.stylePc1').hide();
				html = minFeeAfterDiv0;
			}
			$('#stylePcInputHid').val(v);
			$('#minFeeAfterDiv').html(html);
			setBdData();
		}); 
		
		$('#mod-add1-submit').on('click', function() {
			submitModAdd1();
		});
	});
</script>