<template>
	<view class="container" :style="{ height: winHeight+ 'px' }">
		<!-- 状态栏高度 -->
		<view :style="{ height: `${statusBarHeight+20}px` }"></view>
		<!-- 进行中/历史订单 -->
		<view class="status-bar">
			<view>
				<text :class="isShow? 'status-true': 'status-false'" @click="statusClick">外卖配送</text>
				<text :class="!isShow? 'status-true': 'status-false'" @click="statusClick">到店自取<text class="box-txt">（15分钟后取餐）</text></text>
			</view>
		</view>

		<view style="background-color: white;border-radius: 23rpx;margin-bottom: 10rpx;padding: 19rpx 38rpx 38rpx 38rpx">
			<view class='address' @tap='onAddress'>
				<view class='addressCon' v-if="addressInfo.realName">
					<view class='name acea-row boxnamae'>
						<view class="line1 select-name">{{addressInfo.realName}}</view>
						<view class='phone'>{{addressInfo.phone}}</view>
					</view>
					<view class="acea-row">
						<view ><text class='default  font_color' v-if="addressInfo.isDefault">[默认]</text>{{addressInfo.province}}{{addressInfo.city}}{{addressInfo.district}}{{ addressInfo.street}}{{addressInfo.detail}}</view>
					</view>
				</view>
				<view class='addressCon' v-else>
					<view class='setaddress'>设置收货地址</view>
				</view>
				<view class='iconfont icon-jiantou ' style="font-size: 30rpx;"></view>
			</view>
			<view class="item-go" @tap="goIndex = true" :class="goIndex ? 'item-go-avt' : ''">
				<text style="font-weight: 600;font-size: 27rpx;">立即配送</text>
				<text style="font-weight: 500;font-size: 23rpx;margin-left: 76rpx">预计10：12送达</text>
				<image class="go-img"></image>
			</view>
			<view style="margin-top: 10rpx;" class="item-go" @tap="goIndex = false" :class="!goIndex ? 'item-go-avt' : ''">
				<text style="font-weight: 600;font-size: 27rpx;">预约配送</text>
				<text style="font-weight: 500;font-size: 23rpx;margin-left: 76rpx">选择时间 》</text>
				<image class="go-img"></image>
			</view>
		</view>

		<view class="item-goods">
			<text>{{detils.merchantInfoList[0].merName}}</text>
			<view class="good" v-for="(good, key) in goods_list" :key="key">
				<image :src="good.image" class="image"></image>
				<view class="right">
					<text class="name">{{ good.productName }}</text>
					<text class="tips">x {{ good.payNum }}</text>
					<view class="price_and_action">
						<text class="price">￥{{ good.price }}</text>
						<view class="btn-group">
							<view class="dot"></view>
						</view>
					</view>
				</view>
			</view>
			<view style="width: 100%; height: 2rpx;border: 2rpx dashed #D6D7DC;position: relative;bottom: 20rpx;"></view>
			<view class='item-con'>
				<text>打包费</text>
				<text>￥2</text>
			</view>
			<view class='item-con' style="margin: 0;">
				<text>用户配送费</text>
				<text style="text-decoration: line-through;">￥2 <text>免费配送</text></text>
			</view>
			<view style="font-weight: 400;font-size: 21rpx;color: #999999;margin-bottom: 10rpx;">配送高峰期，可能配送费上调</view>
			<view class='item-con'>
				<text @tap="isZhuan = !isZhuan">已享优惠 <text :style="{ transform: isZhuan ? 'rotate(90deg)' : 'rotate(-90deg)'}"
					 class='iconfont icon-jiantou xuanzhuan' style="font-size: 22rpx;display: inline-block;"></text></text>
				<text>- ￥2</text>
			</view>
			<view v-if="isZhuan" class='item-con' style="background: #F8F9FB;padding: 8rpx 19rpx;font-weight: 500;font-size: 21rpx;color: #FF6702;">
				<text>活动红包</text>
				<text>- ￥2</text>
			</view>
			<!-- 备注 -->
			<view>备注：</view>
			<textarea class="remark-txt" v-model="remark">
				
			</textarea>
			
		</view>
		<viwe style="height: 200rpx;"></viwe>

		<!-- 购物车栏 begin -->
		<view class="cart-box" :style="{top: (winHeight - 100) + 'px'}">
			<view style="margin-left: 39rpx;">
				<view style="font-weight: 600;font-size: 27rpx;color: #FFFFFF;" class="price">￥{{ total }}</view>
				<text style="font-weight: 400;font-size: 23rpx;color: #999999;">已优惠￥2</text>
			</view>
			<button type="primary" class="pay-btn bg-color" @tap="orderCreate">
				去支付
			</button>
		</view>
		<!-- 购物车栏 end -->
	</view>
</template>

<script>
	import {
		getAddressDetail,
		getAddressDefault,
		getAddressList,
	} from '@/api/user.js';
	import {
	
		loadPreOrderApi,
		orderCreate
	} from '@/api/order.js';
	var statusBarHeight = uni.getSystemInfoSync().statusBarHeight + 'px';
	let app = getApp();
	export default {
		data() {
			return {
				total: 0,
				statusBarHeight: app.globalData.statusBarHeight,
				navigationBarHeight: 0,
				winHeight: 0,
				isZhuan: true,
				isShow: true,
				goIndex: true,
				orderNo: '', //预下单订单号
				addressInfo: {}, //地址信息
				addressId: 0, //地址id
				goods_list: [],  //购物车数据
				cart: [],
				detils:'',//详情数据
				remark:'',//备注
				
			}
		},
		//下拉刷新
		onPullDownRefresh() {

		},
		onLoad(options) {
	
			let that = this;
			that.getAddressList()//获取地址			
			that.orderNo=options.orderNo
			console.log('订单号',that.orderNo)
			that.loadPreOrderApi()  //获取详情
			// 获取app德地址
			const app = getApp();
			let addressList=app.globalData.addressList
			uni.getSystemInfo({
				success: function(res) {
					that.winHeight = res.windowHeight
				},
			});
			//首页数据加载
			// this.getIndexConfig();
			// #ifdef MP-WEIXIN
			// 获取微信胶囊的位置信息 width,height,top,right,left,bottom
			const custom = wx.getMenuButtonBoundingClientRect()
			// 导航栏高度(标题栏高度) = 胶囊高度 + (顶部距离 - 状态栏高度) * 2
			this.navigationBarHeight = custom.height + (custom.top - this.statusBarHeight) * 2
			// console.log("导航栏高度："+this.globalData.navigationBarHeight)
			// #endif
			this.cart = uni.getStorageSync('cart')
		
		},
		onShow() {
				this.getAddressList()//获取地址
		},
		methods: {
			//订单创建
			orderCreate(){
				let orderMerchantRequestList = [] //数组	
				let merId=this.detils.merchantInfoList[0].merId // 商户id
				orderMerchantRequestList.push({
				   merId:merId,              // 商户id
				   remark:this.remark,             // 备注
				   shippingType: 1,        // 快递类型: 1-快递配送，2-到店自提，3-虚拟发货
				   userCouponId: 0,        // 惠券编号（不选时为0）
				})
				orderCreate({
					isUseIntegral:false, // 是否使用积分
					addressId:this.addressInfo.id, //收货地址id
					orderExtend:  JSON.stringify([{ key: 'value' }, { field: 'data' }]), //系统表单内容
					orderMerchantRequestList:orderMerchantRequestList, //订单商户对象
					platUserCouponId:0,//用户平台优惠券编号（不选时为0）
					preOrderNo:this.orderNo,//预下单订单号
					systemFormId: '',//系统表单ID
					traceId:"1 15N4MJksUcgG7onuaA9xuvoZwoLatM4mAE21CqXmMz6",
				}).then(res=>{
					console.log('创建',res)
					let data=res.data
					if(res.code){
						let url = `/pages/goods/order_payment/index?orderNo=${data.orderNo}&payPrice=${data.payPrice}`
						uni.redirectTo({
							url: url
						});
					}
				})
			},
			// 详情数据
			loadPreOrderApi(){
				loadPreOrderApi(this.orderNo).then(res=>{
					console.log('详情数据',res)
					if(res.code==200){
						let data=res.data
						this.detils=data
						this.goods_list=data.merchantInfoList[0].orderInfoList  //购物车数据
						// 总价
						this.total = (this.goods_list.reduce((acc, cur) => acc + cur.payNum * cur.price, 0)).toFixed(2)
					}
				})
			},
			// 获取地址数据
			getAddressList(){
				getAddressList().then(res=>{
					console.log('地址',res)
					if(res.code=200){
						this.addressInfo=res.data.find(item => item.isDefault === true);
						console.log('默认地址',this.addressInfo)
					}
				})
			},
			
			statusClick() {
				this.isShow = !this.isShow;
			},
		
			onAddress: function() {
				uni.navigateTo({
					url: '/pages/address/user_address_list/index?orderNo=' + this.orderNo
				});
			},
			/*
			 * 获取默认收货地址或者获取某条地址信息
			 */
			getaddressInfo: function() {
				// if (this.addressId) {
					getAddressDetail(this.addressId).then(res => {
						console.log('地址信息',res)
						if (res.data) {
							res.data.isDefault = res.data.isDefault;
							this.addressInfo = res.data || {};
						}
					})
				// }
			},
		},
		mounted() {

		},
	}
</script>

<style lang="scss" scoped>
	.bg-color {
		background-color: $bg-color-primary;
	}

	.status-bar {
		display: flex;
		align-items: center;
		justify-content: center;
		height: 125rpx;
		background-color: #FF6702;
		text-align: center;
		padding: 20px 0 16rpx 0;
	}

	.status-bar view {
		display: flex;
		align-items: center;
		justify-content: center;
		background: #EB5E00;
		border-radius: 37rpx;
	}

	.status-true {
		display: inline-block;
		width: 326rpx;
		padding: 10rpx 0;
		background-color: white;
		color: #FF6600;
		border-radius: 37rpx;
	}

	.status-false {
		display: inline-block;
		width: 326rpx;
		padding: 10rpx 0;
		color: #FFCBA9;
		border-radius: 37rpx;
	}

	.icon-jiantou {
		font-size: 26rpx;
		color: #515151;
		margin-left: 14rpx;
	}

	.address {
		margin-bottom: 40rpx;
		display: flex;
		align-items: center;
		justify-content: space-between;
	}

	.item-go {
		position: relative;
		width: 100%;
		padding: 20rpx 0;
		padding-left: 39rpx;
		background: #FFFFFF;
		border-radius: 15rpx;
		border: 2rpx solid #D6D7DC;
	}

	.item-go-avt {
		position: relative;
		color: #FF6702;
		background: #FFECE0;
		border-radius: 15rpx;
		border: 2rpx solid #FF6702;
	}

	.go-img {
		width: 54rpx;
		height: 54rpx;
		position: absolute;
		right: 0;
		top: 0;
	}

	.item-goods {
		display: flex;
		flex-direction: column;
		gap: 20rpx;
		width: 100%;
		// height: 100%;
		padding: 19rpx 38rpx;
		background: #FFFFFF;
		border-radius: 23rpx 23rpx 0rpx 0rpx;
	}

	.good {
		display: flex;
		align-items: center;
		

		.image {
			width: 160rpx;
			height: 160rpx;
			margin-right: 20rpx;
			border-radius: 8rpx;
		}

		.right {
			flex: 1;
			// height: 160rpx;
			overflow: hidden;
			display: flex;
			flex-direction: column;
			align-items: flex-start;
			justify-content: space-between;
			padding-right: 14rpx;

			.name {
				font-size: $font-size-base;
				margin-bottom: 10rpx;
			}

			.tips {
				width: 100%;
				height: 40rpx;
				line-height: 40rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				white-space: nowrap;
				font-size: $font-size-sm;
				color: $text-color-assist;
				margin-bottom: 10rpx;
			}

			.price_and_action {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;

				.price {
					font-size: $font-size-base;
					font-weight: 600;
				}
			}
		}
	}

	.cart-box {
		position: absolute;
		// bottom: 30rpx;
		left: 30rpx;
		right: 30rpx;
		height: 96rpx;
		border-radius: 48rpx;
		box-shadow: 0 0 20rpx rgba(0, 0, 0, 0.2);
		background-color: #000;
		display: flex;
		align-items: center;
		justify-content: space-between;
		z-index: 9999;

		.cart-img {
			width: 50rpx;
			height: 47rpx;
			position: relative;
			// margin-top: -48rpx;
		}

		.pay-btn {
			height: 100%;
			padding: 0 30rpx;
			color: #FFFFFF;
			border-radius: 0 50rpx 50rpx 0;
			display: flex;
			align-items: center;
			font-size: $font-size-base;
		}

		.price {
			flex: 1;
			color: $text-color-base;
		}
	}


	.item-con {
		display: flex;
		justify-content: space-between;
		align-items: center;
		font-weight: 500;
		font-size: 21rpx;
		color: #141414;
		margin-bottom: 10rpx;
	}

	.container {
		overflow: hidden;
		position: relative;
	}
	.box-txt{
		 overflow-wrap: break-word;
	
		  font-size: 22rpx;
		  font-family: PingFangSC-Medium;
		  font-weight: 500;
		  text-align: left;
		  white-space: nowrap;
		  line-height: 40rpx;
	}
	.boxnamae{
		overflow-wrap: break-word;
		  color: rgba(20, 20, 20, 1);
		  font-size: 32rpx;
		  font-family: PingFangSC-Medium;
		  font-weight: 500;
		  text-align: center;
		  white-space: nowrap;
		  line-height: 44rpx;
		
	}
	.remark-txt{
		width: 100%;
		height: 150rpx;
		background: #D6D7DC;
		color: #141414;
		padding: 20rpx;
		box-sizing: border-box;
	}
</style>
