<template>
	<view class="content">
		<view class="info">

			<image class="info-bg" src="/static/images/sales_bg.png"></image>
			<image class="info-title" src="/static/images/sales_title.png"></image>
			<view class="status_bar" :style="{height: statusBarHeight}"></view>
			<view class="search-box">
				<view class="back-box">
					<image class="img" src="/static/images/back_white_icon.png"></image>
				</view>
			</view>
		</view>
		<!-- 产品列表 -->
		<view class="shop-block">
			<view class="tab-inner" v-if="activeTab === 0">
				<view v-for="(item,index) in selectList" :key="index"
					:class="activeIndex == item.value?'item active':'item'" @click="onSelect(item)">
					<view class="text">
						<text>{{ item.name }}</text>
						</image>
					</view>
				</view>
			</view>
			<!-- 视频文章列表 -->
			<mescroll-body bottom="0" ref="mescrollRef" @init="mescrollInit" @down="downCallback" @up="upCallback"
				:down="downOption" :up="upOption">
				<view class="medic-list">
					<view v-for="(item,index) in dataList" :key="index" class="item" @click="showDetail(item)">
						<view class="img-box">
							<view class="label" :class="[index < 3 ? ['bg1', 'bg2', 'bg3'][index] : 'bg']">
								{{ index + 1 }}</view>
							<image :src=" item.image" mode="aspectFit">
							</image>
							<view class="sales-label">已售{{item.sales}}+</view>
						</view>
						<view class="info-box">
							<view class="hot-sale">
								<image class="w20 h20 ml10 mr8" src="/static/images/hot.png"></image>
								<text>今日热销1545件</text>
							</view>
							<view class="title ellipsis2">{{item.productName}}</view>
							<view class="sale">肌肉酸疼、扭伤拉伤</view>
							<view class="lable-group">
								<view class="lable-item">9.5折</view>
								<view class="lable-item">限购1份</view>
							</view>
							<view class="prce-num">
								<view class="price">
									<text class="unit">¥</text><text
										class="bold">{{splitPrice(item.price).integer}}</text>.{{splitPrice(item.price).decimal}}
								</view>
								<view class="cart-img" @click="navgetTo('../shopping/cart')">
									<image class="w40 h40" src="/static/images/add_car.png"></image>
								</view>
							</view>
						</view>
					</view>
				</view>
			</mescroll-body>
		</view>
	</view>
</template>

<script>
	import {
		liveList
	} from '@/api/living.js'
	import MescrollMixin from "@/uni_modules/mescroll-uni/components/mescroll-uni/mescroll-mixins.js";
	export default {
		mixins: [MescrollMixin],
		data() {
			return {
				isFollow: false, //关注
				activeIndex: 0,
				selectList: [{
					name: '全部',
					value: 0
				}, {
					name: '健康新品',
					value: 1
				}, {
					name: '营养保健',
					value: 2
				}, {
					name: '健康滋补',
					value: 3
				}, {
					name: '天然食品',
					value: 4
				}],
				activeTab: 0,
				// 状态栏的高度
				statusBarHeight: uni.getStorageSync('menuInfo').statusBarHeight,
				list: [],
				downOption: {
					offset: 80,
					use: true,
					auto: false
				},
				upOption: {
					use: true,
					auto: true,
					page: {
						num: 0,
						size: 10
					}
				},
				mescroll: null,
				dataList: [{
					cateId: 364,
					image: "https://hos-1309931967.cos.ap-chongqing.myqcloud.com/fs/20251216/6a3c96bd3fc44b15b760871670302e9a.jpg",
					otPrice: 268,
					price: 98,
					productId: 66050,
					productInfo: "电肌肉刺激(EMS)，又称神经肌肉电刺激，是利用电脉冲诱发多发性肌肉收缩；中枢神经系统使肌肉收缩；EMS被证明是一种积极的肌肉强化工具。↵EMS的用途：肌肉强化，预防废肌萎缩，肌肉松弛，缓解酸痛，放松肌肉，美容肌肉调理。",
					productName: "佟络宝®健步器",
					sales: 1026,
					unitName: "个",
					warehouseCode: "JST001",
					warehouseId: 3,
				}, {
					cateId: 364,
					image: "https://hos-1309931967.cos.ap-chongqing.myqcloud.com/fs/20251204/c53bb29cf794468ba59ee2d60983b92f.jpg",
					otPrice: 298,
					price: 138,
					productId: 65920,
					productInfo: "1、取本品1包放入脚盆，倒入适量沸水浸泡搅拌，约5-10分钟，再加入适量温水或冷水调至适宜水温即可足浴，泡脚15- 25分钟。↵2、取本品1包放入锅内煮约5分钟，将煮好的水倒入脚盆，再加入适量温水或冷水调至适宜温度即可足浴，泡脚15-25分钟。",
					productName: "[九仙汤]除痹通络汤 30克x30包",
					sales: 6042,
					unitName: "盒",
					warehouseCode: "JST001",
					warehouseId: 3,
				}],
			}
		},
		onLoad() {

		},
		onUnload() {

		},
		methods: {
			// 一键到顶
			scrollToTop() {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 300 // 动画时间，单位ms
				});
			},
			onSelect(item) {
				console.log("item", item)
				this.activeIndex = item.value
			},
			splitPrice(price) {
				const priceStr = parseFloat(price).toFixed(2).toString();
				return {
					integer: priceStr.split('.')[0],
					decimal: priceStr.split('.')[1]
				};
			},
			switchTab(tab) {
				if (this.activeTab === tab) return;
				this.activeTab = tab;
				this.$emit('change', tab);
			},
			mescrollInit(mescroll) {
				this.mescroll = mescroll;
			},

			// 下拉刷新回调
			downCallback(mescroll) {
				this.list = [];
				mescroll.resetUpScroll();
			},

			// 上拉加载回调
			upCallback(mescroll) {
				const pageNum = mescroll.num;
				const pageSize = mescroll.size;

				let data = {
					pageSize: pageSize,
					pageNum: pageNum,
				}
				liveList(data).then(res => {
					if (!res) {
						mescroll.endErr();
						return;
					}
					if (res.code == 200) {
						let curPageData = Array.isArray(res.data.list) ? res.data.list : [];
						let totalSize = Number(res.data.total) || 0;
						if (pageNum === 1) {
							this.list = [];
						}
						this.list = this.list.concat(curPageData);

						mescroll.endBySize(curPageData.length, totalSize);
					} else {
						mescroll.endErr();
						uni.showToast({
							title: res.msg,
							icon: 'none'
						});
					}
				}).catch(err => {
					mescroll.endErr();
				});
			},

			goLive(item) {
				uni.navigateTo({
					// &immediate=true
					url: `./living?liveId=${item.liveId}`
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		min-height: 100vh;
		background: #F5F7FA;

		.info {
			width: 100%;
			height: 454rpx;
			position: relative;
			z-index: 0;

			.info-bg {
				position: absolute;
				z-index: -1;
				width: 100%;
				height: 100%;
			}

			.info-title {
				width: 560rpx;
				height: 176rpx;
				position: absolute;
				bottom: 68rpx;
				left: 50%;
				transform: translateX(-50%);
			}

			.search-box {
				.back-box {
					height: 88rpx;
					display: flex;
					align-items: center;
					margin-left: 24rpx;

					.img {
						width: 48rpx;
						height: 48rpx;
					}
				}
			}
		}

		//产品
		.shop-block {
			position: relative;
			z-index: 1;
			background-color: #FFFFFF;
			margin-top: -28rpx;
			border-radius: 32rpx 32rpx 0rpx 0rpx;
			padding: 26rpx 0;


			.tab-inner {
				margin-left: 24rpx;
				height: 88upx;
				line-height: 88upx;
				display: flex;
				justify-content: space-between;
				align-items: center;
				font-size: 26rpx;
				color: #666666;

				.item {
					white-space: nowrap;
					line-height: 1;
					margin-right: 40upx;
					display: flex;
					align-items: center;
					justify-content: center;

					&:last-child {
						margin-right: 0;
					}

					&.active {
						font-weight: 600;
						color: #02B176;

						.text {
							position: relative;

							// 使用伪元素添加横线
							&::after {
								content: '';
								position: absolute;
								bottom: -10rpx; // 调整横线距离文字的位置
								left: 50%;
								transform: translateX(-50%);
								width: 100%;
								height: 12rpx;
								background: linear-gradient(90deg, #02B176 0%, rgba(2, 177, 118, 0) 100%);
								border-radius: 6rpx;
								z-index: 1;
							}
						}
					}
				}
			}

			.medic-list {
				padding: 20upx;

				.item {
					box-sizing: border-box;
					min-height: 296upx;
					background: #FFFFFF;
					border: 4upx solid #FFFFFF;
					border-radius: 16upx;
					margin-bottom: 18upx;
					padding: 24rpx;
					display: flex;

					.img-box {
						flex-shrink: 0;
						width: 248upx;
						height: 248upx;
						margin-right: 20upx;
						border-radius: 16upx;
						overflow: hidden;
						text-align: center;
						position: relative;

						.sales-label {
							position: absolute;
							bottom: 0;
							right: 0;
							width: 116rpx;
							height: 36rpx;
							line-height: 36rpx;
							background: rgba(245, 247, 250, 0.8);
							border-radius: 16rpx 0rpx 16rpx 0rpx;
							font-size: 20rpx;
							color: #626468;
						}

						.label {
							position: absolute;
							top: 0;
							left: 0;
							font-weight: 500;
							font-size: 24rpx;
							color: #FFFFFF;
							width: 40rpx;
							height: 40rpx;
							line-height: 40rpx;
							border-radius: 16rpx 0rpx 16rpx 0rpx;
						}

						.bg1 {
							background: linear-gradient(135deg, #FB7E4C 0%, #ED4B17 100%);
						}

						.bg2 {
							background: linear-gradient(135deg, #FBCF3F 0%, #F9B71B 100%);
						}

						.bg3 {
							background: linear-gradient(135deg, #D5AB78 0%, #C39760 100%);
						}

						.bg {
							background: linear-gradient(135deg, #C1C5CD 0%, #A5AAB5 100%);
						}

						image {
							width: 100%;
							height: 100%;
						}
					}

					.info-box {
						flex: 1;
						min-width: 0;

						.hot-sale {
							font-size: 22rpx;
							color: #FA341E;
							text-align: left;
							width: 260rpx;
							height: 36rpx;
							display: flex;
							align-items: center;
							background: linear-gradient(90deg, #FFE1D7 3.85%, #FFE8E1 53.85%, #FFFFFF 100%);
							border-radius: 20rpx 20rpx 20rpx 20rpx;
							margin-bottom: 8rpx;
						}

						.title {
							font-size: 28upx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #333333;
							margin-bottom: 4rpx;
						}

						.intro {
							font-weight: 400;
							font-size: 22rpx;
							color: #D46C0D;
							margin-bottom: 8upx;
						}

						.sale {
							font-size: 21rpx;
							color: #999999;
						}

						.lable-group {
							display: flex;
							margin-top: 8rpx;

							.lable-item {
								margin-right: 8rpx;
								height: 30rpx;
								padding: 0 8rpx;
								border-radius: 4rpx 4rpx 4rpx 4rpx;
								border: 1rpx solid #FFA599;
								font-size: 21rpx;
								color: #FF4B33;
							}
						}

						.prce-num {
							display: flex;
							align-items: center;
							justify-content: space-between;
							margin-top: 8upx;

							.price {
								font-weight: 600;
								font-size: 26rpx;
								color: #FA341E;
								font-size: 26rpx;

								.unit {
									font-size: 20upx;
								}

								.bold {
									font-size: 36upx;
								}
							}
						}
					}
				}
			}
		}
	}
</style>