<style>
	.line_p{line-height:26px;font-size:13px;margin-top:5px;}
	.goods_refund_form th{border: 1px solid #efefef;line-height: 34px;}
	.goods_refund_form td{border: 1px solid #efefef;line-height: 34px;padding: 9px 15px;}
	.w100{width:100px;}
	.lh30{line-height:30px;font-size:13px;}
	.mrt8{margin-top:8px;}
	.blu{color:blue;}
</style>
<form class="form-horizontal form-validate" id="form1" action="{:U('order/change_order')}" method="post" enctype="multipart/form-data">
	<input type='hidden' name='id' value="<?php echo $item['order_id']?>" />

	<div class="modal-dialog">
		<div class="modal-content" style="width:1000px;">
			<div class="modal-header">
				<button data-dismiss="modal" class="close" type="button">×</button>
			</div>
			<div class="modal-body" style = "padding: 10px;" >
				<div style="display: flex">
					<div class="goods_refund_form" style="width:100%;display: flex;flex-direction: column;justify-content: center;align-items: flex-start;padding-left: 5px">
						<table style="border: 1px solid #efefef;width:100%;">
							<tr>
								<th style="width:30%;text-align:center;" colspan="2">商品</th>
								<th style="width:10%;text-align:center;">单价</th>
								<th style="width:10%;text-align:center;">数量</th>
								<th style="width:10%;text-align:center;">价格</th>
								<th style="width:10%;text-align:center;">改价最多可减</th>
								<th style="width:25%;text-align:center;">涨价或减价</th>
							</tr>
							<?php $order_goods_total = 0;?>
							<?php $member_youhui = 0; ?>
							<?php foreach($order_goods as $goods){?>
							<tr>
								<?php $member_youhui += round(($goods['oldprice'] - $goods['price'])*$goods['quantity'],2); ?>
								<input type="hidden" name="order_goods_id[]" value="<?php echo $goods['order_goods_id']?>"/>
								<td style="text-align: right;padding-right: 0;magrin-right: 10px;border-right: none;">
									<img src="<?php echo $goods['goods_img_url'];?>" style='width:52px;height:52px;border:1px solid #efefef; padding:1px;' >
								</td>
								<td style="padding-left:10px;border-left: none;"><?php echo $goods['name'];?></td>
								<td style="text-align:center;">
									￥<?php echo $goods['oldprice'];?>
								</td>
								<td style="text-align:center;">
									<?php echo $goods['quantity'];?>
								</td>
								<td style="text-align:center;">
									￥<span id="goods_total_<?php echo $goods['order_goods_id']?>"><?php echo round($goods['old_total'],2);?></span>
								</td>
								<td style="text-align:center;">
									￥<span id="goods_max_total_<?php echo $goods['order_goods_id']?>"><?php echo round($goods['total']-$goods['fullreduction_money']-$goods['voucher_credit']-$goods['score_for_money'],2);?></span>
								</td>
								<td style="text-align:center;">
									<input style="width: 35px; height: 35px; border: 2px white; float: left;" type="button" value="-" onclick="reduction(this)" order_goods_id="<?php echo $goods['order_goods_id']?>"/>
									<input type="text" name="change_price[]" class="layui-input change_price" id="change_price_<?php echo $goods['order_goods_id']?>" value="" maxlength="10" style="width: 100px;float: left;" order_goods_id="<?php echo $goods['order_goods_id']?>" onchange="validationNumber(this, 2)"/>
									<input style="width: 35px; height: 35px; border: 2px white;float: left;" type="button" value="+" onclick="add(this)" order_goods_id="<?php echo $goods['order_goods_id']?>"/>
								</td>
							</tr>
							<?php
								$order_goods_total = round($order_goods_total + $goods['old_total'],2);
							?>
							<?php } ?>
							<!--
							<tr>
								<td colspan="7" style="text-align:right;">
									<?php
										$change_price = 0;
										if($item['is_change_price'] == 1){
											$change_price = round($item['total'] - $item['old_price'],2);
										}
										$youhui_total = round($item['voucher_credit']+$item['fullreduction_money']+$item['score_for_money']+$member_youhui,2);
										$free_tongji = round($order_goods_total + $item['shipping_fare']-$item['voucher_credit']-$item['fullreduction_money'] - $item['score_for_money'] - $member_youhui + $change_price,2);
										
										$order_goods_total2 = $order_goods_total + $change_price ;
										
									?>
									<p> <span class="price-inner">商品合计：</span><span style="font-weight: bold;color:red;">￥<font id="goods_total_price"><?php echo $order_goods_total2;?></font></span></p>
								</td>
							</tr>
							-->
							<tr>
								<td colspan="7" style="text-align:left;">
									<?php
										$change_price = 0;
										if($item['is_change_price'] == 1){
											$change_price = round($item['total'] - $item['old_price'],2);
										}
										$youhui_total = round($item['voucher_credit']+$item['fullreduction_money']+$item['score_for_money']+$member_youhui,2);
										$free_tongji = round($order_goods_total + $item['shipping_fare']-$item['voucher_credit']-$item['fullreduction_money'] - $item['score_for_money'] - $member_youhui + $change_price,2);
									?>
									<input type="hidden" id="order_total" name="order_total" value="<?php echo $order_goods_total;?>"/>
									<input type="hidden" id="youhui_total" name="youhui_total" value="<?php echo $youhui_total;?>"/>
									<input type="hidden" id="shipping_fare" name="shipping_fare" value="<?php echo $item['shipping_fare'];?>"/>
									<input type="hidden" id="free_tongji" name="free_tongji" value="<?php echo $free_tongji;?>"/>
									<input type="hidden" id="member_youhui" name="member_youhui" value="<?php echo -$member_youhui;?>"/>
									<input type="hidden" id="order_change_price" name="order_change_price" value="<?php echo $change_price;?>"/>
									<p>只有在订单未付款时才支持改价，商品改价后价格不能小于 0 元，订单修改的价格不包含优惠信息，运费价格是 0 的时候商品免运费</p>
									<p>买家实付 = 原价 + 运费 + 涨价或减价 - 优惠&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
										买家实付：<?php echo round($order_goods_total,2);?> + <?php echo round($item['shipping_fare'],2);?>  <span id="change_price"><?php if($change_price >= 0){echo ' + '.$change_price;}else{echo ' - '.(0-$change_price);}?></span>
										<?php if($youhui_total >= 0){echo ' - '.$youhui_total;}else{echo ' + '.(0-$youhui_total);}?> = <span id="free_total"><?php echo $free_tongji;?></span>
								</td>
							</tr>
						</table>

					</div>
					<div style="clear:both;"></div>
				</div>
			</div>
			<div class="modal-footer">
				<input type="hidden" id="real_refund_quantity" name="real_refund_quantity" value="0" />
				<button class="btn btn-primary" type="submit" id="ck_opgoodsrefund">确认改价</button>
				<button data-dismiss="modal" class="btn btn-default" type="button">取消</button>
			</div>
		</div>
</form>
<script>
function validationNumber(e, num) {
	var regu = /^[\-0-9]+\.?[0-9]*$/;
	var order_goods_id = $(e).attr("order_goods_id");
	var goods_total = parseFloat($('#goods_max_total_'+order_goods_id).html());
	if (e.value != "") {
		if (!regu.test(e.value)) {
			e.value = 0;
			e.focus();
		} else {
			if (num == 0) {
				if (e.value.indexOf('.') > -1) {
					e.value = e.value.substring(0, e.value.length - e.value.split('.')[1].length + 2);
					e.focus();
				}
			}
			if (e.value.indexOf('.') > -1) {
				if (e.value.split('.')[1].length > num) {
					e.value = e.value.substring(0, e.value.length - e.value.split('.')[1].length + 2);
					e.focus();
				}
			}
			var price = operation(parseFloat(goods_total),parseFloat(e.value),2,'add');
			if(price < 0.1){
				layer.msg("改价后的商品实付价格不能低于0.1元",{icon: 1,time: 2000});
				e.value = 0;
				e.focus();
			}
		}
	}
	cal_total(order_goods_id);
}
function validationNumber_reduce(e, num) {
	var regu = /^[\-0-9]+\.?[0-9]*$/;
	var order_goods_id = $(e).attr("order_goods_id");
	var goods_total = parseFloat($('#goods_max_total_'+order_goods_id).html());
	if (e.val() != "") {
		if (!regu.test(e.val())) {
			e.val(0);
			e.focus();
		} else {
			if (num == 0) {
				if (e.val().indexOf('.') > -1) {
					e.val(e.val().substring(0, e.val().length - e.val().split('.')[1].length + 2));
					e.focus();
				}
			}
			if (e.val().indexOf('.') > -1) {
				if (e.val().split('.')[1].length > num) {
					e.val(e.val().substring(0, e.val().length - e.val().split('.')[1].length + 2));
					e.focus();
				}
			}
			var price = operation(parseFloat(goods_total),parseFloat(e.val()),2,'add');
			if(price < 0.1){
				layer.msg("改价后的商品实付价格不能低于0.1元",{icon: 1,time: 2000});
				e.val(0);
				e.focus();
			}
		}
	}
	cal_total(order_goods_id);
}

function reduction(obj){
	var change_price = $.trim($(obj).parent().find('.change_price').val());
	var order_goods_id = $(obj).attr("order_goods_id");
	if(change_price == ''){
		$(obj).parent().find('.change_price').val(-1);
	}else{
		var price = parseFloat(change_price)-1;
		$(obj).parent().find('.change_price').val(price.toFixed(2));
	}
	validationNumber_reduce($('#change_price_'+order_goods_id), 2);
}

function add(obj){
	var change_price = $.trim($(obj).parent().find('.change_price').val());
	var order_goods_id = $(obj).attr("order_goods_id");
	if(change_price == ''){
		$(obj).parent().find('.change_price').val(1);
	}else{
		var price = parseFloat(change_price)+1;
		$(obj).parent().find('.change_price').val(price.toFixed(2));
	}
	cal_total(order_goods_id);
}
//计算商品合计
function cal_total(order_goods_id){
	var order_total = $('#order_total').val();
	var change_price = 0;
	$('.change_price').each(function(){
		var price = $.trim($(this).val());
		if(price != ''){
			change_price = parseFloat(change_price) + parseFloat(price);
		}
	})
	var order_change_price = $('#order_change_price').val();

	var free_tongji = parseFloat($('#free_tongji').val());
	var free_total = parseFloat(free_tongji) + parseFloat(change_price);
	
	var goods_total_price = parseFloat(free_tongji) + parseFloat(change_price);
	$('#goods_total_price').html(goods_total_price.toFixed(2));
	
	if(free_total <= 0){
		layer.msg("买家实付金额不能小于0",{icon: 1,time: 2000});
		$('#change_price_'+order_goods_id).val(0);
		cal_total(order_goods_id);
		return false;
	}else{
		change_price = change_price + parseFloat(order_change_price);
		change_price = change_price.toFixed(2);
		if(change_price > 0){
			$('#change_price').html('+ '+change_price);
		}else{
			$('#change_price').html('- '+(0-change_price));
		}
		$('#free_total').html(free_total.toFixed(2));
	}

}
/*
     * 判断obj是否为一个整数
     */
function isInteger(obj) {
	return Math.floor(obj) === obj;
}
/*
     * 将一个浮点数转成整数，返回整数和倍数。如 3.14 >> 314，倍数是 100
     * @param floatNum {number} 小数
     * @return {object}
     *   {times:100, num: 314}
     */
function toInteger(floatNum) {
	var ret = {times: 1, num: 0}
	var isNegative = floatNum < 0
	if (isInteger(floatNum)) {
		ret.num = floatNum
		return ret
	}
	var strfi  = floatNum + ''
	var dotPos = strfi.indexOf('.')
	var len    = strfi.substr(dotPos+1).length
	var times  = Math.pow(10, len)
	var intNum = parseInt(Math.abs(floatNum) * times + 0.5, 10)
	ret.times  = times
	if (isNegative) {
		intNum = -intNum
	}
	ret.num = intNum
	return ret
}
/*
     * 核心方法，实现加减乘除运算，确保不丢失精度
     * 思路：把小数放大为整数（乘），进行算术运算，再缩小为小数（除）
     *
     * @param a {number} 运算数1
     * @param b {number} 运算数2
     * @param digits {number} 精度，保留的小数点数，比如 2, 即保留为两位小数
     * @param op {string} 运算类型，有加减乘除（add/subtract/multiply/divide）
     *
     */
function operation(a, b, digits, op) {
	var o1 = toInteger(a)
	var o2 = toInteger(b)
	var n1 = o1.num
	var n2 = o2.num
	var t1 = o1.times
	var t2 = o2.times
	var max = t1 > t2 ? t1 : t2
	var result = null
	switch (op) {
		case 'add':
			if (t1 === t2) { // 两个小数位数相同
				result = n1 + n2
			} else if (t1 > t2) { // o1 小数位 大于 o2
				result = n1 + n2 * (t1 / t2)
			} else { // o1 小数位 小于 o2
				result = n1 * (t2 / t1) + n2
			}
			return result / max
		case 'subtract':
			if (t1 === t2) {
				result = n1 - n2
			} else if (t1 > t2) {
				result = n1 - n2 * (t1 / t2)
			} else {
				result = n1 * (t2 / t1) - n2
			}
			return result / max
		case 'multiply':
			result = (n1 * n2) / (t1 * t2)
			return result
		case 'divide':
			result = (n1 / n2) * (t2 / t1)
			return result
	}
}

</script>