<template>
	<view class="whole">
		<view class="oneBox" @click="toGroups()" v-if="shareInfo.group_goods">
			<image class="img" :src="shareInfo.group_goods.face_img" mode="aspectFill"></image>
			<view class="box">
				<view class="tit">{{shareInfo.group_goods.title}}</view>
				<view class="text">{{shareInfo.group_goods.visitor}}人看过 | {{shareInfo.group_goods.order_count}}人跟团</view>
				<view class="flex1">
					<view class="price" v-if="shareInfo.group_goods.min_price == shareInfo.group_goods.max_price">￥{{shareInfo.group_goods.min_price}}</view>
					<view class="price" v-else>￥{{shareInfo.group_goods.min_price}}-{{shareInfo.group_goods.max_price}}</view>
					<view class="buy flex">立即跟团</view>
				</view>
			</view>
		</view>
		
		<view class="twoBox mar24">
			<view class="topBox" v-if="shareInfo.user">
				<image :src="shareInfo.user.avatar"></image>
				<view class="right">
					<view class="name">{{shareInfo.user.nickname}}</view>
					<view class="time">{{shareInfo.created_at}}</view>
				</view>
			</view>
			<view class="desc">{{shareInfo.comment}}</view>
			<view class="pic" v-if="shareInfo.material && shareInfo.material.length !== 0">
				<swiper class="swiper" :autoplay="false" :indicator-dots="true" :circular="false">
					<swiper-item v-for="(it, index) in shareInfo.material" :key="index">
						<view class="swiper_imgs">
							<image :src="it.url" v-if="it.type === 1" @click="preView(it, shareInfo.material)" mode="aspectFill"></image>
							<template v-else>
								<image :src="it.img_video" mode="aspectFill"></image>
								<view class="play" @click="preView(it)"><up-icon name="play-right-fill" color="#fff" size="40" /></view>
							</template>
						</view>
					</swiper-item>
				</swiper>
			</view>
		</view>
		
		<view class="threeBox mar24" v-if="total > 1">
			<view class="title">其他团员也在说({{total}})</view>
			<view class="cont">
				<view class="twoBox border" v-for="item in infoList" :key="item.id">
					<view class="topBox">
						<image :src="item.user.avatar"></image>
						<view class="right">
							<view class="name">{{item.user.nickname}}</view>
							<view class="time">{{item.date}}</view>
						</view>
					</view>
					<view class="desc">{{item.comment}}</view>
					<view class="box_zong" v-if="item.material.length">
						<view class="box_imgs" v-for="(it, index) in item.material" :key="index">
							<image :src="it.url" v-if="it.type === 1" @click="preView(it, item.material)" mode="aspectFill"></image>
							<template v-else>
								<image :src="it.img_video" mode="aspectFill"></image>
								<view class="play" @click="preView(it)"><up-icon name="play-right-fill" color="#fff" size="40" /></view>
							</template>
						</view>
					</view>
				</view>
			</view>
		</view>
		
		<view class="btnBox"><button class="btn flex" @click="handleShare()">分享本条素材</button></view>
		
		<video-dialog :url="videoUrl" :show="showVideo" @close="showVideo = false"></video-dialog>
		
	</view>
	
</template>

<script>
	import { reactive, toRefs } from 'vue'
	import { get, post } from '@/api/request.js'
	import { Style } from '@/utils/list.js'
	import videoDialog from '@/components/videoDialog/index.vue'
	import { uniShare } from '@/components/common.js'
	
	export default {
		components: {
			videoDialog
		},
		setup() {
			const data = reactive({
				id: '',
				group_id: '',
				shareInfo: {},
				infoList: [],
				page: 1,
				lastPage: 0,
				Color: '',
				priceColor: '',
				total: 0,
				showVideo: false,
				imageUrl: 'https://ct-upimg.yx090.com/g.ii090/images/sprite/recommended.jpg',
				videoUrl: '',
			})
			
			const getShareInfo = async () => {
				await get(`/api/v1/orderComment/recommendDetail/` + data.id).then((res) => {
					data.shareInfo = res
					data.group_id = res.shop_group_goods_id
				})
			}
			
			const fetchList = () => {
				get(`/api/v1/goods/comment/${data.group_id}`, {
					page: data.page,
					pageSize: 20,
					type: 1
				}).then((res) => {
					data.infoList = data.infoList.concat(res.data)
					data.lastPage = res.meta.last_page
					data.total = res.meta.total
				})
			}
			
			const toGroups = () => {
				uni.navigateTo({
					url: '/pages/groups/index?id=' + data.group_id
				})
			}
			
			const preView = (it, imgs) => {
				var img = []
				imgs && imgs.forEach((res) => {
					if (res.type == 1) {
						img.push(res.url)
					}
				})
				if (it.type === 1) {
					uni.previewImage({
						current: it.url,
						urls: img
					})
				} else {
					data.showVideo = true
					data.videoUrl = it.url
				}
			}
			
			function handleShare() {
				let title = '本团已收到多条好评，快来看看吧！！！'
				let path = 'pages/mine/other/grassShare?id=' + data.id
				uniShare(title, data.imageUrl, path)
			}
			
			return {
				uniShare,
				...toRefs(data),
				preView,
				fetchList,
				toGroups,
				getShareInfo,
				handleShare
			}
		},
		async onLoad(options) {
			this.id = options.id || 0
			await this.getShareInfo()
			this.fetchList()
			this.Color = uni.getStorageSync('theme_color')
			this.priceColor = Style[uni.getStorageSync('theme_index') * 1].priceColor
		},
		onReachBottom() {
			if (this.page < this.lastPage) {
				this.page ++
				this.fetchList()
			}
		}
	}
</script>

<style lang="scss" scoped>
	.flex{
		display: flex;
		align-items: center;
		justify-content: center;
	}
	.flex1{
		display: flex;
		align-items: center;
		justify-content: space-between;
	}
	.mar24{
		margin-top: 24rpx;
	}
	.whole{
		padding-bottom: 140rpx;
		.oneBox{
			padding: 24rpx;
			background-color: #fff;
			border-radius: 10rpx;
			display: flex;
			justify-content: space-between;
			position: sticky;
			top: 0;
			z-index: 2;
			.img{
				width: 120rpx;
				height: 120rpx;
				object-fit: cover;
			}
			.box{
				padding: 0 0 0 16rpx;
				box-sizing: border-box;
				flex: 1;
			}
			.tit{
				font-size: 28rpx;
				overflow: hidden;
				text-overflow: ellipsis;
				display: -webkit-box;
				-webkit-line-clamp: 2;
				-webkit-box-orient: vertical;
				white-space: break-spaces;
			}
			.text{
				font-size: 24rpx;
				margin: 6rpx 0 0;
				color: #999;
			}
			.price{
				color: v-bind('priceColor');
				font-size: 30rpx;
				font-weight: 600;
			}
			.buy{
				color: #fff;
				font-size: 26rpx;
				width: 150rpx;
				height: 54rpx;
				background: v-bind('Color');
				border-radius: 10rpx;
			}
		}
		.twoBox{
			background-color: #fff;
			box-sizing: border-box;
			padding: 24rpx;
			.topBox{
				display: flex;
				image{
					width: 70rpx;
					height: 70rpx;
					border-radius: 8rpx;
					background: #666;
					margin-right: 20rpx;
				}
				.right{
					width: calc(100% - 90rpx);
					.name{
						font-size: 28rpx;
						color: #333;
						line-height: 1;
					}
					.time{
						font-size: 24rpx;
						color: #999;
						margin-top: 14rpx;
					}
				}
			}
			.desc{
				font-size: 28rpx;
				line-height: 48rpx;
				margin-top: 14rpx;
			}
			.pic{
				margin-top: 14rpx;
				image{
					width: 100%;
					height: 100%;
					vertical-align: bottom;
					border-radius: 8rpx;
				}
			}
		}
		.bottom {
			text-align: center;
			font-size: 24rpx;
			color: #999999;
			margin: auto;
			line-height: 80rpx;
		}
		.threeBox{
			background-color: #fff;
			.title{
				font-size: 32rpx;
				font-weight: bold;
				padding: 30rpx 24rpx;
				border-bottom: 1px solid #F7F7F7;
			}
			.cont{
				.box_zong{
					display: flex;
					flex-wrap: wrap;
					.box_imgs{
						margin: 20rpx 20rpx 0 0;
						position: relative;
						width: calc(33% - 13rpx);
						height: 220rpx;
						&:nth-child(3n+3) {
							margin-right: 0;
						}
						image{
							width: 100%;
							height: 100%;
							vertical-align: bottom;
						}
						.play{
							position: absolute;
							left: 0;
							top: 0;
							background-color: rgba(0, 0, 0, 0.4);
							display: flex;
							align-items: center;
							justify-content: center;
							width: 100%;
							height: 100%;
							z-index: 1;
						}
					}
				}
			}
		}
		.border{
			border-bottom: 1px solid #F7F7F7;
		}
		.btnBox{
			padding: 20rpx 24rpx;
			box-sizing: border-box;
			position: fixed;
			bottom: 0;
			width: 100%;
			z-index: 10;
			background-color: #fff;
			.btn{
				font-size: 28rpx;
				color: #fff;
				background-color: v-bind('Color');
				width: 100%;
				height: 80rpx;
				border-radius: 10rpx;
				z-index: 10;
				box-sizing: border-box;
			}
		}
	}
	.swiper{
		width: 100%;
		height: 340px;
	}
	.swiper_imgs{
		position: relative;
		width: 100%;
		height: 100%;
		image{
			width: 100%;
			height: 100%;
			vertical-align: bottom;
		}
		.play{
			position: absolute;
			left: 0;
			top: 0;
			background-color: rgba(0, 0, 0, 0.4);
			display: flex;
			align-items: center;
			justify-content: center;
			width: 100%;
			height: 100%;
			z-index: 1;
		}
	}
</style>
