<template>
	<view>
		<navBar leftText="板块异动明细" :hideNavBg="true"></navBar>
		<image class="topBg absolute" src="/static/image/index/conceptTopBg.png" mode="widthFix"></image>
		
		<view class="stockDetailsC fixed" style="background-color: white; border-radius: 10rpx; overflow: hidden;" :style="'top:'+contentTop+'px;'">
			
			<view style="height: 86rpx;">
				<scroll-view scroll-x style="white-space: nowrap; height: 100%; padding: 0 20rpx; box-sizing: border-box;" scroll-with-animation :scroll-into-view="'tab-' + activeIndex">
					<view style="display: flex; align-items: center; height: 100%; font-weight: 500;">
						<view :id="'tab-' + index" @click="activeIndex = index" v-for="(item,index) in bkList" :key="index" style="display: flex; align-items: center; justify-content: center; line-height: 85rpx; margin: 0 20rpx;" :style="{color: (activeIndex == index ? '#2B2B2B' : '#999999'), 'border-bottom': (activeIndex == index ? '1rpx solid #F2C369' : 'none'), 'font-size' : (activeIndex == index ? '28rpx' : '26rpx')}">
							{{item.title}}
						</view>
					</view>
				</scroll-view>
			</view>
			
			<view style="height: 1rpx; background-color: #E7E7E7; margin: 0 20rpx;"></view>
			
			<view style="height: 48rpx; display: grid; grid-template-columns: repeat(3, 1fr); gap: 10rpx; margin: 23rpx 40rpx;">
				<view @click="filterIndex = index" style="height: 45rpx; display: flex; align-items: center; justify-content: center; color: #939393; font-size: 24rpx; font-weight: 500; border-radius: 5rpx;" :style="{color: (filterIndex == index ? '#070707' : '#939393'), 'border': (filterIndex == index ? '1rpx solid #F2C369' : '1rpx solid #E5E5E5'), 'background-color' : (filterIndex == index ? '#F2C369' : '#fff')}" v-for="(item,index) in bkFilters" :key="index">
					{{item}}
				</view>
			</view>
			
			
			<view style="margin: 0 20rpx; background-color: #FAFAFC; display: grid; grid-template-columns: 35% 20% 20% 25%;">
				<view v-for="(item,index) in ['名称', '涨幅', '连板', '板块']" :key="index" style="font-size: 22rpx; color: #666666; padding: 0 15rpx; box-sizing: border-box; font-weight: 500; line-height: 60rpx;" :style="{'text-align' : index == 0 ? 'left' : 'center'}">
					{{item}}
				</view>
			</view>
			
			<scroll-view scroll-y style="position: absolute; top: 241rpx; left: 0; right: 0; bottom: 0; font-size: 20rpx; font-weight: 500;">
				<view v-for="(item, index) in 30" style="margin: 0 20rpx; display: grid; grid-template-columns: 35% 20% 20% 25%;" :style="{'background-color': (index % 2 == 0 ? '#fff' : '#FAFAFC')}">
					<view style="display: flex; align-items: center; color: #666666; height: 60rpx;">
						<view style="display: flex; align-items: center; background-color: #FFF0E6; border-radius: 5rpx; padding: 0 10rpx; margin-left: 14rpx;">
							<image style="width: 15rpx; height: 17rpx;" src="/pagesStock/static/icon/all-icon-4.png" mode="widthFix"></image>
							<view style="color: #F97316; margin-left: 5rpx;">跟风</view>
						</view>
						<view style="margin-left: 10rpx;">康强电子</view>
					</view>
					
					<view style="display: flex; align-items: center; justify-content: center;">
						<view style="font-size: 24rpx; color: #EC3440; font-weight: bold;">+10.00%</view>
					</view>
					
					<view style="display: flex; align-items: center; justify-content: center;">
						<view style="color: #F3B800; padding: 0 10rpx; border-radius: 5rpx; background-color: #FFF9E6; border: 1rpx solid #F3B800; display: flex; align-items: center; justify-content: center;">2连板</view>
					</view>
					
					<view style="display: flex; align-items: center; justify-content: center;">
						<view style="background-color: #F4EFFF; border-radius: 5rpx; padding: 0 10rpx; color: #8B5CF6;">芯片(封装材料)</view>
					</view>
					
				</view>
			
			
			
			</scroll-view>
		</view>
		
		

	</view>
</template>

<script>
	import {
		inject
	} from 'vue'
import {
		getBaseURL1
	} from '@/request/http.js'
	
	export default {
		data() {
			return {
				navH: inject('navHeight'),
				contentTop: '',
				activeIndex: 0,
				bkList: [],
				bkFilters: [
					'按涨幅',
					'按连板数',
					'只看龙头'
				],
				filterIndex: 0,
				selectedFullDate: '', // 年-月-日

			}
		},
		onLoad(e) {
			this.activeIndex = e.index
			this.selectedFullDate=e.data
			this.contentTop = this.navH + 20 / 750 * inject('windowWidth')
			
				this.fetchData()
		},
		methods: {
			
			getPreviousDayDate(dateStr) {
				// 校验输入日期格式是否正确
				if (!/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
					console.error('日期格式错误，请传入 YYYY-MM-DD 格式的日期');
					return '';
				}
			
				// 创建日期对象（注意：月份是 0 开始的，所以需要处理）
				const [year, month, day] = dateStr.split('-').map(Number);
				const date = new Date(year, month - 1, day);
			
				// 将日期减一天
				date.setDate(date.getDate() - 2);
			
				// 格式化前一天的日期为 YYYYMMDD 格式（补零处理）
				const prevYear = date.getFullYear();
				const prevMonth = String(date.getMonth() + 1).padStart(2, '0');
				const prevDay = String(date.getDate()).padStart(2, '0');
			
				return `${prevYear}${prevMonth}${prevDay}`;
			},
			
			/**
			 * 请求接口数据（优化：动态日期+自动时间戳）
			 */
			async fetchData() {
				try {
					// 1. 自动生成当前时间戳（替代固定值）
					const timestamp = new Date().getTime();


					// 调用上面的函数，获取前一天的格式化日期（YYYYMMDD）
					const formattedDate = this.getPreviousDayDate(this.selectedFullDate);
					const baseURL = getBaseURL1();
					const requestUrl = `${baseURL}/data/zt/daily/${formattedDate}.json?t=${timestamp}`;

					console.log('请求URL：', requestUrl); // 打印URL便于调试

					const res = await uni.request({
						url: requestUrl,
						method: 'GET'
					});

					if (res.statusCode === 200 && res.data) {
						this.originData = res.data;


						const chartData = this.originData.chart_data || {};
						const labels = chartData.labels || [];
						const counts = chartData.counts || [];

						// 1. 找到counts中的最大值（用于计算热度颜色）
						const maxCount = counts.length > 0 ? Math.max(...counts) : 0;

						// 2. 遍历组装bkList（包含标题、数量、背景色、占比），先保证labels和counts长度一致
						let bkList = [];
						const maxLen = Math.min(labels.length, counts.length); // 取两者较短的长度，避免越界
						for (let i = 0; i < maxLen; i++) {
							const title = labels[i];
							const count = counts[i] || 0;
							
							bkList.push({
								title, // 板块名称
								count, // 数量
	});
						}

						
						this.bkList = bkList;
						
					} else {
						uni.showToast({
							title: '数据请求失败',
							icon: 'none'
						});
					}
				} catch (error) {
					console.error('请求异常:', error);
					uni.showToast({
						title: '网络异常',
						icon: 'none'
					});
				}
			},
		}
	}
</script>

<style lang="less">
	page {
		background-color: #070707;
	}

	.topBg {
		top: 0;
		left: 0;
		width: 100%;
		height: auto;
	}

	.stockDetailsC {
		left: 25rpx;
		right: 25rpx;
		width: calc(100vw - 50rpx);
		bottom: env(safe-area-inset-bottom);
	}
</style>