<template>
	<view class="assistant-container page-container-with-bg">
		<!-- 宠物助手信息卡片 -->
		<view class="assistant-info-card">
			<view class="assistant-avatar-wrapper">
				<view class="assistant-avatar">
					<text class="avatar-emoji">🤖</text>
				</view>
			</view>
			<view class="assistant-info-text">
				<text class="assistant-name">宠物助手</text>
				<text class="assistant-status">专业的宠物护理顾问</text>
			</view>
			<view class="chat-status">
				<view class="status-dot"></view>
				<text class="status-text">在线</text>
			</view>
			<view class="knowledge-icon" @click="openKnowledge">
				<text class="icon-text">📚</text>
			</view>
		</view>

		<!-- 聊天消息列表 -->
		<view class="chat-messages">
			<scroll-view
				class="chat-scroll"
				scroll-y
				:scroll-top="scrollTop"
				scroll-with-animation
			>
				<view class="message-list">
					<!-- 消息项 -->
					<template v-for="(message, index) in messageList" :key="index">
						<!-- AI消息 -->
						<view class="message-item ai" v-if="message.type === 'ai'">
							<view class="message-avatar">
								<view class="assistant-avatar">
									<text class="avatar-emoji">🤖</text>
								</view>
							</view>
							<view class="message-content ai">
								<view class="message-bubble ai">
									<text class="message-text" v-if="!(isThinking && index === messageList.length - 1)">{{ message.content }}</text>
									<view class="typing-dots" v-else>
										<view class="typing-dot"></view>
										<view class="typing-dot"></view>
										<view class="typing-dot"></view>
									</view>
								</view>
								<text class="message-time" v-if="!(isThinking && index === messageList.length - 1)">{{ message.time }}</text>
							</view>
						</view>

						<!-- 用户消息 -->
						<view class="message-item user" v-if="message.type === 'user'">
							<view class="message-content user">
								<view class="message-bubble user">
									<text class="message-text">{{ message.content }}</text>
								</view>
								<text class="message-time">{{ message.time }}</text>
							</view>
							<view class="message-avatar">
								<view class="user-avatar">
									<text class="avatar-emoji">👤</text>
								</view>
							</view>
						</view>
					</template>

					<!-- AI思考中状态 -->
					<view class="message-item ai" v-if="isThinking">
						<view class="message-avatar">
							<view class="assistant-avatar">
								<text class="avatar-emoji">🤖</text>
							</view>
						</view>
						<view class="message-content ai">
							<view class="message-bubble ai typing-bubble">
								<view class="typing-dots">
									<view class="typing-dot"></view>
									<view class="typing-dot"></view>
									<view class="typing-dot"></view>
								</view>
							</view>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>

		<!-- 输入区域 -->
		<view class="chat-input-area">
			<view class="input-container">
				<view class="input-wrapper">
					<input
						v-model="inputMessage"
						placeholder="和宠物助手说点什么..."
						class="message-input"
						@confirm="sendMessage"
						confirm-type="send"
						maxlength="500"
					/>
				</view>
				<view class="send-button" @click="sendMessage" :class="{ active: inputMessage.trim() }">
					<text class="send-text">发送</text>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
export default {
	data() {
		return {
			inputMessage: '',
			scrollTop: 0,
			isThinking: false,
			userAvatar: '/static/user-avatar.png',
			messageList: [
				{
					type: 'ai',
					content: '您好！我是您的宠物AI助手🐾\n\n我可以为您解答关于宠物饲养、健康、训练、营养等方面的问题。有什么可以帮助您的吗？',
					time: this.getCurrentTime()
				}
			],
			quickQuestions: [
				{ id: 1, text: '猫咪不吃饭怎么办？' },
				{ id: 2, text: '狗狗需要多久洗一次澡？' },
				{ id: 3, text: '宠物疫苗接种时间' },
				{ id: 4, text: '如何训练宠物定点上厕所？' },
				{ id: 5, text: '宠物发烧的症状' },
				{ id: 6, text: '幼猫喂养注意事项' }
			],
			// 知识库数据
			knowledgeBase: {
				'喂食': {
					keywords: ['喂食', '吃饭', '食物', '饮食', '营养'],
					responses: [
						'定时定量喂食很重要，成年猫每天2-3次，幼猫3-4次。',
						'选择优质的宠物食品，避免给宠物吃人类食物。',
						'确保宠物随时有清洁的饮用水。'
					]
				},
				'健康': {
					keywords: ['健康', '生病', '症状', '发烧', '呕吐', '腹泻'],
					responses: [
						'定期体检很重要，建议每年至少一次。',
						'注意观察宠物的食欲、精神状态和排便情况。',
						'如果出现异常症状，请及时就医。'
					]
				},
				'疫苗': {
					keywords: ['疫苗', '接种', '免疫', '预防'],
					responses: [
						'幼猫幼犬8-12周开始接种疫苗。',
						'成年宠物每年需要加强免疫。',
						'常见疫苗包括三联疫苗和狂犬疫苗。'
					]
				},
				'训练': {
					keywords: ['训练', '教育', '行为', '定点', '上厕所'],
					responses: [
						'训练需要耐心和一致性。',
						'使用正向强化，奖励好行为。',
						'避免体罚，这会让宠物产生恐惧。'
					]
				},
				'洗澡': {
					keywords: ['洗澡', '清洁', '卫生'],
					responses: [
						'狗狗一般1-2周洗一次澡，猫咪通常不需要经常洗澡。',
						'使用宠物专用洗浴用品。',
						'洗澡后要及时吹干，避免感冒。'
					]
				}
			}
		}
	},
	methods: {
		getCurrentTime() {
			const now = new Date()
			return `${now.getHours().toString().padStart(2, '0')}:${now.getMinutes().toString().padStart(2, '0')}`
		},

		askQuestion(question) {
			this.inputMessage = question
			this.sendMessage()
		},

		sendMessage() {
			if (!this.inputMessage.trim() || this.isThinking) return

			// 添加用户消息
			const userMessage = {
				type: 'user',
				content: this.inputMessage,
				time: this.getCurrentTime()
			}
			this.messageList.push(userMessage)

			// 开始AI思考
			this.isThinking = true
			this.scrollToBottom()

			// 模拟AI回复
			setTimeout(() => {
				const aiMessage = {
					type: 'ai',
					content: this.getAIResponse(this.inputMessage),
					time: this.getCurrentTime()
				}
				this.messageList.push(aiMessage)
				this.isThinking = false
				this.scrollToBottom()
			}, 1500 + Math.random() * 1000) // 1.5-2.5秒随机延迟

			this.inputMessage = ''
		},

		getAIResponse(question) {
			// 智能匹配知识库
			for (let category in this.knowledgeBase) {
				const { keywords, responses } = this.knowledgeBase[category]
				if (keywords.some(keyword => question.includes(keyword))) {
					const randomResponse = responses[Math.floor(Math.random() * responses.length)]
					return `${randomResponse}\n\n💡 如果您需要更详细的建议，建议咨询专业的宠物医生。`
				}
			}

			// 默认回复
			const defaultResponses = [
				'这是一个很好的问题！建议您咨询专业的宠物医生获得更准确的建议。',
				'每只宠物的情况都不同，建议根据具体情况来处理。您可以查看我们的知识库了解更多信息。',
				'感谢您的提问！这个问题比较复杂，建议您带宠物去专业的宠物医院检查。',
				'我理解您的担心。建议您记录宠物的具体症状，然后咨询专业医生。'
			]

			return defaultResponses[Math.floor(Math.random() * defaultResponses.length)]
		},

		scrollToBottom() {
			this.$nextTick(() => {
				this.scrollTop = 999999
			})
		},

		startVoiceInput() {
			uni.showToast({
				title: '语音功能开发中',
				icon: 'none'
			})
		},

		openKnowledge() {
			uni.navigateTo({
				url: '/pages/assistant/knowledge'
			})
		}
	}
}
</script>

<style lang="scss" scoped>
.assistant-container {
  width: 100vw;
  height: 100vh;
  display: flex;
  flex-direction: column;
  position: relative;
}

.assistant-info-card {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20rpx);
  margin: 20rpx 30rpx 0 30rpx;
  border-radius: 24rpx;
  padding: 24rpx;
  display: flex;
  align-items: center;
  box-shadow: 0 8rpx 32rpx rgba(255, 138, 128, 0.2);
  border: 1rpx solid rgba(255, 255, 255, 0.3);

  .assistant-avatar-wrapper {
    margin-right: 24rpx;

    .assistant-avatar {
      width: 80rpx;
      height: 80rpx;
      border-radius: 40rpx;
      background: linear-gradient(135deg, #FF8A80, #FFB6C1);
      display: flex;
      align-items: center;
      justify-content: center;
      box-shadow: 0 8rpx 24rpx rgba(255, 138, 128, 0.3);

      .avatar-emoji {
        font-size: 40rpx;
        color: #ffffff;
      }
    }
  }

  .assistant-info-text {
    flex: 1;
    display: flex;
    flex-direction: column;
    gap: 8rpx;

    .assistant-name {
      font-size: 32rpx;
      font-weight: 600;
      color: #333333;
    }

    .assistant-status {
      font-size: 24rpx;
      color: #666666;
    }
  }

  .chat-status {
    display: flex;
    align-items: center;
    gap: 12rpx;
    margin-right: 20rpx;

    .status-dot {
      width: 16rpx;
      height: 16rpx;
      border-radius: 50%;
      background: #4CAF50;
      box-shadow: 0 0 0 4rpx rgba(76, 175, 80, 0.2);
    }

    .status-text {
      font-size: 24rpx;
      color: #4CAF50;
      font-weight: 500;
    }
  }

  .knowledge-icon {
    width: 60rpx;
    height: 60rpx;
    border-radius: 30rpx;
    background: rgba(255, 255, 255, 0.8);
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);

    .icon-text {
      font-size: 28rpx;
    }
  }
}

.quick-questions {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20rpx);
  margin: 20rpx 30rpx;
  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);

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

  .question-grid {
    display: flex;
    flex-wrap: wrap;
    gap: 20rpx;

    .question-button {
      background: rgba(255, 138, 128, 0.1);
      border: 2rpx solid rgba(255, 138, 128, 0.3);
      border-radius: 20rpx;
      padding: 16rpx 24rpx;
      transition: all 0.3s ease;

      &:active {
        background: rgba(255, 138, 128, 0.2);
        transform: scale(0.98);
      }

      .question-text {
        font-size: 26rpx;
        color: #FF8A80;
        font-weight: 500;
      }
    }
  }
}

.chat-messages {
  margin-top: 20rpx;
  flex: 1;
  overflow: hidden;
}

.chat-scroll {
  width: 100%;
  height: 100%;
}

.message-list {
  padding: 20rpx 0;
  padding-bottom: 80rpx;
  min-height: 100%;
  box-sizing: border-box;
}

.message-item {
  display: flex;
  margin-bottom: 24rpx;
  align-items: flex-start;
}

.message-item.user {
  justify-content: flex-end;
  padding: 0 10rpx 0 80rpx;
}

.message-item.ai {
  justify-content: flex-start;
  padding: 0 80rpx 0 10rpx;
}

.message-avatar {
  width: 64rpx;
  height: 64rpx;
  margin: 0 16rpx;
  flex-shrink: 0;
}

.assistant-avatar, .user-avatar {
  width: 100%;
  height: 100%;
  border-radius: 32rpx;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 32rpx;
}

.assistant-avatar {
  background: linear-gradient(135deg, #FF8A80, #FFB6C1);
  box-shadow: 0 8rpx 24rpx rgba(255, 138, 128, 0.3);
}

.user-avatar {
  background: linear-gradient(135deg, #81C784, #A5D6A7);
  box-shadow: 0 8rpx 24rpx rgba(129, 199, 132, 0.3);
}

.avatar-emoji {
  color: #ffffff;
}

.message-content {
  display: flex;
  flex-direction: column;
  max-width: 65%;
}

.message-content.user {
  align-items: flex-end;
}

.message-content.ai {
  align-items: flex-start;
}

.message-bubble {
  padding: 20rpx 24rpx;
  border-radius: 24rpx;
  word-wrap: break-word;
  position: relative;
}

.message-bubble.ai {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10rpx);
  box-shadow: 0 4rpx 16rpx rgba(0, 0, 0, 0.1);
  border-bottom-left-radius: 8rpx;
}

.message-bubble.ai.typing-bubble {
  padding: 24rpx;
}

.message-bubble.user {
  background: linear-gradient(135deg, #FF8A80 0%, #FFB6C1 100%);
  box-shadow: 0 4rpx 16rpx rgba(255, 138, 128, 0.3);
  border-bottom-right-radius: 8rpx;
}

.message-text {
  font-size: 28rpx;
  line-height: 1.4;
  color: #333333;
}

.message-bubble.user .message-text {
  color: #ffffff;
}

.message-time {
  margin-top: 8rpx;
  font-size: 20rpx;
  color: rgba(255, 255, 255, 0.7);
}

.message-content.user .message-time {
  text-align: right;
}

.message-content.ai .message-time {
  text-align: left;
}

.typing-dots {
  display: flex;
  gap: 8rpx;
  justify-content: center;

  .typing-dot {
    width: 12rpx;
    height: 12rpx;
    border-radius: 50%;
    background-color: #FF8A80;
    animation: typing 1.4s infinite ease-in-out;

    &:nth-child(1) { animation-delay: -0.32s; }
    &:nth-child(2) { animation-delay: -0.16s; }
  }
}

@keyframes typing {
  0%, 80%, 100% {
    transform: scale(0.8);
    opacity: 0.5;
  }
  40% {
    transform: scale(1);
    opacity: 1;
  }
}

.chat-input-area {
  background: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(20rpx);
  padding: 16rpx 20rpx;
  //padding-bottom: calc(16rpx + env(safe-area-inset-bottom));
  border-top: 1rpx solid rgba(255, 255, 255, 0.2);
}

.input-container {
  display: flex;
  align-items: center;
  gap: 16rpx;

  .input-wrapper {
    flex: 1;
    background: rgba(255, 255, 255, 0.9);
    border-radius: 28rpx;
    padding: 0 20rpx;
    backdrop-filter: blur(10rpx);
    border: 1rpx solid rgba(255, 255, 255, 0.3);
    height: 56rpx;
    display: flex;
    align-items: center;

    .message-input {
      width: 100%;
      height: 100%;
      font-size: 28rpx;
      color: #333333;
      border: none;
      outline: none;
      background: transparent;
    }
  }

  .send-button {
    padding: 12rpx 20rpx;
    border-radius: 24rpx;
    background: rgba(200, 200, 200, 0.5);
    transition: all 0.3s ease;

    .send-text {
      font-size: 28rpx;
      color: #666666;
      font-weight: 500;
    }
  }

  .send-button.active {
    background: linear-gradient(135deg, #FF8A80 0%, #FFB6C1 100%);
    box-shadow: 0 4rpx 16rpx rgba(255, 138, 128, 0.4);

    .send-text {
      color: #ffffff;
    }
  }
}
</style>
