<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>私塾家</title>
<script th:include="sys/common/metaCSS" th:remove="tag"></script> 
<script th:include="sys/common/metaJS" th:remove="tag"></script> 
<script src="sys/js/My97DatePicker/WdatePicker.js" th:src="@{/static/js/city/city.json.js}"></script>
<script src="sys/js/My97DatePicker/WdatePicker.js" th:src="@{/static/js/city/citySetMe_V.js}"></script>
<script src="sys/js/My97DatePicker/WdatePicker.js" th:src="@{/static/js/city/Popt.js}"></script>
<script src="sys/js/My97DatePicker/WdatePicker.js" th:src="@{/static/sys/js/My97DatePicker/WdatePicker.js}"></script>
</head>
 <style type="text/css">
   body{min-width:180px;}
       .select1{
   		    width:100%;
		    height: 32px;
		    line-height: 32px;
		    border-top: solid 1px #a7b5bc;
		    border-left: solid 1px #a7b5bc;
		    border-right: solid 1px #ced9df;
		    border-bottom: solid 1px #ced9df;
		    opacity:1;
	        margin-bottom: 10px;
    	}
    	.btn{
    	  width: 100px;
    	}
     label.control-label-error {color: Red;}	
     
._citys {     background-color: whitesmoke;width: 450px; display: inline-block; border: 2px solid #eee; padding: 5px; position: relative; }
._citys span { color: #05920a; height: 15px; width: 15px; line-height: 15px; text-align: center; border-radius: 3px; position: absolute; right: 10px; top: 10px; border: 1px solid #05920a; cursor: pointer; }
._citys0 { width: 95%; height: 34px; line-height: 34px; display: inline-block; border-bottom: 2px solid #05920a; padding: 0px 5px; font-size:14px; font-weight:bold; margin-left:6px; }
._citys0 li { display: inline-block; line-height: 34px; font-size: 15px; color: #888; width: 80px; text-align: center; cursor: pointer; }
._citys1 { width: 100%; display: inline-block; padding: 10px 0; }
._citys1 a { width: 83px; height: 35px; display: inline-block; background-color: #f5f5f5; color: #666; margin-left: 6px; margin-top: 3px; line-height: 35px; text-align: center; cursor: pointer; font-size: 12px; border-radius: 5px; overflow: hidden; }
._citys1 a:hover { color: #fff; background-color: #05920a; }
.AreaS { background-color: #05920a !important; color: #fff !important; }
#school{
	margin-left: 110px;
	padding-bottom: 10px;
}  
#school span{
  	min-width: 25%;
    float: left;
    margin-bottom: 10px;
}
.joinseachform li{
	width: 50%;
	margin-right: 0px;
	padding-right: 0px;
}

.schoolseachform li{
	width: 50%;
	margin-right: 0px;
	padding-right: 0px;
}    

dtext{
	color: red;
	cursor: pointer;
}
.forminfo li label{
	text-align: right;
	padding-right: 10px
}
  
</style>
<body>
<div class="formbody">
		<ul class="forminfo">
			<input type="hidden" id="id" class="dfinput fl" name="id" th:value="${awardActivity.id}"/>
			<input type="hidden" id="status" class="dfinput fl" name="status" th:value="${awardActivity.status}"/>
			
			<li>
				<label>商户名称<b>*</b></label>
				<div class="vocation" style="margin-right: 5px; width: 345px">
					<select name="merchantId" id="merchantId" class="select1" > 
						<option th:each="item:${subMerchants}" th:selected="${item.id eq awardActivity.merchantId  ? true : false}"  th:data-portnum="${item.portNum}"  th:value="${item.id}" th:text="${item.unitName}+' ['+${item.posCode}+']'"></option>
					</select>
				</div>
			</li>
			
			<li>
				<label>端口数量：<b>*</b></label>
				<input disabled="disabled" onblur="refreshRate()" type="number" id="portNum" class="dfinput fl" name="portNum" th:value="${awardActivity.portNum!=null ? awardActivity.portNum : 0}"/>
			</li>
			
			<li>
				<label>预计人数：<b>*</b></label>
				<input onblur="refreshRate()" type="number" id="partakeNum" class="dfinput fl" name="title" th:value="${awardActivity.partakeNum!=null ? awardActivity.partakeNum : 0}"/>
			</li>

			<li>
				<label>活动主题：<b>*</b></label>
				<input type="text" id="title" class="dfinput fl" name="title" th:value="${awardActivity.title}"/>
			</li>
			
			<li>
				<label>一等奖<b>*</b></label>
				<div class="vocation" style="margin-right: 5px; width: 145px">
					<select name="level1MaterialId" id="level1MaterialId" class="select1" > 
						<option value="">选择奖品</option>
						<option th:each="item:${level1Materials}" th:selected="${item.id eq awardActivity.level1MaterialId  ? true : false}"  th:value="${item.id}" th:text="${item.name}"></option>
					</select>
				</div>
				<input onChange="refreshRate()" style="width: 95px;" type="number" id="level1Num" class="dfinput fl" name="level1Num" th:value="${awardActivity.level1Num!=null ? awardActivity.level1Num : 0}"/>
				<text>&nbsp;库存:<t id="level1_num" style="color:red">0</t>&nbsp;中奖率:<t id="level1_num_rate" style="color:red">0%</t></text>
			</li>
			
			<li>
				<label>二等奖<b></b></label>
				<div class="vocation" style="margin-right: 5px; width: 145px">
					<select name="level2MaterialId" id="level2MaterialId" class="select1" >
						<option value="">选择奖品</option> 
						<option th:each="item:${level2Materials}" th:selected="${item.id eq awardActivity.level2MaterialId  ? true : false}"  th:value="${item.id}" th:text="${item.name}"></option>
					</select>
				</div>
				<input onChange="refreshRate()" style="width: 95px;" type="number" id="level2Num" class="dfinput fl" name="level2Num" th:value="${awardActivity.level2Num!=null ? awardActivity.level2Num : 0}"/>
				<text>&nbsp;库存:<t id="level2_num" style="color:red">0</t>&nbsp;中奖率:<t id="level2_num_rate" style="color:red">0%</t></text>
			</li>
			
			
			<li>
				<label>三等奖<b></b></label>
				<div class="vocation" style="margin-right: 5px; width: 145px">
					<select name="level3MaterialId" id="level3MaterialId" class="select1" > 
						<option value="">选择奖品</option>
						<option th:each="item:${level3Materials}" th:selected="${item.id eq awardActivity.level3MaterialId  ? true : false}"  th:value="${item.id}" th:text="${item.name}"></option>
					</select>
				</div>
				<input onChange="refreshRate()" style="width: 95px;" type="number" id="level3Num" class="dfinput fl" name="level3Num" th:value="${awardActivity.level3Num!=null ? awardActivity.level3Num : 0}"/>
				<text>&nbsp;库存:<t id="level3_num" style="color:red">0</t>&nbsp;中奖率:<t id="level3_num_rate" style="color:red">0%</t></text>
			</li>
			
			<li>
				<label>活动备注：<b></b></label>
				<input type="text" id="remarks" class="dfinput fl" name="title" th:value="${awardActivity.remarks}"/>
			</li>
			
			<li style="margin-bottom:3px">
				<label>活动底图：<b></b></label>
				<input type="hidden" id="img" th:data-path="${awardActivity.img}" />
				<plug id="pluguploadid" th:include="sys/common/imgUpload"  th:inline="javascript"  data-bindelem="#img" th:data-filepaths="${awardActivity.img}"  data-type="1" data-maxnum="2"  \>
			</li>
			
			<li style="padding-top: 20px">
				<label>&nbsp;</label>
				<button type="submit" onclick="save();" class="btn">提交</button>
				<button type="button" class="btn" onclick="closeWin();">取消</button>
			</li>
			
		</ul>
			
</div>

</body>
<script type="text/javascript">
var id='[[${awardActivity.id}]]';

function save(){
	
	if($("#portNum").val()<=0){
		parent.layer.msg("端口数量不能少等于0",{icon:11});
		return
	}
	if($("#partakeNum").val()<=0){
		parent.layer.msg("预计人数不能少等于0",{icon:11});
		return
	}
	
	if($("#title").val()==''){
		parent.layer.msg("活动主题不能为空",{icon:11});
		return
	}
	
	if($("#level1MaterialId").val()=='' && $("#level2MaterialId").val()=='' && $("#level3MaterialId").val()=='' ){
		parent.layer.msg("请选择奖品",{icon:11});
		return
	}
	
	if($("#level1MaterialId").val()!=''){
		if($("#level1Num").val()<=0){
			parent.layer.msg("一等奖数量不能少等于0",{icon:11});
			return
		}
		if(parseInt($("#level1_num").html())<$("#level1Num").val()){
			parent.layer.msg("一等奖数量不能大于库存量",{icon:11});
			return
		}
	}
	
	if($("#level2MaterialId").val()!=''){
		if($("#level2Num").val()<=0){
			parent.layer.msg("二等奖数量不能少等于0",{icon:11});
			return
		}
		if(parseInt($("#level2_num").html())<$("#level2Num").val()){
			parent.layer.msg("二等奖数量不能大于库存量",{icon:11});
			return
		}
	}
	
	if($("#level3MaterialId").val()!=''){
		if($("#level3Num").val()<=0){
			parent.layer.msg("三等奖数量不能少等于0",{icon:11});
			return
		}
		if(parseInt($("#level3_num").html())<$("#level3Num").val()){
			parent.layer.msg("三等奖数量不能大于库存量",{icon:11});
			return
		}
	}
	
	parent.layer.confirm("确定保存活动信息?", {
		icon: 4, btn: ['确定','关闭'],offset: '100px' //按钮
	}, function(){
		var url="[[${sysUrl}]]/sys/award/activity/save";
		var params ={
		 	'id' : id,
            'merchantId' : $("#merchantId").val(),
            'portNum' : $("#portNum").val(),
            'partakeNum' : $("#partakeNum").val(),
            'title' : $("#title").val(),
            'level1MaterialId' : $("#level1MaterialId").val(),
            'level1Num' : $("#level1Num").val(),
            'level2MaterialId' : $("#level2MaterialId").val(),
            'level2Num' : $("#level2Num").val(),
            'level3MaterialId' : $("#level3MaterialId").val(),
            'level3Num' : $("#level3Num").val(),
            'remarks' : $("#remarks").val(),
            'img' : $("#img").val()
        };
		$.post(url,params,function(data){
			if(data.meta.success){
				parent.layer.msg("保存成功",{icon:1});
				parent.location.reload();
				parent.layer.close(parent.layer.getFrameIndex(window.name));
			}else{
				parent.layer.msg(data.meta.message,{icon:11});
			}
		});
	});
}


function refreshRate(){
	var portNum=Number($("#partakeNum").val())==0?1:Number($("#partakeNum").val());
	var level1Num=Number($("#level1Num").val());
	var level2Num=Number($("#level2Num").val());
	var level3Num=Number($("#level3Num").val());
	
	$("#level1_num_rate").html(new Number((level1Num/portNum*100)).toFixed(3)+"%");
	$("#level2_num_rate").html(new Number((level2Num/portNum*100)).toFixed(3)+"%");
	$("#level3_num_rate").html(new Number((level3Num/portNum*100)).toFixed(3)+"%");
}



$(function (){
	
	 $('#merchantId').change(function(){
      	var data= $(this).val();
    	var portnum= $(this).find("option:selected").attr('data-portnum'); 
      	$("#portNum").val(portnum);       
   
      	
      	var merchantId= $("#merchantId").val();
    	var levelMaterialId= $("#level1MaterialId").val();
      	getStock($("#level1_num"),merchantId,levelMaterialId);
      	
      	levelMaterialId= $("#level2MaterialId").val();
      	getStock($("#level2_num"),merchantId,levelMaterialId);
      	
      	levelMaterialId= $("#level3MaterialId").val();
      	getStock($("#level3_num"),merchantId,levelMaterialId);
      	
   	 });
	

	 $('#level1MaterialId').change(function(){
      	var levelMaterialId= $(this).val();
      	var merchantId= $("#merchantId").val();
      	getStock($("#level1_num"),merchantId,levelMaterialId);
   	 });
	 
	 $('#level2MaterialId').change(function(){
      	var levelMaterialId= $(this).val();
      	var merchantId= $("#merchantId").val();
      	getStock($("#level2_num"),merchantId,levelMaterialId);
   	 });
	 
	 $('#level3MaterialId').change(function(){
      	var levelMaterialId= $(this).val();
    	var merchantId= $("#merchantId").val();
      	getStock($("#level3_num"),merchantId,levelMaterialId);
   	 });
			 
	 
	 function getStock(obj,merchantId,levelMaterialId){
		 var url="[[${sysUrl}]]/sys/award/grant/stock/get";
			var params ={'levelMaterialId' : levelMaterialId,'merchantId':merchantId};
			$.post(url,params,function(data){
				if(data.meta.success){
					$(obj).html(data.data.stock);
					refreshRate();
				}else{
					parent.layer.msg(data.meta.message,{icon:11});
				}
			});
	 }
	
		
})

</script>
</html>