<!-- +----------------------------------------------------------------------
| 麦沃德科技赋能开发者，助力商协会发展
+----------------------------------------------------------------------
| Copyright (c) 2017～2024  www.wdsxh.cn    All rights reserved.
+----------------------------------------------------------------------
| 沃德商协会系统并不是自由软件，不加密，并不代表开源，未经许可不可自由转售和商用
+----------------------------------------------------------------------
| Author: MY WORLD Team <bd@maiwd.cn>   www.maiwd.cn
+----------------------------------------------------------------------
| 商品详情 开发者: 麦沃德科技-半夏
+---------------------------------------------------------------------- -->

<template>
	<page-meta :page-style="'overflow:' + (pageShow ? 'hidden' : 'visible')"></page-meta>
	<view class="container" :style="{'--theme-color': themeColor}">
		<!-- 标题栏 -->
		<title-bar :showBack="true" title="商品详情"></title-bar>
		<!-- 内容区 -->
		<view class="container-main" v-if="loadEnd">
			<view class="main-carousel">
				<carousel :show-data="carouselList" height="660rpx" radius="0" right="32rpx" bottom="32rpx"></carousel>
			</view>
			<view class="main-info">
				<view class="info-title">{{goodsInfo.name}}</view>
				<view class="info-amount">{{goodsInfo.points}}积分</view>
			</view>
			<view class="main-content">
				<view class="content-title">商品详情</view>
				<mp-html :content="goodsInfo.content" />
			</view>
			<view class="main-footer">
				<view class="footer-btn" @click="toOrder()">立即兑换</view>
				<view class="safe-padding"></view>
			</view>
		</view>
		<!-- 选择数量弹窗 -->
		<quantity-modal ref="quantityModal" @confirm="changeQuantity" @onChange="pageChange"></quantity-modal>
	</view>
</template>

<script>
	import { mapState } from "vuex"
	// #ifdef H5
	import wx from 'weixin-js-sdk';
	// #endif
	import carousel from "@/pages/component/carousel/carousel.vue"
	import quantityModal from "@/pagesPoints/component/modal/quantity.vue"
	export default {
		components: {
			carousel,
			quantityModal,
		},
		data() {
			return {
				// 页面是否阻止滚动
				pageShow: false,
				// 是否加载完成
				loadEnd: false,
				// 商品Id 
				goodsId: null,
				// 商品详情
				goodsInfo: {},
				// 轮播图列表
				carouselList: [],
			}
		},
		computed: {
			...mapState({
				themeColor: state => state.app.themeColor,
			}),
		},
		onLoad(option) {
			this.goodsId = option.id
			uni.showLoading({
				title: "加载中"
			})
			this.getGoodsDetails(() => {
				uni.hideLoading()
				this.loadEnd = true
				// #ifdef H5
				this.initConfig()
				// #endif
			})
		},
		onShareAppMessage() {
			return {
				title: this.goodsInfo.name,
				path: '/pagesPoints/goods/details?id=' + this.goodsId,
				imageUrl: this.carouselList[0].image,
			}
		},
		onShareTimeline() {
			return {
				title: this.goodsInfo.name,
				path: '/pagesPoints/goods/details?id=' + this.goodsId,
				imageUrl: this.carouselList[0].image,
			}
		},
		methods: {
			// 改变页面滚动状态
			pageChange(state) {
				this.pageShow = state
			},
			// #ifdef H5
			// 微信公众号初始化方法
			initConfig() {
				this.$util.request("main.WeChatConfig", {
					url: location.href.split('#')[0]
				}).then(res => {
					if (res.code == 1) {
						wx.config({
							debug: false,
							appId: res.data.appId,
							timestamp: Number(res.data.timestamp),
							nonceStr: res.data.nonceStr,
							signature: res.data.signature,
							jsApiList: ["updateAppMessageShareData", "updateTimelineShareData", "wx-open-launch-weapp"],
							openTagList: ["updateAppMessageShareData", "updateTimelineShareData", 'wx-open-launch-weapp'],
						})
						wx.ready(() => {
							wx.updateAppMessageShareData({
								title: this.goodsInfo.name,
								desc: "",
								link: window.location.href,
								imgUrl: this.carouselList[0].image,
							});
							wx.updateTimelineShareData({
								title: this.goodsInfo.name,
								link: window.location.href,
								imgUrl: this.carouselList[0].image,
							});
						});
					} else {
						uni.hideLoading()
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					uni.hideLoading()
					console.error('通过config接口注入权限验证配置 ', error)
				})
			},
			// #endif
			// 获取商品详情
			getGoodsDetails(fn) {
				this.$util.request("points.goodsDetails", {
					id: this.goodsId
				}).then(res => {
					if (fn) fn()
					if (res.code == 1) {
						this.goodsInfo = res.data
						this.carouselList = this.splitImages(res.data.slider_images)
					} else {
						uni.showToast({
							title: res.msg,
							icon: 'none'
						})
					}
				}).catch(error => {
					if (fn) fn()
					console.error('获取商品详情', error)
				})
			},
			// 字符串转数组格式图片
			splitImages(images) {
				try {
					if (images) return images.split(',');
					else return []
				} catch (error) {
					return [];
				}
			},
			// 前往订单确认 
			toOrder() {
				if (uni.getStorageSync("token")) {
					this.$refs.quantityModal.open(1)
				} else {
					uni.navigateTo({
						url: "/pages/login/index",
						animationType: "fade-in"
					})
				}
			},
			// 改变选择的数量
			changeQuantity(number) {
				const mallOrder = {
					id: this.goodsInfo.id,
					name: this.goodsInfo.name,
					image: this.goodsInfo.image,
					points: this.goodsInfo.points,
					number
				}
				this.$store.commit("app/setPointsOrder", mallOrder)
				this.$util.toPage({
					mode: 1,
					path: "/pagesPoints/goods/order",
				})
			},
		}
	}
</script>

<style lang="scss">
	.container {
		.container-main {
			padding-bottom: 112rpx;

			.main-info {
				padding: 32rpx;
				background: #FFF;

				.info-title {
					color: #5A5B6E;
					font-size: 32rpx;
					font-weight: 600;
					line-height: 48rpx;
				}

				.info-amount {
					margin-top: 16rpx;
					color: var(--theme-color);
					font-size: 28rpx;
					font-weight: 600;
					line-height: 40rpx;
				}
			}

			.main-content {
				padding: 32rpx;
				background: #FFF;
				margin-top: 16rpx;

				.content-title {
					color: #5A5B6E;
					font-size: 28rpx;
					font-weight: 600;
					line-height: 44rpx;
					margin-bottom: 32rpx;
				}
			}

			.main-footer {
				position: fixed;
				left: 0;
				right: 0;
				bottom: 0;
				z-index: 96;
				background: #FFF;
				border-top: 1rpx solid #F6F7FB;
				padding: 16rpx 24rpx;

				.footer-btn {
					color: #FFF;
					text-align: center;
					font-size: 32rpx;
					line-height: 44rpx;
					padding: 22rpx 32rpx;
					border-radius: 16rpx;
					background: var(--theme-color);
				}
			}
		}
	}
</style>