<template>
	<view :class="['page-container' ,animationClass,'page-style']">
		<view class="temp-custom-navbar" :style="{'height': totalHeight+'px','padding-top': statusBarHeight+'px'}">
			<view @click="doBack" class="icon-arrow-white-left-line size-64"></view>
			<view class="temp-navbar-title">我要买</view>
		</view>
		<view v-if="goods != null" class="detail-box">
			<image @click="doPreImg" :data-url="goods.goodsPhoto" :src="goods.goodsPhoto" mode="scaleToFill"
				class="detail-img"></image>
			<view class="bottom-fixed-footer col-white">
				<view class="goods-name">{{goods.goodsName}}</view>
				<scroll-view scroll-y style="height: 140rpx;margin-top: 8rpx;">
					<view>
						<view style="font-size: 24rpx;">{{goods.goodsDescription}}</view>
					</view>
				</scroll-view>
				<view class="goods-info-box mt-20">
					<rich-text class="goods-price" :nodes="moneyTxt(12,16,goods.goodsOpenPrice)"></rich-text>
					<view style="font-size: 28rpx;">上架:{{goods.goodsStatusTime}}</view>
				</view>
				<view class="deal-box mt-20" style="margin-bottom: 60rpx;">
					<view class="deal-box" @click="doChangeDeal">
						<checkbox style="margin-top: -6rpx;" :checked="isagree">
						</checkbox>
						<view>我同意平台</view>
					</view>
					<view class="deal-link" @click="doShowDeal">《购买协议》</view>
				</view>
				<view :class="['bottom-btn-green', isagree? '':'active']" @click="doConfirm">立即购买</view>
			</view>
		</view>
		<uni-popup type="message" ref="msg">
			<uni-popup-message :isTop="false" :duration="2000" :type="msgType" :message="msgHint"></uni-popup-message>
		</uni-popup>
	</view>
</template>

<script>
	import Shop from '@/common/js/net/shop.js'
	import {
		sImgPrefix,
	} from '@/common/js/net/mainUrl.js'
	import {
		moneyTxt
	} from '@/common/js/conver.js'
	export default {
		setup() {},
		data() {
			return {
				animationClass: '',
				totalHeight: 50,
				statusBarHeight: 24,
				id: '',
				msgType: 'info',
				msgHint: '',
				msgShow: false,
				goods: null,
				isagree: false
			};
		},
		onLoad(options) {
			const id = options.id
			if (id != '') {
				this.id = id
				this.doGetDetail()
			} else {
				this.setData({
					msgType: 'error',
					msgHint: '数据有误,请刷新重试',
					msgShow: true
				})
				this.msgType = 'error'
				this.msgHint = '数据有误,请刷新重试'
				this.$refs.msg.open()
				setTimeout(() => {
					uni.navigateBack()
				}, 1200);
			}
			this.countBarHeight()
		},
		onShow() {
			this.animationClass = 'fade-in'
		},
		onHide() {
			this.animationClass = 'fade-out'
		},

		methods: {
			moneyTxt,
			//计算状态栏等高度
			countBarHeight() {
				//#ifdef MP-WEIXIN || H5
				const windowInfo = uni.getWindowInfo()
				const deviceInfo = uni.getDeviceInfo()
				this.statusBarHeight = windowInfo.statusBarHeight
				this.totalHeight = deviceInfo.osName.toLowerCase() == 'ios' ? 48 : 50 // 导航栏总高度
				//#endif
				//#ifdef MP-ALIPAY ||MP-BAIDU||MP-TOUTIAO || MP-KUAISHOU || MP-XHS
				const deviceInfo = uni.getSystemInfoSync()
				console.log('设备信息', deviceInfo);
				this.statusBarHeight = deviceInfo.statusBarHeight
				this.totalHeight = deviceInfo.osName.toLowerCase() == 'ios' ? 48 : 50 // 导航栏总高度
				console.log('系统平台', deviceInfo.osName);
				//#endif
			},
			doBack() {
				uni.navigateBack()
			},
			doPreImg(e) {
				const url = e.currentTarget.dataset.url
				uni.previewImage({
					urls: [url],
				})
			},
			doChangeDeal() {
				this.isagree = !this.isagree
			},
			//显示购买协议
			doShowDeal() {
				uni.navigateTo({
					url: '/pages/common/rule/rule?id=db805bf2-eef9-47d5-941b-cdc3b83ec316&needToken=true',
				})
			},
			//提交购买
			doConfirm() {
				if (this.isagree) {
					this.doSaveOrder()
				} else {
					this.msgHint = '请阅读并同意购买协议'
					this.msgType = 'error'
					this.$refs.msg.open()
				}
			},
			async doSaveOrder() {
				try {
					uni.showLoading({
						title: '购买中..',
					});

					const res = await Shop.doSaveOrder(this.id);
					uni.hideLoading();

					if (res && res.data != '') {
						await this.doConfirmOrder(res.data);
					} else {
						this.msgHint = '网络错误,请稍后重试'
						this.msgType = 'error'
						this.$refs.msg.open()
					}
				} catch (err) {
					uni.hideLoading();
					this.msgHint = err.msg ? err.msg : '网络错误,请稍后重试'
					this.msgType = 'error'
					this.$refs.msg.open()
				}
			},

			async doConfirmOrder(id) {
				try {
					uni.showLoading({
						title: '购买中...',
					});

					await Shop.doConfirmOrder(id);
					uni.hideLoading();
					this.msgHint = '购买成功'
					this.msgType = 'success'
					this.$refs.msg.open()
					setTimeout(() => {
						this.backPageRefresh()
					}, 1500);
				} catch (err) {
					uni.hideLoading();
					this.msgHint = err.msg ? err.msg : '网络错误,请稍后重试'
					this.msgType = 'error'
					this.$refs.msg.open()
					this.upPagerefresh()
				}
			},
			doGetDetail() {
				const that = this
				uni.showLoading({
					title: '加载中...',
				})
				Shop.doGetGoodsDetail(that.id)
					.then(res => {
						uni.hideLoading()
						console.log(res)
						if (res.goodsPhoto && res.goodsPhoto != '') {
							res.goodsPhoto = sImgPrefix + res.goodsPhoto
							this.goods = res
						} else {
							that.msgHint = '网络错误,请稍后重试'
							that.msgType = 'error'
							that.$refs.msg.open()
							setTimeout(() => {
								uni.navigateBack()
							}, 1200);
						}
					})
					.catch(err => {
						uni.hideLoading()
						that.msgHint = err.msg ? err.msg : '网络错误,请稍后重试'
						that.msgType = 'error'
						that.$refs.msg.open()
						setTimeout(() => {
							uni.navigateBack()
						}, 1200);
					})
			},
			backPageRefresh: function() {
				var pages = getCurrentPages();
				var beforePage = pages[pages.length - 2];
				beforePage.$vm.needRefresh=true
				uni.navigateBack();
			},
			upPagerefresh(){
				var pages= getCurrentPages()
				var beforePage= pages[pages.length-2]
				beforePage.$vm.needRefresh= true
			}
		},
	};
</script>

<style lang="scss" scoped>
	.page-style {
		height: 100vh;
		background-color: $black-tr-color;
		padding: 0rpx 30rpx;
	}

	.fade-in {
		animation: fadeIn 0.5s;
	}

	.fade-out {
		animation: fadeOut 0.5s;
	}

	@keyframes fadeIn {
		from {
			opacity: 0;
		}

		to {
			opacity: 1;
		}
	}

	@keyframes fadeOut {
		from {
			opacity: 1;
		}

		to {
			opacity: 0;
		}
	}

	.temp-custom-navbar {
		display: flex;
		flex-direction: row;
		align-items: center;
		background-color: transparent;
		position: relative;
	}

	/* 标题 */
	.temp-navbar-title {
		font-size: 32rpx;
		font-weight: bold;
		position: absolute;
		left: 50%;
		transform: translateX(-50%);
		color: $white-color;
	}

	.temp-navbar-back {
		color: $white-color;
	}

	.detail-box {
		display: flex;
		flex-direction: column;
		align-items: center;
	}

	.detail-img {
		width: 80%;
		height: 50vh;
		margin-top: 30rpx;
	}

	.goods-name {
		font-size: 32rpx;
		font-weight: bold;
	}

	.goods-info-box {
		display: flex;
		flex-direction: row;
		align-items: center;
		justify-content: space-between;
	}

	.deal-box {
		display: flex;
		flex-direction: row;
		align-items: center;
	}

	.goods-price {
		font-size: 42rpx;
		font-weight: bold;
	}

	.deal-link {
		color: $primary-color;
	}

	.col-white {
		background-color: $black-tr-color;
		color: $white-color;
	}

</style>