<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>
<link href="sys/css/jquery.searchableSelect.css" th:href="@{/static/sys/css/jquery.searchableSelect.css}" rel="stylesheet"/>
<script src="js/layer/layer.js" th:src="@{/static/sys/js/jquery.searchableSelect.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
}
.searchable-select{
	width: 345px;
    margin-bottom: 13px;
}
  
</style>
<body>
<div class="formbody">
		<ul class="forminfo">
			<li>
				<label>商户名称<b>*</b></label>
				<div class="vocation" style="margin-right: 5px; width: 345px">
					<select name="toMerchantId" id="toMerchantId" class="select1" > 
						<option th:each="item:${subMerchants}"  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" type="number" id="portNum" class="dfinput fl" name="portNum" value="0"/>
			</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:value="${item.id}" th:text="${item.name}"></option>
					</select>
				</div>
				<input onblur='grantRate($("#level1Num"),$("#level1_num_rate"));' style="width: 95px;" type="number" id="level1Num" class="dfinput fl" name="level1Num" value="0" />
				<text>&nbsp;库存:<t id="level1_num" style="color:red">0</t>&nbsp;&nbsp;比例:<t id="level1_num_rate" data-val="0.005" data-numval="1"  style="color:red">200:1</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:value="${item.id}" th:text="${item.name}"></option>
					</select>
				</div>
				<input onblur='grantRate($("#level2Num"),$("#level2_num_rate"));' style="width: 95px;" type="number" id="level2Num" class="dfinput fl" name="level2Num" value="0" />
				<text>&nbsp;库存:<t id="level2_num" style="color:red">0</t>&nbsp;&nbsp;比例:<t id="level2_num_rate" data-val="0.015" data-numval="3"  style="color:red">200:3</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:value="${item.id}" th:text="${item.name}"></option>
					</select>
				</div>
				<input onblur='grantRate($("#level3Num"),$("#level3_num_rate"));' style="width: 95px;" type="number" id="level3Num" class="dfinput fl" name="level3Num" value="0" />
				<text>&nbsp;库存:<t id="level3_num" style="color:red">0</t>&nbsp;&nbsp;比例:<t id="level3_num_rate" data-val="0.075"  data-numval="15" style="color:red">200:15</t></text>
			</li>
		
			<li>
				<label>钉钉单号：<b></b></label>
				<input type="text" id="dingNumber" class="dfinput fl" name="dingNumber" />
			</li>
			
			<li th:if="${curMerchant!=null && 'shadmin' ne curMerchant.posCode}">
				<label>物流单号：	<b></b></label>
				<input type="text" id="emsNumber" class="dfinput fl" name="emsNumber" />
			</li>
			
			<li th:if="${curMerchant!=null && 'shadmin' ne curMerchant.posCode}">
				<label>收货人：<b></b></label>
				<input type="text" id="people" class="dfinput fl" name="people" />
			</li>
			
			<li th:if="${curMerchant!=null && 'shadmin' ne curMerchant.posCode}">
				<label>联系电话：<b></b></label>
				<input type="text" id="telephone" class="dfinput fl" name="telephone" />
			</li>
			
			<li th:if="${curMerchant!=null && 'shadmin' ne curMerchant.posCode}">
				<label>配送地址：<b></b></label>
				<input type="text" id="address" class="dfinput fl" name="address" />
			</li>
			
			<li th:if="${curMerchant!=null && 'shadmin' ne curMerchant.posCode}">
				<label>配送备注：<b></b></label>
				<input type="text" id="remarks" class="dfinput fl" name="remarks" />
			</li>
			
			<li style="margin-bottom:3px" th:if="${curMerchant!=null && 'shadmin' ne curMerchant.posCode}">
				<label>快递单号：<b></b></label>
				<input type="hidden" id="img" data-path="" />
				<plug id="pluguploadid" th:include="sys/common/imgUpload"  th:inline="javascript"  data-bindelem="#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 shadmin="[[${curMerchant.posCode}]]";


// $(function(){
// 	$('#toMerchantId').searchableSelect();
// });


function save(){
	
	if(shadmin=='shadmin' && $("#dingNumber").val()==''){
		parent.layer.msg("钉钉单号不能为空",{icon:11});
		return
	}
	if($("#portNum").val()<=0){
		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(!grantRate($("#level1Num"),$("#level1_num_rate"))){
			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(!grantRate($("#level2Num"),$("#level2_num_rate"))){
			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
		}
		if(!grantRate($("#level3Num"),$("#level3_num_rate"))){
			return
		}
	}
	
	parent.layer.confirm("确定配送奖品?", {
		icon: 4, btn: ['确定','关闭'],offset: '100px' //按钮
	}, function(){
		var url="[[${sysUrl}]]/sys/award/grant/save";
		var params ={
            'toMerchantId' : $("#toMerchantId").val(),
            'level1MaterialId' : $("#level1MaterialId").val(),
            'level2MaterialId' : $("#level2MaterialId").val(),
            'level3MaterialId' : $("#level3MaterialId").val(),
            'level1Num' : $("#level1Num").val(),
            'level2Num' : $("#level2Num").val(),
            'level3Num' : $("#level3Num").val(),
            'remarks' : $("#remarks").val(),
            
            'dingNumber' : $("#dingNumber").val(),
            'emsNumber' : $("#emsNumber").val(),
            'people' : $("#people").val(),
            'telephone' : $("#telephone").val(),
            'img' : $("#img").val(),
            'address':$("#address").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 merchantInfo(){
	var id=$('#toMerchantId').val();
	var url="[[${sysUrl}]]/sys/award/merchant/info";
	var params ={'id' :id};
	$.post(url,params,function(data){
		if(data.meta.success){
			$("#portNum").val(data.data.merchant.portNum);
			$("#people").val(data.data.merchant.deputyName);
			$("#telephone").val(data.data.merchant.deputyPhone);
			$("#address").val(data.data.merchant.address);
			
			if(data.data.merchant.portNum<=0){
				parent.layer.msg("此商户端口数不足，无法为其配送奖品。",{icon:11});
			}
		}else{
			parent.layer.msg(data.meta.message,{icon:11});
		}
	});
}

//level1Num level1_num_rate  grantRate($("#level1Num"),$("#level1_num_rate"));
function grantRate(level,rate){
	if($("#portNum").val()<=0){
		parent.layer.msg("此商户端口数不足，无法为其配送奖品。",{icon:11});
		return false;
	}
  	var rateVal=$(rate).data("val");
  	var numVal=$(rate).data("numval");
  	var currate=new Number($(level).val()).toFixed(3)/new Number($("#portNum").val()).toFixed(3);
  	if(currate>rateVal){
  		parent.layer.msg("端口配送比例为："+$(rate).html()+"</br>配送数量上限为："+new Number($("#portNum").val()/200*numVal).toFixed(0),{icon:11});
  		return false;
  	}
  	return true;
}



$(function (){
	
	 merchantInfo();
	
	 $('#toMerchantId').change(function(){
	      merchantInfo();  	
	 });
	
	 $('#level1MaterialId').change(function(){
      	var levelMaterialId= $(this).val();
      	getStock($("#level1_num"),levelMaterialId);
      	
      	grantRate($("#level1Num"),$("#level1_num_rate"));
      	
   	 });
	 
	 
	 $('#level2MaterialId').change(function(){
      	var levelMaterialId= $(this).val();
      	getStock($("#level2_num"),levelMaterialId);
      	
      	grantRate($("#level2Num"),$("#level2_num_rate"));
   	 });
	 
	 
	 $('#level3MaterialId').change(function(){
      	var levelMaterialId= $(this).val();
      	getStock($("#level3_num"),levelMaterialId);
      	
      	grantRate($("#level3Num"),$("#level3_num_rate"));
   	 });
	 

	 function getStock(obj,levelMaterialId){
		 var url="[[${sysUrl}]]/sys/award/grant/stock/get";
			var params ={'levelMaterialId' : levelMaterialId};
			$.post(url,params,function(data){
				if(data.meta.success){
					$(obj).html(data.data.stock);
				}else{
					parent.layer.msg(data.meta.message,{icon:11});
				}
			});
	 }
	 	
})

</script>
</html>