<template>
	<view class="page">
		<navbar title="下单支付" leftClick @leftClick="$utils.navigateBack" />

		<view class="box">
			<!-- 地址 -->
			<view class="address" @click="openAddress">
				<image src="../static/address/icon1.png" mode=""></image>
				<view class="">
					{{ address.name }}
				</view>
				<view class="">
					{{ address.address }}
				</view>
				<view class="icon">
					<uv-icon size="30rpx" name="arrow-right"></uv-icon>
				</view>
			</view>

			<view class="productList">
				<view class="item"
				:key="index"
				v-for="(item, index) in productList">
					<view class="item-image">
						<image :src="item.image &&
						item.image.split(',')[0]" 
						mode="aspectFill"></image>
					</view>

					<view class="info">
						<view class="title">
							{{ item.title }}
						</view>
						<view class="desc">
							{{ item.sku }}
						</view>
						<view class="price-box">
							<view class="">
								<uv-number-box 
								v-model="item.num"
								v-if="[1, 2].includes(item.type)"
								></uv-number-box>
							</view>
							
							<!-- 会员价：普通商品并且是会员的时候显示 -->
							<view class="price"
							v-if="[2].includes(item.type) && [1].includes(userInfo.isPay)">
								会员价￥{{ item.vipPrice }}
							</view>
							
							<!-- 零售价、价格 -->
							<view class="price"
							v-else>
								￥{{ item.price }}
							</view>
						</view>
					</view>
				</view>
			</view>
			
			<view class="submit-box">
				<view class="peis">
					<view class="">
						配送方式
					</view>
					<view class="">
						商家自行配送
					</view>
				</view>
				<view class="priceInfo">
					<view class="">
						付款金额
					</view>
					<view class="totalPrice">
						￥{{ totalPrice }}
					</view>
				</view>
				<view class="remark">
					<input type="text" 
					placeholder="请输入备注"
					v-model="remark"/>
				</view>
			</view>
			
			
			<view class="uni-color-btn"
			@click="submit">
				确认下单
			</view>
		</view>




		<!-- 地址选择 -->
		<uv-popup ref="addressPopup" :round="30">
			<addressList ref="addressList" height="60vh" @select="selectAddress" />
		</uv-popup>

	</view>
</template>

<script>
	import addressList from '../components/address/addressList.vue'
	import { mapState } from 'vuex'
	export default {
		components: {
			addressList,
		},
		data() {
			return {
				productList: [],
				address: {
					name: '请选择地址',
					address: '',
				},
				addressTotal: 0,
				remark : '',
				num : 1,
			}
		},
		computed : {
			totalPrice(){
				let price = 0
				
				this.productList.forEach(n => {
					
					if([2].includes(n.type) &&
					[1].includes(this.userInfo.isPay)){
						// 常规、普通商品的同时用户是会员就计算会员价
						price += n.vipPrice * n.num
					}else{
						// 普通价格
						price += n.price * n.num
					}
				})
				
				return Number(price).toFixed(2)
			},
			...mapState(['payOrderProduct', 'userInfo']),
		},
		onLoad() {
			this.getRiceProductDetail()
			this.$store.commit('getUserInfo')
		},
		onShow() {
			this.getAddressList()
		},
		methods: {
			// 获取商品
			getRiceProductDetail() {
				this.productList = JSON.parse(JSON.stringify(this.payOrderProduct))
			},
			// 打开
			getAddressList() {
				// 获取地址列表
				this.$refs.addressList.getAddressList().then(res => {
					this.addressTotal = res.total
					if (this.addressTotal != 0) {
						this.address = res.records[0]
					}
				})
			},
			// 打开选择地址
			openAddress() {
				if (this.addressTotal == 0) {
					return uni.navigateTo({
						url: '/pages_order/mine/address?type=back'
					})
				}
				this.$refs.addressPopup.open('bottom')
			},
			// 选择地址
			selectAddress(e) {
				this.address = e
				this.$refs.addressPopup.close()
			},
			// 确认下单
			// submit(){
			// 	this.$api('createOrder', {
			// 		productId : this.productList[0].id,
			// 		num : 1,
			// 	}, res => {
			// 		if(res.code == 200){
			// 			uni.showToast({
			// 				title: '购买成功',
			// 				icon: 'none'
			// 			})
						
			// 			setInterval(uni.navigateTo, 1000, {
			// 				url : '/pages/index/order'
			// 			})
			// 		}
			// 	})
			// },
			submit(){
				
				let addressId = this.address.id
				
				if(!addressId){
					uni.showToast({
						title: '请选择地址',
						icon: 'none'
					})
					return
				}
				
				let data = {}
				let api = ''
				
				
				if(this.productList[0].shopId || this.productList[0].type == 2){//普通商品
				
					let list = []
					this.productList.forEach(n => {
						list.push({
							num : n.num,
							shopId : n.shopId || n.id,
						})
					})
					data = {
						addressId,
						list : JSON.stringify(list),
					}
					api = 'createSumOrder'
					
					this.deleteCart(this.productList.map(n => n.id).join(','))
					
				}else{//体验、常规商品
					data = {
						addressId,
						num : this.productList[0].num,
						shopId : this.productList[0].id,
					}
					api = 'createOrder'
				}
				
				this.$api(api, data, res => {
					if(res.code == 200){
						uni.requestPaymentWxPay(res)
						.then(res => {
							uni.showToast({
								title: '下单成功',
								icon: 'none'
							})
							
							setTimeout(uni.redirectTo, 700, {
								url: '/pages/index/order'
							})
						}).catch(n => {
							setTimeout(uni.redirectTo, 700, {
								url: '/pages/index/order'
							})
						})
					}else if(res.code == 902){
						uni.showModal({
							title: res.message,
							success(e) {
								if(e.confirm){
									uni.redirectTo({
										url: '/pages/index/order'
									})
								}
							}
						})
					}
				})
				
			},
			// 删除购物车
			deleteCart(ids){
				this.$api('deleteCart', {
					ids
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.page {
		.box {
			padding: 20rpx;

			.address {
				display: flex;
				padding: 20rpx;
				background-color: #fff;
				align-items: center;
				border-radius: 20rpx;

				image {
					width: 60rpx;
					height: 40rpx;
					margin: 20rpx;
				}

				view {
					margin: 20rpx;
					overflow: hidden; //超出的文本隐藏
					text-overflow: ellipsis; //溢出用省略号显示
					white-space: nowrap; //溢出不换行
				}

				.icon {
					margin-left: auto;
				}
			}

			.productList {
				margin-top: 20rpx;
				background-color: #fff;
				border-radius: 20rpx;
				.item {
					padding: 10rpx 20rpx;
					align-items: center;
					margin-bottom: 20rpx;
					display: flex;
					width: 100%;
					box-sizing: border-box;
					.item-image {
						width: 140rpx;
						height: 140rpx;
						flex-shrink: 0;

						image {
							height: 100%;
							width: 100%;
							border-radius: 20rpx;
						}
					}

					.info {
						padding: 20rpx;
						color: #555;
						flex: 1;

						.title {
							font-size: 28rpx;
							font-weight: 900;
						}

						.desc {
							font-size: 22rpx;
							color: #777;
							margin-top: 10rpx;
						}

						.price-box {
							display: flex;
							justify-content: space-between;
							color: #f40;
							font-size: 30rpx;
							font-weight: 900;
							.price{
								&>view:nth-child(2){
									font-size: 22rpx;
									color: #777;
									text{
										font-size: 26rpx;
										font-weight: 900;
									}
								}
							}
						}
					}
				}
			}
			
			.submit-box{
				background-color: #fff;
				padding: 20rpx;
				border-radius: 20rpx;
				&>view:nth-child(2){
					margin-top: 20rpx;
				}
				&>view{
					display: flex;
					justify-content: space-between;
					align-items: center;
					font-size: 28rpx;
					&>view:nth-child(1){
						font-weight: 900;
					}
					&>view:nth-child(2){
						color: #999;
						font-size: 24rpx;
					}
					.totalPrice{
						color: #f40 !important;
						font-size: 34rpx !important;
						font-weight: 900;
					}
				}
				.remark{
					margin-top: 30rpx;
					input{
						background-color: #f3f3f3;
						padding: 14rpx 20rpx;
						border-radius: 20rpx;
						flex: 1;
					}
				}
			}
		}
	}
</style>