<template>
	<view class="knowledge-container">
		<view class="search-container">
			<u-search placeholder="搜索知识" v-model="searchKeyword" @search="searchKnowledge" @custom="searchKnowledge"></u-search>
		</view>
		
		<view class="category-tabs">
			<u-tabs :list="categories" @click="switchCategory" :current="currentCategory"></u-tabs>
		</view>
		
		<scroll-view class="knowledge-list" scroll-y="true">
			<view v-for="item in filteredKnowledge" :key="item.id" class="knowledge-card" @click="viewDetail(item)">
				<view class="knowledge-item">
					<text class="knowledge-title">{{ item.title }}</text>
					<text class="knowledge-summary">{{ item.summary }}</text>
					<view class="knowledge-meta">
						<view class="category-tag">
							<text class="tag-text">{{ item.category }}</text>
						</view>
						<text class="read-count">{{ item.readCount }}次阅读</text>
					</view>
				</view>
			</view>
		</scroll-view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			searchKeyword: '',
			currentCategory: 0,
			categories: [
				{ name: '全部' },
				{ name: '饮食' },
				{ name: '健康' },
				{ name: '训练' },
				{ name: '护理' }
			],
			knowledgeList: [
				{
					id: 1,
					title: '猫咪日常饮食指南',
					summary: '了解猫咪的营养需求，选择合适的猫粮和喂食方式',
					category: '饮食',
					readCount: 1234,
					content: '详细的猫咪饮食指南内容...'
				},
				{
					id: 2,
					title: '狗狗疫苗接种时间表',
					summary: '幼犬和成犬的疫苗接种计划和注意事项',
					category: '健康',
					readCount: 856,
					content: '疫苗接种详细说明...'
				},
				{
					id: 3,
					title: '宠物基础训练方法',
					summary: '如何训练宠物基本指令和良好习惯',
					category: '训练',
					readCount: 642,
					content: '训练方法详细介绍...'
				}
			]
		}
	},
	computed: {
		filteredKnowledge() {
			let result = this.knowledgeList
			
			// 分类筛选
			if (this.currentCategory > 0) {
				const categoryName = this.categories[this.currentCategory].name
				result = result.filter(item => item.category === categoryName)
			}
			
			// 搜索筛选
			if (this.searchKeyword) {
				result = result.filter(item => 
					item.title.includes(this.searchKeyword) || 
					item.summary.includes(this.searchKeyword)
				)
			}
			
			return result
		}
	},
	methods: {
		switchCategory(item) {
			this.currentCategory = item.index
		},
		
		searchKnowledge() {
			// 搜索逻辑已在computed中处理
		},
		
		viewDetail(item) {
			uni.navigateTo({
				url: `/pages/assistant/knowledge-detail?id=${item.id}`
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.knowledge-container {
	background: linear-gradient(135deg, #FF8A80 0%, #FFB6C1 25%, #FECFEF 50%, #F8BBD9 100%);
	min-height: 100vh;
}

.search-container {
	background: rgba(255, 255, 255, 0.95);
	backdrop-filter: blur(20rpx);
	margin: 20rpx 30rpx;
	border-radius: 24rpx;
	padding: 20rpx;
	box-shadow: 0 8rpx 32rpx rgba(255, 138, 128, 0.2);
	border: 1rpx solid rgba(255, 255, 255, 0.3);
}

.category-tabs {
	background: rgba(255, 255, 255, 0.95);
	backdrop-filter: blur(20rpx);
	margin: 20rpx 30rpx;
	border-radius: 24rpx;
	padding: 20rpx 0;
	box-shadow: 0 8rpx 32rpx rgba(255, 138, 128, 0.2);
	border: 1rpx solid rgba(255, 255, 255, 0.3);
}

.knowledge-list {
	height: calc(100vh - 300rpx);
	padding: 0 30rpx;
}

.knowledge-card {
	background: rgba(255, 255, 255, 0.95);
	backdrop-filter: blur(20rpx);
	margin: 20rpx 0;
	border-radius: 24rpx;
	padding: 24rpx;
	box-shadow: 0 8rpx 32rpx rgba(255, 138, 128, 0.2);
	border: 1rpx solid rgba(255, 255, 255, 0.3);
	transition: all 0.3s ease;

	&:active {
		transform: scale(0.98);
		box-shadow: 0 4rpx 16rpx rgba(255, 138, 128, 0.3);
	}
}

.knowledge-item {
	.knowledge-title {
		font-size: 32rpx;
		font-weight: 600;
		color: #FF8A80;
		display: block;
		margin-bottom: 16rpx;
	}

	.knowledge-summary {
		font-size: 28rpx;
		color: #666666;
		line-height: 1.4;
		display: block;
		margin-bottom: 20rpx;
	}

	.knowledge-meta {
		display: flex;
		justify-content: space-between;
		align-items: center;

		.category-tag {
			background: rgba(255, 138, 128, 0.1);
			border: 2rpx solid rgba(255, 138, 128, 0.3);
			border-radius: 16rpx;
			padding: 8rpx 16rpx;

			.tag-text {
				font-size: 24rpx;
				color: #FF8A80;
				font-weight: 500;
			}
		}

		.read-count {
			font-size: 24rpx;
			color: #999999;
		}
	}
}
</style>
