<template>
	<view class="content">
		<view class="info">
			<image class="info-bg" src="https://cdn.his.cdwjyyh.com/images/sales_bg.png"></image>
			<image class="info-title" src="https://cdn.his.cdwjyyh.com/images/sales_title.png"></image>
			<view class="status_bar" :style="{height: statusBarHeight}"></view>
			<view class="search-box">
				<view class="back-box">
					<image @click="goBack" class="img" src="https://cdn.his.cdwjyyh.com/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>
					</view>
				</view>
			</view>
			<!-- 商品列表 -->
			<view class="medic-list">
				<!-- 空状态提示 -->
				<view v-if="dataList.length === 0" class="empty-state">
					<text>暂无销量数据</text>
				</view>
				<!-- 商品列表 -->
				<view v-else 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="https://cdn.his.cdwjyyh.com/images/hot.png"></image>
							<text>今日热销{{item.todaySales||'0'}}件</text>
						</view>
						<view class="title ellipsis2">{{item.productName}}</view>
						<view class="sale">{{item.productInfo || '肌肉酸疼、扭伤拉伤'}}</view>
						<view class="lable-group">
								<view class="lable-item">9.5折</view>
								<view class="lable-item" >限购{{item.productLimitNum || 1}}份</view>
							</view>
							<view class="spacer"></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.stop="addToCart(item)">
									<image class="w40 h40" src="https://cdn.his.cdwjyyh.com/images/add_car.png"></image>
								</view>
						</view>
					</view>
				</view>
			</view>
		</view>
		<!-- 商品规格选择弹窗 -->
	<ProductSpecPopup
		:visible="specVisible"
		:product="currentProduct"
		:is-buy-mode="isBuyMode"
		@update:visible="specVisible = $event"
		@cart-updated="onCartUpdated"
	/>
	</view>
</template>

<script>
	import {
		productSalesRanking // 获取当天商品实时销量榜单
	} from '@/api/product'
	import ProductSpecPopup from '@/components/product-spec-popup/product-spec-popup.vue'
	export default {
		components: {
			ProductSpecPopup
		},
		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,
				dataList: [],
				// 弹窗相关数据
				specVisible: false,
				currentProduct: {},
				isBuyMode: false
			}
		},
	onLoad() {
		// 加载销量榜单
		this.getProductSalesRanking();
	},
	onUnload() {

	},
	methods: {
		// 获取销量榜单
		getProductSalesRanking() {
			let data = {
				cateId:320
			};
			console.log('开始获取销量榜单，参数:', data);
			productSalesRanking(data).then(
				res => {
					console.log('销量榜单API返回:', res);
					if (res.code == 200) {
						// 处理销量榜单数据
						console.log('销量榜单数据:', res.data);
						// 将获取到的数据赋值给dataList，用于页面渲染
						this.dataList = res.data.list || [];
						console.log('dataList赋值后:', this.dataList);
					} else {
						console.log('获取销量榜单失败:', res.msg);
						uni.showToast({
							title: res.msg || '获取销量榜单失败',
							icon: 'none'
						});
					}
				},
				rej => {
					console.log('获取销量榜单接口调用失败:', rej);
					uni.showToast({
						title: '网络错误，请重试',
						icon: 'none'
					});
				}
			);
		},
		// 一键到顶
		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);
		},
		// 查看商品详情
		showDetail(item) {
			uni.navigateTo({
				url: `/pages_shopping/shopping/productDetails?productId=${item.productId}`
			});
		},
		// 跳转到购物车
		navgetTo(url) {
			uni.navigateTo({
				url: url
			});
		},
		// 返回上一页
		goBack() {
			uni.navigateBack({
				delta: 1 // 返回的页面数，如果 delta 大于现有页面数，则返回首页
			});
		},
		// 添加到购物车
		addToCart(product) {
			console.log('添加到购物车:', product);
			// 弹出规格选择弹窗
			this.currentProduct = product;
			this.isBuyMode = false; // 标记为添加购物车模式
			this.specVisible = true;
		},
		// 购物车更新回调
		onCartUpdated() {
			console.log('购物车已更新');
			// 可以在这里更新购物车数量等操作
		}
	}}
</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;
							display: flex;
							flex-direction: column;

							.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;
								margin-bottom: 8rpx;
							}

							.lable-group {
								display: flex;
								margin-bottom: 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;
								}
							}

							.spacer {
								flex: 1;
							}

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

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

									.unit {
										font-size: 20upx;
									}

									.bold {
										font-size: 36upx;
									}
								}
							}
					}
				}

				.empty-state {
					display: flex;
					align-items: center;
					justify-content: center;
					height: 400rpx;
					font-size: 28rpx;
					color: #999999;
				}
			}
		}
	}
</style>