<{import file="common.html" title="订单结算"}> 
<link rel="stylesheet" type="text/css" href="<{$__APP__}>public/css/settle.css?v=<{$js_version}>"> 
<div class="cnav">
	<a class="back" href="<{$__WAP__}>cart/index">&nbsp;</a>订单结算<a href="javascript:;" class="thome">主页</a>
</div> 
<input type="hidden" name="post_method" value="<{$smarty.session.post_method}>"/>
<div class="addr_box">
	<{uqcms m="address" v="address_row" c="get_session_address"}>
	<{if $address_row}>
		<div class="addr_detail"> 
			<a href="<{$__WAP__}>address/index?cart=1">
				<div class="addr_icon"><em class="addr_icon"></em></div>
				<div class="addr_info">
					<div class="name">
						<span><{$address_row.name}></span>
						<span><{$address_row.mobile}></span>
					</div>
					<div class="detail"><{$address_row.prov_cn}>，<{$address_row.city_cn}>，<{$address_row.dist_cn}>，<{$address_row.address}></div>
				</div> 
			</a>
		</div>
		<input type="hidden" name="address_id" value="<{$address_row.id}>" id="address_id">
	<{else}>	
		<div class="addr_no">	
			<a href="<{$__WAP__}>address/index?cart=1" class="chose_address"><span></span><em>请选择收货地址！</em></a>
		</div>
	<{/if}>
</div>

<{uqcms m="cart" v="cart" c="settle"}> 
<{if $cart.list}>
	<div class="cart_box"> 
		<{foreach item=i from=$cart.list}>
		<div class="cart_shop">
			<div class="cart_name">
				<em></em>
				<span><{$i.name}></span>
			</div> 
			<div class="cart_gds">
				<{foreach item=ii from=$i.goods}>
					<div class="gs_desc">
						<div class="images">
							<a href="<{$__WAP__}>goods/<{$ii.gid}><{$__HTML__}>"><img src="<{$__IMG__}><{$ii.images|img_size:200x200}>"/></a>
						</div>
						<div class="ttl">
							<div class="sttl"><a href="<{$__WAP__}>goods/<{$ii.gid}><{$__HTML__}>" class="gttl"><{$ii.title}></a></div>
							<{if $ii.sku_name}>
								<div class="sku"> 
									<{foreach item=s from=$ii.sku_name}>
										 <span><{$s.0}>：<{$s.1}> &nbsp;&nbsp;</span>
									<{/foreach}>
								</div>
							<{/if}>
							
							<div class="pr">
								<{if $ii.stock_status}>
									<span class="l uq_color">¥ <{$ii.price}></span> <span class="r">X<{$ii.buy_num}></span>
								<{else}>
									<span class="l">¥ <{$ii.price}></span> <span class="r">库存不足</span>
								<{/if}> 
							</div>
						</div>
					</div> 
				<{/foreach}> 
			</div> 
			<div class="other_pirce"> 
				<div class="obox all_price">
					<span>商品</span><strong><{$i.goods_price}></strong>
				</div>
				<div class="obox all_price express_price_<{$i.id}>">
					<span>运费</span>
					<strong><{if $settle.express_price}>+ <{$settle.express_price}><{else}>免邮<{/if}></strong>
				</div> 
				<!--促销部分-->
				<{if $settle.coupon_list}>  
					<div class="obox all_price">
						<span>优惠券</span> 
						<em>
							<select name="coupon_id[<{$i.id}>]" class="coupon"  style="margin:0;">
								<option value="0" data-spid="<{$i.id}>">不使用优惠</option>
								<{foreach item=c from=$i.coupon_list}>
									<option value="<{$c.id}>" data-spid="<{$i.id}>">优惠券：满<{$c.full_price}> 减<{$c.price}></option>
								<{/foreach}>
							</select>
						</em>
					</div> 
				<{/if}>
				<div class="ly">
					<span>买家留言：</span> 
					<em><input type="text" name="message[<{$i.id}>]" class="message" data-spid="<{$i.id}>" placeholder="填写留言和备注"/></em>
				</div>
			</div>
			
		</div> 
		<{/foreach}> 	
	</div> 
<{else}>
	<script type="text/javascript"> 
		location.href="<{$__WAP__}>cart/index"; 
	</script>
<{/if}> 

<{if $cart.post_method == 'direct'}>
	<!--直接提交需要传递 sku_id,buy_num -->
	<input type="hidden" name="post_method" value="<{$settle.post_method}>"/>
	<input type="hidden" name="sku_id" 		value="<{$settle.post_sku_id}>"/>
	<input type="hidden" name="buy_num" 	value="<{$settle.post_buy_num}>"/>
<{/if}>

<!--去下单--> 
<div class="go_pay">
	<div class="lft"> 
		<div class="total_price" id="total_price">
			<span>合计</span>
			<em id="pay_total_price" class="uq_color"><{$cart.settle_price}></em> 
		</div>
	</div>
	<div class="rit"><input type="submit" name="submit" class="gopay settle_pay" value="提交订单"/></div></div> 
</div>
<{import file="footer.html"}>
<script type="text/javascript"> 
$(function(){  
	if($('input:radio[name="payment"]:checked').length == 0){
		$('input:radio[name="payment"]').eq(0).prop("checked",true);
	}
	
	// 地址
	address_id = $('input[name="address_id"]').val();
	if(address_id){
		price_count();
	}
	
	// 选择优惠券计费 
	$(document).on('change', ".coupon", function(){ 
		price_count();
	});
	
	function price_count(){ 
		// 优惠券的获取
		var coupon_arr={};
		$(".coupon").each(function(){
			cp_id = $(this).find("option:selected").val();
			sp_id = $(this).find("option:selected").attr('data-spid'); 
			coupon_arr[sp_id] = cp_id;
		}); 
		// 地址的获取
		address_id = $("input[name='address_id']").val(); 
		//$.loading();
		$.ajax({ 
			async: true, 
			type : "POST", 
			url  : URI+'cart/settle_price',
			data : {address_id:address_id, coupon_id:coupon_arr},
			//dataType : 'json', 
			success:function(result) { 
				//$.loadingClose();
				data = jQuery.parseJSON(result); 
				if(data.error == '0'){
					$.each(data.list, function(k,v){
						$(".express_price_"+k+" strong").html('¥ '+v.express_price);
						$(".shop_price_"+k+" strong").html('¥ '+v.shop_price);
					}); 
					$("#pay_total_price").html(data.settle_price);
				} 	
			} 
		});
	}
	
	$(document).on('click', ".settle_pay", function(){ 
		var address_id = $('input[name="address_id"]').val();
		if(address_id == '' || address_id == undefined){  
			$.alert("请选择一个地址");  return false;  
		}
		
		var coupon_arr = {};
		$(".coupon").each(function(){
			cp_id = $(this).find("option:selected").val();
			sp_id = $(this).find("option:selected").attr('data-spid'); 
			coupon_arr[sp_id] = cp_id;
		}); 
		
		var message_arr = {};
		$(".message").each(function(){
			cp_id = $(this).val();
			sp_id = $(this).attr('data-spid'); 
			message_arr[sp_id] = cp_id;
		});
		
		$.ajax({
			type:'POST',
			url:URI+'cart/create_order?v='+new Date().getTime(),
			data:{address_id:address_id, coupon_id:coupon_arr, message:message_arr},
			success:function(result){
				data = $.parseJSON(result);
				if(data.error == '0'){
					location.href=WAP+"account/pay?trade_no="+data.trade_no;
				}else{
					$.alert(data.msg); 
				}
			},error:function(){
				$.alert('系统超时');
			}
		});
		return false;
	}); 
	
}); 
</script>  