<{import 
	file="cart/header.html"
	title="订单结算"  
	css="public,settle"
}> 
<div class="main_bar">
	<div class="main">
		<!-- 购买地址 --> 
		<div class="addr">
			<div class="name"><span>确认收货地址</span><a href="<{$__URI__}>address/index" target="_blank">管理收货地址</a></div>  
			<div class="con"> 
				<{uqcms m="address" v="addr" c="get_all"}> 
				<{if $addr}> 
					<{foreach item=i from=$addr}>
						<div class="addr_box add_con_<{$i.id}> <{if $i.is_default}> ck<{/if}>">
							<label>
								<span><{if $i.is_default}> 寄送至<{else}>&nbsp;<{/if}></span>
								<input type="radio" name="address_id" value="<{$i.id}>" <{if $i.is_default}> checked="checked"<{/if}>/>
								<p> <{$i.prov_cn}> <{$i.city_cn}> <{$i.dist_cn}> <{$i.address}> ( <{$i.name}> 收) <{$i.mobile}></p>
							</label>
							<div class="rt">
								<a href="javascript:;" addrid="<{$i.id}>" onClick="address_edit('编辑地址', '<{$__URI__}>address/edit?type=1&id=<{$i.id}>')">编辑</a>
								<a href="javascript:;" addrid="<{$i.id}>" class="address_del">删除</a>	
							</div>	
						</div>
					<{/foreach}>
				<{else}> 
					<div class="noaddr">请添加地址</div>
				<{/if}>
			</div>
			<div class="addr_add">
				<a href="javascript:;" onClick="address_edit('添加地址', '<{$__URI__}>address/edit?type=1')">添加新地址</a>
			</div>
		</div>
		
		<!------购物清单列表-------> 
		<div class="cart_name"><span>商品清单</span></div>
		<div class="cart">
		<{uqcms m="cart" v="cart" c="settle"}>
		<{if $cart.list}>	
			<{foreach item=i from=$cart.list}> 
				<div class="cart_box">
					<div class="name">
						<i class="sp_icon" style="background:url(<{$__PUBLIC__}>images/icon/shop.png)"></i>
						<a href="<{$__URI__}>shop/<{$i.id}><{$__HTML__}>" target="_blank">店铺：<{$i.name}></a>
						<{if $i.sps_reduce_name}>
							<span class="reduce_box"><{$i.sps_reduce_name}></span>
						<{/if}>
						<div class="ms">
							<div class="zd m2">单价</div>
							<div class="zd m3">数量</div>
							<div class="zd m4">小计</div>
							<div class="zd m5">状态</div>
						</div>	
					</div> 
					<!--产品购买列表-->
					<{foreach item=ii from=$i.goods}>
						<div class="desc cart_detail_<{$ii.id}>">
							<div class="gs_con">
								<div class="images">
									<a href="<{$__URI__}>goods/<{$ii.gid}><{$__HTML__}>" target="_blank">
										<img src="<{$__IMG__}><{$ii.images|img_size:80x80}>" width="70"/>
									</a>
								</div>
								<div class="ttl">
									<div class="sttl"><a href="<{$__URI__}>goods/<{$ii.gid}><{$__HTML__}>" target="_blank" class="gttl"><{$ii.title}></a></div>
									<div class="sku">
										<{if $ii.sku_name}>
											<{foreach item=iii from=$ii.sku_name}>
												 <span style="color:#999;"><{$iii.0}>：<{$iii.1}> &nbsp;&nbsp;&nbsp;</span>
											<{/foreach}>
										<{/if}>
									</div>
								</div>
							</div>
							<div class="cart_right">
								<div class="ct">
									<span> ¥ <{$ii.price}><{if $ii.act_status}><a class="cuxiao">促</a><{/if}></span>
									<s> ¥ <{$ii.market_price}></s>
								</div>
								<div class="ct"><{$ii.buy_num}> </div> 
								<div class="ct zs">¥ <{$ii.total_price}></div> 
								<div class="ct"><{if $ii.stock_status}>有货<{else}>无货<{/if}></div> 
							</div>
						</div> 
					<{/foreach}>
					
					<!--留言和金额部分-->
					<div class="ly">
						<div class="left">
							<span>留言：</span>
							<p><input type="text" name="message[<{$i.id}>]" class="message" data-spid="<{$i.id}>" size="50" placeholder="选填:填写内容已和卖家协商确认"/></p>
						</div>
						<div class="right">
							<div class="info">
								<span>商品费用</span>
								<strong>¥ <{$i.goods_price}></strong>
							</div>  
							<{if $i.coupon_list}>
								<div class="info">
									<span>优惠</span>
									<strong>
										<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>
									</strong>
								</div>
							<{/if}> 
							<div class="info express_price_<{$i.id}>">
								<span>运费</span>
								<strong>¥  <{$i.express_price}></strong>
							</div>
							<div class="info shop_price_<{$i.id}>">
								<span>店铺合计(含运费)</span>
								<strong>¥ <{$i.shop_price}></strong>
							</div> 
						</div>
					</div>
					 
				</div>
			<{/foreach}>
		<{else}>
			<div class="cart_no_goods">提交的订单为空</div>
			<script type="text/javascript"> 
			location.href="<{$__URI__}>cart/index"; 
			</script>
		<{/if}> 
		</div>
		<div class="total_div">
			<div class="total_price">
				<span>实付款：</span>
				<p id="pay_total_price"><{$cart.settle_price}></p>
				<span style="padding-left:5px;">元</span>
			</div> 
		</div>
		<div class="submit_order">
			<div class="lft">
				<a href="<{$__URI__}>cart/index">返回购物车修改</a>
				<{if $cart.post_method == 'direct'}>
					<!--直接提交需要传递 sku_id,buy_num -->
					<input type="hidden" name="post_method" value="<{$cart.post_method}>"/> 
					<input type="hidden" name="buy_num" value="<{$cart.post_buy_num}>"/>
					<input type="hidden" name="sku_id" 	value="<{$cart.post_sku_id}>"/>
				<{/if}>
				<input type="hidden" name="share_id" value="<{$cart.share_id}>"/>
				<input type="button" name="submit" class="settle_pay" value="下单支付"/>
			</div>
		</div>
		
	</div>
</div>
<{import file="footer.html"}>
<script type="text/javascript">
function address_edit(name, url){ 
	ajax_dialog(name, url, '550'); 
}

function settle_edit_address(data, is_update){
	con_html = '<label>'+
					'<span>寄送至</span>'+
					'<input type="radio" name="address_id" value="'+data.id+'"/>'+
					'<p>'+data.prov_cn+' '+data.city_cn+' '+data.dist_cn+' '+data.address+' ( '+data.name+' 收) '+data.mobile+'</p>'+
				'</label>'+
				'<div class="rt">'+
					'<a href="javascript:;" addrid="'+data.id+'" onClick="address_edit(\'添加地址\', \'<{$__URI__}>address/edit?type=1&id='+data.id+'\')">编辑</a> '+
					'<a href="javascript:;" addrid="'+data.id+'" class="address_del">删除</a>'+
				'</div>';
	div_num = $('.addr_box').length;
	if(div_num == '0'){
		$(".addr .con").html('<div class="addr_box add_con_'+data.id+'">'+con_html+'</div>'); 
	}else{
		if(is_update == '1'){
			$(".add_con_"+data.id).html(con_html);
		}else{
			$(".addr_box:last-child").after('<div class="addr_box add_con_'+data.id+'">'+con_html+'</div>');
		}
	}
	$('.add_con_'+data.id+' input[name=address_id]').attr("checked", "checked"); 
	
	// 改变状态
	$(".addr .con .addr_box").removeClass('ck').find('label span').html('&nbsp;')
	$(".add_con_"+data.id).addClass('ck').find('label span').html('寄送至'); 
	
	/*这个地方只在结算处起作用，type=1只对结算处有作用*/
	coupon_id = $("#promote").val();
	area_id   = $(".addr_box label input:checked").val();  
	//getPrice(area_id, coupon_id);
} 
$(function(){
	// 设为默认
	$(document).on("click", ".address_default", function(){ 
		$this = $(this);
		var id = $this.attr('addrid'); 
		$.post(URI+'address/set_default',{id:id},function(result){ 
			data = jQuery.parseJSON(result); 
			if(data.error == '0'){
				$(".add_con input:radio[name=address_id]").prop("checked",false);
				$('.add_con_'+id+' input:radio[name=address_id]').prop("checked",true);
			}else{
			 	$.alert(data.msg);
			}
		});
	});
	// 地址删除
	$(document).on('click', ".address_del", function(){ 
		$this = $(this);
		id = $this.attr('addrid');
		$.post(URI+'address/del', {id:id}, function(result){
			data = jQuery.parseJSON(result); 
			if(data.error == '0'){
				$('.add_con_'+id).remove(); 
				var div_num = $('.add_con').length;
				if(div_num == '0'){ 
					$(".add_box").html('<span class="noaddr">请添加地址</span>');
				}		 
			}else{
				$.alert(data.msg); 
			}
		});
	});
	// -----------------------------
	
	// 地址选择
	$(document).on("click", "input[name='address_id']", function(){ 
		$this = $(this);
		val_id = $this.val();
		// 选中地址重新计费 
		price_count();
		// 改变状态
		$(".addr .con .addr_box").removeClass('ck').find('label span').html('&nbsp;')
		$(".add_con_"+val_id).addClass('ck').find('label span').html('寄送至');  
	});
 	 
	// 选择优惠券计费 
	$(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);
				} 	
			} 
		});
	}
	
	// 支付默认第一个自动选择
	if($('input:radio[name="payment"]:checked').length == 0){
		$('input:radio[name="payment"]').eq(0).prop("checked",true);
	}
	
	$(document).on('click', ".settle_pay", function(){ 
		var address_id = $('input[name="address_id"]:checked').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=URI+"account/pay?trade_no="+data.trade_no;
				}else{
					$.alert(data.msg); 
				}
			},error:function(){
				$.alert('系统超时');
			}
		});
		return false;
	}); 
}); 

function pay_status_box(){
	var dialog_con = '<div class="dg_bgcolor"></div><div class="dg_box_bar">'+
	 					'<div class="pay_status_box">'+
							'<div class="tishi"><p>付款提示,请您在新打开的页面进行支付，支付完成前请不要关闭此窗口。</p></div>'+
							'<div class="jguo">'+
								'<a href="'+URI+'news/help?id=6">支付遇到了问题</a>'+
								'<a href="'+URI+'buy/index">查看结果</a>'+
							'</div>'+
						'</div>'+
					'</div>'; 
	$("body").after(dialog_con).css({opacity:'0',marginTop:'-10px'}).animate({opacity:'1',marginTop:'0'},300); 
} 
</script>