<template>
	<view class="bg">
		<image v-if="headImg" :src="showImg(headImg)" class="topImg" mode="widthFix"></image>
		
		<view class="main-container">
			<image class="title-image" src="https://static.ticket.sz-trip.com/uploads/20250815/36ddcb32b78b28ee6f6991ae9af8c52f.png"></image>
			<view class="coupon-box">
				<view class="rule" @click="goRule(1)">活动规则</view>
				<view class="coupon-tip">消费券领取后3天内未使用自动失效<br>失效后可再次领取</view>
				<view class="flex-between" style="flex-wrap: wrap;margin-top: 26rpx;">
					<image class="coupon-img" v-for="(item,i) in couponList" :key="i"
					:src="item.img" @click="getCoupon(item)" mode="aspectFill"></image>
				</view>
			</view>
			
			<image class="adv-image" mode="aspectFill" @click="gotoPath('/subPackages/activity/agriculturalProdNH')"
			src="https://static.ticket.sz-trip.com/uploads/20250815/3786f50e9baa674b850df7383fa6ed88.png"></image>
			
			<image class="title-image" style="margin-bottom: 30rpx;" src="https://static.ticket.sz-trip.com/uploads/20250821/e45546af63bafab92ac2e8783d7b079c.png"></image>
			
			<view class="row-product" v-for="(item,index) in list.slice(0,viewNum)" 
			:key="index" @click="gotoDetailByType(item)">
				<image class="img" :src="showImg(item.headimg)" mode="aspectFill"></image>
				<view class="content flex-column">
					<view class="title text-overflowRows">{{item.title}}</view>
					<view class="tags"  v-if="item.display_tags">
						<view class="tag" v-for="(tag,tagI) in item.display_tags.split(',')" :key="tagI">{{tag}}</view>
					</view>
					<view class="flex-between">
						<view class="price">{{item.price/100}}</view>
						<view class="btn">立即购买</view>
					</view>
				</view>
			</view>
			<view class="view-more" v-if="list.length>0&&viewNum<=3" @click="viewNum=999">查看更多</view>
			
			<view class="type-container">
				<view :class="['type-item',typeIndex==i?'active':'']" @click="typeIndex=i"
				v-for="(item,i) in typeList" :key="i">{{item.title}}</view>
			</view>
			<view class="flex-between" style="flex-wrap: wrap;">
				<view class="column-product"  @click="gotoDetailByType(item)" :key="index"
					v-for="(item,index) in typeList[typeIndex].list.slice(0,typeList[typeIndex].viewNum)">
					<image class="img" :src="showImg(item.headimg)" mode="aspectFill"></image>
					<view class="content flex-column">
						<view class="title text-overflow">{{item.title}}</view>
						<view class="tags"  v-if="item.display_tags">
							<view class="tag" v-for="(tag,tagI) in item.display_tags.split(',')" :key="tagI">{{tag}}</view>
						</view>
						<view class="flex-between">
							<view class="price">{{item.price/100}}</view>
							<view class="btn">购买</view>
						</view>
					</view>
					
				</view>
			
			
			</view>
			
			<view class="view-more" v-if="typeList[typeIndex].list.length>0&&typeList[typeIndex].viewNum<=4" @click="typeList[typeIndex].viewNum=999">查看更多</view>
				
			<image class="bottom-img" src="https://static.ticket.sz-trip.com/uploads/20250815/877b45bf01e0181fd489f0e1086c62fd.png"></image>
			<image @click="returnTop" v-show="showGoTop" class="back-img" src="https://static.ticket.sz-trip.com/uploads/20250815/108583b6b888b0e5556393b140a7cb84.png"></image>
			
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				list: [],
				viewNum: 3,
				headImg: '',
				couponList: [
					{id: null, img: "https://static.ticket.sz-trip.com/uploads/20250815/f41aea639d47410e6e2fb23fbc7061a4.png"},
					{id: null, img: "https://static.ticket.sz-trip.com/uploads/20250815/1373e0b00e33d21f951d992f01a54ca6.png"},
					{id: null, img: "https://static.ticket.sz-trip.com/uploads/20250815/0a0574f43a008c57a4ac13ad2695260a.png"},
					{id: null, img: "https://static.ticket.sz-trip.com/uploads/20250815/b37ab2354e27a715760c3c963a5da90f.png"},
				],
				isReceive: true,
				typeIndex: 0,
				typeList: [
					{id: 96,title: "当季鲜享",viewNum:4, list: []},
					{id: 97,title: "风味特产",viewNum:4, list: []},
					{id: 98,title: "入秋养生",viewNum:4, list: []},
					{id: 99,title: "啡尝美味",viewNum:4, list: []},
				],
				showGoTop: false,
			}
		},
		onLoad(option) {
			
		},
		onReady() {
			this.getHeadImg(2384)
			this.getGoods()
		},
		methods: {
			getHeadImg (id) {
				this.Post({id},'/api/multimedia/detail').then(res => {
					this.headImg = res.data.head_img
					uni.setNavigationBarTitle({
						title:res.data.title
					})
					if (res.data.company_name) {
						res.data.company_name.split(',').forEach((item, index) => {
							this.couponList[index].id = item
						})
					}
				});	
			},
			goRule (id) {
				uni.navigateTo({
					url:"/subPackages/activity/commonRule?id="+id
				})
			},
			
			// 产品列表
			getGoods() {
				this.Post({
					tag_id: 95,
					offset: 0,
				    limit: 999,
				},'/api/product/get_product_by_tag_subject').then(res => {
					if (res.data.length < this.viewNum) {
						this.viewNum = 999
					}
					this.list = res.data.list
				})
			
				for(let p of this.typeList) {
					this.Post({
						tag_id: p.id,
						offset: 0,
					    limit: 999,
					},'/api/product/get_product_by_tag_subject').then(res => {
						if (res.data.list.length < p.viewNum) {
							p.viewNum = 999
						}
						p.list = res.data.list
						console.log(p)
					})
				}
			
			},
			getCoupon(item) {
				if (!this.isReceive) {
					uni.showToast({
						title: "短时间内请勿重复点击！",
						icon: 'none'
					})
					return;
				}
				this.isReceive = false
				setTimeout(() => {this.isReceive = true}, 3000)
				this.Post({
					ids: item.id,
					is_all: 1,
				},"/api/coupon/getNewCouponsByActivitiesIds").then(res => {
					if (res) {
						uni.showToast({
							title: res.data,
							icon: 'none'
						})
					}
				})
			},
			

			viewDetail(item) {
				this.gotoDetailByType(item)
			},
			returnTop(){
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 200,
				})
			},
		},
		onPageScroll(res) {
			this.showGoTop = res.scrollTop > 200 ? true : false;
		}
	}
</script>

<style lang="scss" scoped>
	.bg {
		width: 750rpx;
		min-height: 100vh;
		background: #E9F5EC;
		padding-bottom: 60rpx;
	}
	
	.topImg {
		width: 100%;
	}
	.main-container{
		width: 100%;
		margin-top: -10rpx;
		padding: 0 26rpx 60rpx;
		.title-image{
			width: 417.33rpx;
			height: 66.67rpx;
			margin: 0 auto;
			display: block;
			position: relative;
			z-index: 10;
		}
		.adv-image{
			margin: 47rpx 0 57rpx;
			width: 100%;
			height: 200rpx;
		}
	}
	.coupon-box{
		height: 529rpx;
		background: #FFFFFF;
		border-radius: 13rpx;
		border: 1px solid #629FAE;
		position: relative;
		padding: 60rpx 18rpx 0;
		margin-top: -33rpx;
		overflow: hidden;
		.rule{
			width: 66rpx;
			height: 64rpx;
			background: #4C9BB4;
			border-radius: 0rpx 2rpx 0rpx 13rpx;
			font-weight: 500;
			font-size: 21rpx;
			color: #FFFFFF;
			text-align: center;
			padding:0 10rpx;
			position: absolute;
			top: 0;
			right: 0;
			display: flex;
			align-items: center;
			justify-content: center;
			line-height: 1.2;
		}
		.coupon-tip{
			font-weight: 500;
			font-size: 24rpx;
			color: #F13E35;
			text-align: center;
		}
		.coupon-img{
			width: 323.33rpx;
			height: 171.33rpx;
			margin-bottom: 15rpx;
		}
	}
	
	.row-product{
		width: 100%;
		height: 227rpx;
		background: #FFFFFF;
		border-radius: 8rpx;
		padding: 6rpx;
		display: flex;
		margin-bottom: 22rpx;
		.img{
			width: 213rpx;
			height: 100%;
			border-radius: 7rpx;
			flex-shrink: 0;
		}
		.content{
			width: 100rpx;
			flex: 1;
			justify-content: space-between;
			padding: 6rpx 0 6rpx 22rpx;
		}
		.price{
			font-weight: bold;
			font-size: 40rpx;
			color: #F13E35;
			&::before{
				content: "￥";
				font-size: 24rpx;
			}
			&::after{
				content: "起";
				font-size: 24rpx;
			}
		}
		.btn{
			width: 164rpx;
			height: 50rpx;
			background: #FF9838;
			border-radius: 25rpx;
			font-weight: bold;
			font-size: 27rpx;
			color: #FFFFFF;
			line-height: 50rpx;
			text-align: center;
		}
	}
	.title{
		font-weight: bold;
		font-size: 29rpx;
		color: #000000;
	}
	.tags{
		width: 100%;
		display: flex;
		overflow: hidden;
		.tag{
			font-weight: 500;
			font-size: 24rpx;
			height: 38rpx;
			line-height: 38rpx;
			color: #4C9BB4;
			padding: 0rpx 6rpx;
			border-radius: 5rpx;
			border: 1px solid #4C9BB4;
			margin-right: 13rpx;
		}
	}
	.view-more{
		width: 227rpx;
		height: 49rpx;
		border-radius: 8rpx;
		border: 1px solid #4C9BB4;
		line-height: 49rpx;
		font-weight: 500;
		font-size: 25rpx;
		color: #4C9BB4;
		text-align: center;
		margin: 40rpx auto 0;
	}

	.type-container{
		display: flex;
		justify-content: space-between;
		margin: 52rpx 0 30rpx;
		.type-item{
			width: 164rpx;
			height: 66.67rpx;
			text-align: center;
			line-height: 66.67rpx;
			font-weight: bold;
			font-size: 31rpx;
			color: #4C9BB4;
			background-image: url("https://static.ticket.sz-trip.com/uploads/20250815/e165c4614ae82351d8b96f4dd0ab947c.png");
			background-size: 100% 100%;
			&.active{
				color: #FFFFFF;
				background-image: url("https://static.ticket.sz-trip.com/uploads/20250815/dba0ff60f303c031420958d67fa36614.png");
			}
		}
	}

	.column-product{
		width: 340rpx;
		height: 497rpx;
		background: #FFFFFF;
		border-radius: 20rpx;
		padding: 6rpx;
		overflow: hidden;
		margin-bottom: 16rpx;
		.img{
			width: 327rpx;
			height: 327rpx;
			border-radius: 13rpx 13rpx 0rpx 0rpx;
		}
		.content{
			width: 100%;
			flex: 1;
			height: 150rpx;
			justify-content: space-between;
			padding: 4rpx 10rpx;
		}
		.price{
			font-weight: bold;
			font-size: 28rpx;
			color: #E62525;
			&::before{
				content: "￥";
			}
		}
		.btn{
			width: 100rpx;
			height: 40rpx;
			background: #FF9838;
			border-radius: 20rpx;
			font-weight: bold;
			font-size: 27rpx;
			color: #FFFFFF;
			line-height: 40rpx;
			text-align: center;
		}
	}
	
	.bottom-img{
		width: 174.67rpx;
		height: 78rpx;
		margin: 74rpx auto 0;
		display: block;
	}
	.back-img{
		position: fixed;
		width: 66rpx;
		height: 66rpx;
		bottom: 66rpx;
		right: 26rpx;
	}
</style>