<template>
	<view class="search-container">
		<!-- 头部搜索栏 -->
		<view class="search-header">
			<view class="status-bar" :style="{ height: statusBarHeight + 'px' }"></view>
			<view class="header-main">
				<view class="back-btn" @click="goBack">
					<text class="iconfont icon-back">＜</text>
				</view>
				<view class="search-input-wrap">
					<text class="search-icon">🔍</text>
					<input class="search-input" type="text" v-model="keyword" placeholder="输入关键词或粘贴商品标题" 
						confirm-type="search" @confirm="onSearch(keyword)" :focus="true" />
				</view>
				<view class="search-btn-red" @click="onSearch(keyword)">搜索</view>
			</view>
			<!-- 平台切换Tab -->
			<view class="platform-tabs">
				<view class="tab-item" :class="{ 'active': activePlatform === 0 }" @click="activePlatform = 0">淘宝</view>
				<view class="tab-item" :class="{ 'active': activePlatform === 1 }" @click="activePlatform = 1">京东</view>
				<view class="tab-item" :class="{ 'active': activePlatform === 2 }" @click="activePlatform = 2">拼多多</view>
				<view class="tab-item" :class="{ 'active': activePlatform === 3 }" @click="activePlatform = 3">抖音电商</view>
				<view class="active-line" :style="{ left: tabLineLeft + '%' }"></view>
			</view>
		</view>

		<scroll-view scroll-y class="search-body" :style="{ paddingTop: (statusBarHeight + 96) + 'px' }">
			<!-- 历史搜索 -->
			<view class="section history-section" v-if="historyList.length > 0">
				<view class="tag-list">
					<view class="tag-item" v-for="(word, index) in historyList" :key="index" @click="onSearch(word)">
						{{ word }}
					</view>
				</view>
			</view>

			<!-- 热门搜索 (固定的词条文字) -->
			<view class="section hot-tags-section">
				<view class="section-title">热门搜索</view>
				<view class="tag-list">
					<view class="tag-item" v-for="(word, index) in hotTags" :key="index" @click="onSearch(word)">
						{{ word }}
					</view>
				</view>
			</view>

			<!-- 榜单与话题双栏 -->
			<view class="dual-columns">
				<!-- 全网热搜 (左侧) -->
				<view class="column-card rank-card">
					<view class="card-header">
						<text class="card-icon">📢</text>
						<text class="card-title">全网热搜</text>
						<text class="card-sub">今日热搜全知道</text>
					</view>
					<view class="rank-list">
						<view class="rank-item" v-for="(item, index) in rankingList" :key="index" @click="onSearch(item.keyword)">
							<view class="rank-num">
								<image v-if="index < 3" :src="'https://img.bc.haodanku.com/cms_web/rank_' + (index + 1) + '.png'" mode="aspectFit" class="rank-icon-img"></image>
								<text v-else class="num-txt">{{ index + 1 }}</text>
							</view>
							<image class="rank-img" :src="item.itempic" mode="aspectFill"></image>
							<text class="rank-name">{{ item.keyword }}</text>
						</view>
					</view>
				</view>

				<!-- 热点话题 (右侧) -->
				<view class="column-card topic-card">
					<view class="card-header">
						<text class="card-icon">💬</text>
						<text class="card-title">热点话题</text>
						<text class="card-sub">热点资讯</text>
					</view>
					<view class="topic-list">
						<view class="topic-item" v-for="(item, index) in hotThemes" :key="index">
							<view class="topic-num" :class="'num-' + (index + 1)">{{ index + 1 }}</view>
							<text class="topic-title">{{ item.title }}</text>
						</view>
					</view>
				</view>
			</view>

			<view class="copyright">
				<text>© 粉丝福利购专属优惠商城</text>
			</view>
			<view class="footer-placeholder"></view>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				statusBarHeight: 44,
				keyword: '',
				activePlatform: 0,
				historyList: [],
				hotTags: ['风扇', '牙膏', '面膜', '洗发水', '保温杯', '蚊香液', '洗脸巾', '四件套'],
				rankingList: [],
				hotThemes: []
			}
		},
		computed: {
			tabLineLeft() {
				// 淘宝 京东 拼多多 抖音 对应的中心点位置
				const positions = [12.5, 37.5, 62.5, 87.5];
				return positions[this.activePlatform];
			}
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync();
			this.statusBarHeight = sysInfo.statusBarHeight || 44;
			
			this.loadHistory();
			this.getRankingList();
			this.getHotThemes();
		},
		methods: {
			goBack() {
				uni.navigateBack();
			},
			loadHistory() {
				const history = uni.getStorageSync('search_history');
				if (history) {
					this.historyList = JSON.parse(history);
				}
			},
			saveHistory(word) {
				if (!word.trim()) return;
				let list = [...this.historyList];
				const idx = list.indexOf(word);
				if (idx !== -1) {
					list.splice(idx, 1);
				}
				list.unshift(word);
				if (list.length > 10) list = list.slice(0, 10);
				
				this.historyList = list;
				uni.setStorageSync('search_history', JSON.stringify(list));
			},
			getRankingList() {
				uni.request({
					url: 'https://api.cmspro.haodanku.com/search/searchRankingList?cid=qOstW90',
					success: (res) => {
						if (res.data && res.data.code === 200 && res.data.data.ranking_list) {
							this.rankingList = res.data.data.ranking_list.slice(0, 10);
						}
					}
				});
			},
			getHotThemes() {
				uni.request({
					url: 'https://api.cmspro.haodanku.com/index/hotTheme?cid=qOstW90',
					success: (res) => {
						if (res.data && res.data.code === 200) {
							this.hotThemes = res.data.data.slice(0, 10);
						}
					}
				});
			},
			onSearch(word) {
				if (!word.trim()) {
					uni.showToast({ title: '请输入搜索内容', icon: 'none' });
					return;
				}
				this.saveHistory(word);
				const name = encodeURIComponent(word.trim());
				uni.navigateTo({
					url: `/pages/category/category_detail?cate_name=${name}&is_search=1`
				});
			}
		}
	}
</script>

<style scoped>
	.search-container {
		width: 100%;
		height: 100vh;
		background-color: #f8f8f8;
	}

	/* 头部 */
	.search-header {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		background: #ffffff;
		z-index: 100;
		box-shadow: 0 2rpx 10rpx rgba(0,0,0,0.02);
	}

	.header-main {
		height: 88rpx;
		display: flex;
		align-items: center;
		padding: 0 24rpx;
	}

	.back-btn {
		width: 50rpx;
		font-size: 36rpx;
		color: #333;
	}

	.search-input-wrap {
		flex: 1;
		height: 72rpx;
		background-color: #ffffff;
		border: 2rpx solid #ff4d6d;
		border-radius: 36rpx;
		display: flex;
		align-items: center;
		padding: 0 24rpx;
		margin: 0 20rpx;
	}

	.search-icon {
		font-size: 28rpx;
		color: #ff4d6d;
		margin-right: 12rpx;
	}

	.search-input {
		flex: 1;
		height: 100%;
		font-size: 26rpx;
		color: #333;
	}

	.search-btn-red {
		width: 120rpx;
		height: 64rpx;
		background: linear-gradient(to right, #ff715a, #ff416c);
		color: #ffffff;
		font-size: 28rpx;
		font-weight: bold;
		border-radius: 32rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	/* 平台Tab */
	.platform-tabs {
		height: 80rpx;
		display: flex;
		position: relative;
		border-bottom: 1rpx solid #f2f2f2;
	}

	.tab-item {
		flex: 1;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 28rpx;
		color: #666;
		transition: all 0.3s;
	}

	.tab-item.active {
		color: #ff416c;
		font-weight: bold;
		font-size: 30rpx;
	}

	.active-line {
		position: absolute;
		bottom: 8rpx;
		width: 40rpx;
		height: 4rpx;
		background-color: #ff416c;
		border-radius: 4rpx;
		transform: translateX(-50%);
		transition: left 0.3s cubic-bezier(0.645, 0.045, 0.355, 1);
	}

	/* 内容区 */
	.search-body {
		height: 100%;
		box-sizing: border-box;
	}

	.section {
		padding: 30rpx 24rpx 10rpx;
		background-color: #ffffff;
	}

	.history-section {
		padding-top: 20rpx;
	}

	.section-title {
		font-size: 26rpx;
		color: #999;
		margin-bottom: 20rpx;
	}

	.tag-list {
		display: flex;
		flex-wrap: wrap;
	}

	.tag-item {
		padding: 10rpx 30rpx;
		background-color: #f1f1f1;
		border-radius: 30rpx;
		font-size: 24rpx;
		color: #333;
		margin-right: 20rpx;
		margin-bottom: 20rpx;
	}

	/* 双栏卡片 */
	.dual-columns {
		display: flex;
		padding: 20rpx;
		justify-content: space-between;
	}

	.column-card {
		width: 48.5%;
		background-color: #ffffff;
		border-radius: 16rpx;
		padding: 20rpx;
		box-sizing: border-box;
	}

	.card-header {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
		flex-wrap: wrap;
	}

	.card-icon {
		font-size: 32rpx;
		margin-right: 10rpx;
	}

	.card-title {
		font-size: 28rpx;
		font-weight: bold;
		color: #d16d3f;
	}

	.topic-card .card-title {
		color: #e66b5b;
	}

	.card-sub {
		font-size: 20rpx;
		color: #d1a38f;
		margin-left: 10rpx;
		width: 100%;
		margin-top: 4rpx;
	}

	/* 全网热搜列表 */
	.rank-item {
		display: flex;
		align-items: center;
		margin-bottom: 30rpx;
	}

	.rank-num {
		width: 40rpx;
		display: flex;
		justify-content: center;
		margin-right: 10rpx;
	}

	.rank-icon-img {
		width: 32rpx;
		height: 32rpx;
	}

	.num-txt {
		font-size: 26rpx;
		font-style: italic;
		font-weight: bold;
		color: #ff9d00;
	}

	.rank-img {
		width: 80rpx;
		height: 80rpx;
		border-radius: 8rpx;
		margin-right: 16rpx;
		background-color: #f5f5f5;
	}

	.rank-name {
		font-size: 26rpx;
		color: #333;
		font-weight: bold;
		flex: 1;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	/* 热点话题列表 */
	.topic-item {
		display: flex;
		align-items: center;
		margin-bottom: 36rpx;
	}

	.topic-num {
		width: 36rpx;
		height: 36rpx;
		font-size: 24rpx;
		font-weight: bold;
		margin-right: 16rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.topic-num.num-1 { color: #ffb800; font-size: 32rpx; }
	.topic-num.num-2 { color: #c0c0c0; font-size: 32rpx; }
	.topic-num.num-3 { color: #d1a38f; font-size: 32rpx; }
	.topic-num.num-4 { color: #ff9d00; }
	.topic-num.num-5 { color: #ff9d00; }

	.topic-title {
		font-size: 26rpx;
		color: #333;
		font-weight: bold;
		flex: 1;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.copyright {
		text-align: center;
		padding: 40rpx 0;
		font-size: 24rpx;
		color: #ccc;
	}

	.footer-placeholder {
		height: 60rpx;
	}
</style>
