<template>
  <view class="ai-box">
		<view class="left-box" v-if="answer.length > 0">
			<view class="content answer-box">
				<view class="item-box" v-for="(item, index) in state.list" :key="index">
					<view class="item right">
						<text class="text"> {{ item.question }} </text>
					</view>
					<view class="item left">
						<text class="text"> {{ item.answer }}</text> 
					</view>
				</view>
			</view>
		</view>
		<view class="left-box" v-else>
			<view class="content answer-box" style="padding: 10px 0 0 10px;">暂无答案,请先输入问题</view>
		</view>
		<view class="right-box">
			<view class="title"><text>Ai</text>解题答疑</view>
			<view class="content" @keypress.enter="sendAi">
				<textarea  
					v-model:value="question"
					placeholder="请输入需要咨询的问题" 
				/>
			</view>
		</view>
  </view>
</template>
<script lang="ts" setup>
  import { ref, reactive } from 'vue';
	import config from '@/common/config/index.js' 
	import { useUserStore } from '@/common/store/user'; 
	const userStore = useUserStore();
 
  const question = ref<string>('');
  const answer = ref<string>('');
  const state = reactive({
    list: [] as Array<{ question: string; answer: string }>,
  });

  const sendAi = async () => {
    if(!question.value) return;
    try {
			uni.showLoading({
				title: '正在思考...'
			})
      let url = config.baseUrl + '/form/ai-create-table/get-kimi-chat';
      const token = 'Bearer ' + userStore.getToken;
      const formData = {
        message: question.value,
      };
      const res = await fetch(url, {
        method: 'POST',
        headers: {
          Authorization: token,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify(formData),
      });

      const result = await res.json();
      if (result && result.data) {
        answer.value = result.data;
      } else {
        answer.value = 'ai无回复';
      }
      state.list.push({ question: question.value, answer: answer.value });
      question.value = '';
      uni.hideLoading()
    } catch (error) {
      uni.hideLoading()
    }
  };

 
</script>
<style lang="less" scoped>
  .ai-box {
    border: 1px solid #f2f2f2;
    font-size: 14px;
    background: #fff;

    .left-box {
      display: flex;
      align-items: center;
    }

    .right-box {
      border: 1px solid #f3f3f3;
      border-radius: 10px;
      margin: 10px;
      box-shadow: 0 5px 5px 0 #f3f3f3;

      .title {
        padding: 5px 10px;
        font-weight: bold;
        background: #fcfcfc;
        border-radius: 10px 10px 0 0;

        text {
          font-size: 18px;
          margin-right: 5px;
        }
      }

      .content {
        overflow: auto;
        padding: 10px 5px;
        height: 110px;
      }
    }

    .answer-box {
      color: rgb(0 0 0 / 80%);
    }

    .content {
      flex: 1;
      display: flex;
      flex-flow: column;
      min-height: 46px;
      box-sizing: border-box;
      max-width: 100%;
      position: relative;
      height: 170px;
      overflow: scroll;
    }
  }

  .item-box {
    display: flex;
    flex-direction: column;

    .text {
      width: 100%;
			word-break: break-all;
    }

    .item {
      display: flex;
    }

    .left {
      align-self: flex-start;
      flex-direction: column;
      max-width: calc(100% - 10px);
      margin-left: 10px;
    }

    .right {
      align-self: flex-end;
      background: #f3f3f3;
      padding: 5px 15px;
      border-radius: 5px;
      margin: 15px 10px;
      max-width: calc(100% - 20px);
    }
  }
	uni-textarea{
		width: auto;
	}
</style>
