<template>
	<mobile-frame>
		<view class="main">
			<view class="one">
				<scroll-view scroll-y="true" class="scroll-view">
					<view class="list-scroll-view">
						<view class="one_1" @tap="toChoose">
							<text class="localicon iconfont icon-dingweixiao"></text>
							<view class="other" v-if="address._id">
								<view class="name">
									<text>{{address.name}},</text>{{address.phone}}
								</view>
								<view class="other_1">
									<text>{{address.province}}</text><text>{{address.city}}</text>
									<text>{{address.area}}</text><text>{{address.address}}</text>
								</view>
							</view>
							<view class="address" v-else><text>请选择一个收货地址</text></view>
							<text class="iconfont icon-jiantouyou"></text>
						</view>
						<view class="one_2">
							<view class="list">
								<view class="list_1">
									<view class="l">
										<text class="iconfont icon-shangdian"></text>
										<text>{{info.shop.name}}</text>
									</view>
								</view>
								<view class="list_2">
									<view class="list_2_1">
										<view class="l">
											<image class="image"
												v-if="info.goodsSpec.file&&info.goodsSpec.file.length>0"
												:src="info.goodsSpec.file&&info.goodsSpec.file.length>0?info.goodsSpec.file[0].url:''"
												mode=""></image>
											<image class="image" v-else
												:src="info.goods.file&&info.goods.file.length>0?info.goods.file[0].url:''"
												mode=""></image>
										</view>
										<view class="c">
											<view class="name">
												{{info.goods.name}}
											</view>
											<view class="Spec">
												规格:{{info.goodsSpec.name}}
											</view>
										</view>
										<view class="r">
											<view class="price">
												¥{{info.goodsSpec.price}}
											</view>
											<view class="num">
												×{{info.num}}
											</view>
										</view>
									</view>
								</view>
								<view class="list_3">
									<view class="other">
										<view class="other_1">配送方式</view>
										<view class="other_2"><text class="iconfont icon-duihao"></text>快递配送</view>
									</view>
									<view class="other">
										<view class="other_1">运费</view>
										<view class="other_2" v-if="info.goodsSpec.freight!='0'">
											￥{{info.goodsSpec.freight}}
										</view>
										<view class="other_2" v-else>包邮</view>
									</view>
									<view class="other">
										<view class="other_1">订单备注</view>
										<view class="other_3">
											<input type="text" v-model="remarks" placeholder="选填,可填写您与卖家达成一致的要求" />
										</view>
									</view>
								</view>
							</view>
							<view class="other" v-for="(item,index) in total_detail" :key="index">
								<view class="other_1">{{item.zh}}</view>
								<view class="other_2">￥{{item.money}}</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="two">
				<view class="two_1">
					<view>实付金额:</view>
					<view>￥{{goods_total}}</view>
				</view>
				<view class="two_2">
					<text @tap="toSubmit">提交订单</text>
				</view>
			</view>
		</view>
	</mobile-frame>
</template>

<script>
	export default {
		data() {
			return {
				// 设备平台信息
				system: {},
				user: {},
				key: '',
				address: {},
				info: {},
				goods_total: 0,
				// 金额明细
				total_detail: [],
				// 收货地址
				addressList: [],
				// 备注
				remarks: ''
			};
		},
		onLoad: function(e) {
			const that = this;
			uni.$on("id", async (id) => {
				let arr = await that.$api(`/address/${id}`, `GET`)
				if (arr.errcode == '0') {
					that.$set(that, `address`, arr.data)
				}
			})
			that.$set(that, `key`, e.key || '');
			that.watchLogin()
		},
		methods: {
			// 监听用户是否登录
			watchLogin() {
				const that = this;
				uni.getStorage({
					key: 'token',
					success: function(res) {
						let user = that.$jwt(res.data);
						that.$set(that, `user`, user);
						// 设备平台信息
						let config = that.$config;
						that.$set(that, `system`, config.system);
						that.search()
					},
					fail: function(err) {
						uni.navigateTo({
							url: `/pages/login/index`
						})
					}
				})
			},
			// 查询列表
			async search() {
				const that = this;
				let user = that.user;
				const res = await that.$api(`/address`, 'GET', {
					customer: user._id
				})
				if (res.errcode == '0') that.$set(that, `addressList`, res.data.reverse());
				const arr = await that.$api(`/groupOrder/toOrderPage`, 'POST', {
					key: that.key
				}, 'group')
				if (arr.errcode == '0') {
					// 地址
					that.$set(that, `address`, arr.data.address);
					// 订单
					that.$set(that, `info`, arr.data);
					// 总价
					that.$set(that, `total_detail`, arr.data.total_detail);
					that.computedTotal();
				}
			},
			// 选择收货地址
			toChoose() {
				const that = this;
				if (that.address?._id) {
					uni.navigateTo({
						url: `/pagesMy/address/index?type=${"shopping"}&id=${that.address._id}`
					})
				} else {
					uni.navigateTo({
						url: `/pagesMy/address/index?type=${"shopping"}`
					})
				}
			},
			// 提交订单
			async toSubmit() {
				const that = this;
				let system = that.system;
				if (that.address) {
					let obj = {
						address: that.address,
						shop: that.info.shop._id,
						goods: that.info.goods._id,
						goodsSpec: that.info.goodsSpec._id,
						num: that.info.num,
						group: that.info.group,
						remarks: that.remarks
					}
					// 创建订单
					let p1 = await that.$api(`/groupOrder`, 'POST', obj, 'group');
					if (p1.errcode == '0') {
						if (system.uniPlatform == "mp-weixin") {
							// 微信支付
							uni.showLoading({
								title: '加载中'
							})
							// 支付信息
							let p2 = await that.$api('/orderDeal/pay', 'POST', {
								order_id: p1.data,
							}, 'group')
							if (p2.errcode == '0' && p2.data.paySign) {
								uni.requestPayment({
									"provider": "wxpay",
									...p2.data,
									success: async function(res) {
										uni.reLaunch({
											url: `/pagesMy/dough/index`
										})
									},
									fail: function(err) {
										uni.showToast({
											title: `支付失败`,
											icon: 'none'
										})
										uni.hideLoading();
										uni.reLaunch({
											url: `/pagesMy/dough/index`
										})
									}
								})
							} else {
								uni.showToast({
									title: p2.errmsg,
									icon: 'none'
								})
							}
						} else if (system.uniPlatform == "app") {
							uni.requestPayment({
								provider: 'alipay',
								orderInfo: 'orderInfo',
								success: function(res) {
									console.log('success:' + JSON.stringify(res));
								},
								fail: function(err) {
									console.log('fail:' + JSON.stringify(err));
								}
							});
						} else {
							uni.showToast({
								title: `平台不支持支付`,
								icon: 'none'
							})
						}
					} else {
						uni.showToast({
							title: p1.errmsg,
							icon: 'none'
						})
					}
				} else {
					uni.showToast({
						title: `没有收货地址`,
						icon: 'none'
					})
				}
			},
			// 计算优惠券
			async computedTotal() {
				const total_detail = this.total_detail;
				let total = 0;
				for (let val of total_detail) total = this.$plus(total, val.money)
				if (total >= 0) {
					this.$set(this, `goods_total`, total);
				} else {
					uni.showToast({
						title: `实付金额不能为负数 不能使用该优惠劵`,
						icon: 'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss">
	.main {
		display: flex;
		flex-direction: column;
		width: 100vw;
		height: 100vh;

		.one {
			position: relative;
			flex-grow: 1;
			background-color: var(--f1Color);

			.one_1 {
				display: flex;
				justify-content: space-between;
				align-items: center;
				width: 100vw;
				padding: 2vw;
				border-bottom: 1vw dashed var(--fcColor);

				.address {
					flex-grow: 1;
					margin: 0 0 0 2vw;
					font-size: var(--font14Size);
					color: var(--f85Color);
				}

				.iconfont {
					font-size: var(--font20Szie);
				}

				.other {
					width: 82vw;
					padding: 0 2vw;

					.name {
						font-size: var(--font16Size);
					}

					.other_1 {
						font-size: var(--font14Size);
						color: var(--f85Color);

						text {
							margin: 0 1vw 0 0;
						}
					}
				}
			}

			.one_2 {
				.list {
					width: 100vw;
					margin: 2vw 0;

					.list_1 {
						padding: 2vw;
						background-color: var(--mainColor);
						border-bottom: 0.1vw solid var(--fcColor);

						text {
							padding: 0 0 0 1vw;
						}
					}

					.list_2 {
						padding: 2vw;
						background-color: var(--mainColor);

						.list_2_1 {
							display: flex;

							.l {
								width: 20vw;

								.image {
									width: 100%;
									height: 20vw;
									border-radius: 5px;
								}
							}

							.c {
								width: 60vw;
								padding: 0 2vw;

								.Spec {
									font-size: var(--font12Size);
									color: var(--f85Color);
								}
							}

							.r {
								width: 15vw;
								text-align: right;
							}
						}

						.list_2_2 {
							padding: 0 1vw 1vw 0;
							font-size: var(--font14Size);
						}

						.list_2_3 {
							display: flex;
							justify-content: space-between;
							border: 1px solid var(--fcColor);
							border-radius: 5px;
							margin: 0 0 1vw 0;
							padding: 1vw;

							.left {
								.name {
									font-size: 14px;
								}

								.Spec {
									font-size: var(--font12Size);
									color: var(--f85Color);
								}
							}
						}
					}

					.list_3 {
						width: 96vw;
						padding: 2vw;
						background-color: var(--mainColor);

						.other {
							display: flex;
							justify-content: space-between;
							border-bottom: 0.1vw solid var(--fcColor);
							margin: 0 0 2vw 0;
							padding: 2vw 0;

							.other_1 {
								font-size: var(--font16Size);
								color: var(--f85Color);
							}

							.other_3 {
								flex-grow: 1;
								margin: 0 0 0 2vw;
								font-size: var(--font12Size);
							}

							text {
								padding: 0 1vw 0 0;
								font-size: var(--font20Szie);
							}
						}
					}
				}

				.other {
					display: flex;
					justify-content: space-between;
					background-color: var(--mainColor);
					border-bottom: 0.1vw solid var(--fcColor);
					width: 96vw;
					padding: 2vw;

					.other_1 {
						font-size: var(--font16Size);
						color: var(--f85Color);
					}

					.other_3 {
						flex-grow: 1;
						margin: 0 0 0 2vw;
						font-size: var(--font12Size);
					}

					.other_4 {
						color: var(--fFB1Color);
					}

					text {
						padding: 0 1vw 0 0;
						font-size: var(--font20Szie);
					}
				}
			}
		}

		.two {
			display: flex;
			justify-content: space-between;
			border-top: 0.1vw solid var(--fcColor);

			.two_1 {
				display: flex;
				align-items: center;
				padding: 0 2vw;
				font-size: var(--font16Size);

				view:last-child {
					margin: 0 0 0 2vw;
					color: var(--ff0Color);
					font-weight: bold;
				}
			}

			.two_2 {
				padding: 3vw 4vw;
				color: var(--mainColor);
				font-size: var(--font16Size);
				background-color: var(--ff0Color);
			}
		}
	}

	.scroll-view {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;

		.list-scroll-view {
			display: flex;
			flex-direction: row;
			flex-wrap: wrap;
		}
	}

	.popup {
		display: flex;
		flex-direction: column;
		height: 100vw;
		overflow-x: hidden;
		background-color: var(--f5Color);
		padding: 2vw;

		.list {
			.one {
				display: flex;
				justify-content: space-between;
				margin: 2vw 0 0 0;
				padding: 2vw;
				background-color: var(--mainColor);

				.name {
					font-size: var(--font16Size);
				}

				.address {
					font-size: var(--font14Size);
				}

				.iconfont {
					line-height: 15vw;
					font-size: var(--font20Szie);
				}
			}

			.two {
				display: flex;
				justify-content: space-between;
				border-top: 0.1vw solid var(--fcColor);
				padding: 2vw;
				background-color: var(--mainColor);
				font-size: var(--font14Size);

				.text {
					margin: 0 2vw 0 0;
					border: 0.1vw solid var(--fFB1Color);
					padding: 1vw;
					font-size: var(--font12Size);
					border-radius: 2vw;
					color: var(--fFB1Color);
				}
			}
		}
	}
</style>
