<script setup lang="ts">
import { AxiosResponse } from "axios";
import { log } from "console";
import { onMounted, reactive, ref } from "vue";
import { wrapTreeNode } from "../options/serveice";
import { GetProjectSplitSummary } from "./service";

// 对话列表
let dialogueList = reactive<any>([]);
// 打字机的文字
let printText = ref<string>("");
// 判断当前是否为打字状态
const isVisible = ref<boolean>(false);
// 用于显示已经打印了的内容
const displayedText = ref<string>("");
// 搜索的内容
const searchText = ref<string>("");

onMounted(async () => {
  await handleGetData();

  dialogueList[0] = {
    text: "",
    type: 1,
    searchType: "default",
  };
  printText.value =
    "您好！我是服务区商业智能助理小驿。\n您想了解服务区哪些方面的信息？";
  console.log("printText", printText.value);
  handlePrintText();

  // setTimeout(() => {
  //   dialogueList[0] = {
  //     text: "您好！我是服务区商业智能助理小驿。\n您想了解服务区哪些方面的信息？",
  //     type: 1,
  //     searchType: "default",
  //   };
  // }, 2000);
});

// 打字机效果
const handlePrintText = () => {
  let index = 0;
  displayedText.value = ""; // 清空当前显示的文本
  const interval = setInterval(() => {
    // 如果遇到换行符，则添加 <br> 标签
    if (printText.value[index] === "\n") {
      displayedText.value += "<br>";
    } else {
      displayedText.value += printText.value[index];
    }
    index++;

    // 当文本打印完成时停止定时器
    if (index === printText.value.length) {
      clearInterval(interval);
    }
  }, 100);
};

// 测试调用接口
const handleGetData = async () => {
  let req: any = {
    ServerpartId: "416",
    StatisticsMonth: "202411",
  };
  const data: any = await GetProjectSplitSummary(req);
  // let list: any = wrapTreeNode(data);
  console.log("data", data);
};
</script>

<template>
  <div class="main">
    <div class="content">
      <div
        v-for="(item, index) in dialogueList"
        :key="index"
        :class="item.type === 1 ? 'dialogueItem1' : 'dialogueItem2'"
      >
        <div
          v-html="index === dialogueList.length - 1 ? displayedText : item.text"
        ></div>
      </div>
    </div>

    <div class="InputBox">
      <div class="inputContent">
        <img
          class="voiceIcon"
          src="https://eshangtech.com/ShopICO/ahyd-BID/robot/microphone.png"
        />
        <div class="input">
          <el-input v-model="searchText" placeholder="有问题尽管问我～" />
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped lang='less'>
.main {
  width: 100vw;
  height: 100vh;
  background-image: url("https://eshangtech.com/ShopICO/ahyd-BID/robot/robotBackground.png");
  background-repeat: no-repeat;
  position: relative;
  .content {
    width: 100%;
    height: 100%;
    box-sizing: border-box;
    padding: 16px;
    .dialogueItem1 {
      max-width: 100%;
      box-sizing: border-box;
      padding: 12px 16px;
      background: #ffffff;
      border-radius: 0px 12px 12px 12px;
    }
    .dialogueItem2 {
      max-width: 100%;
      box-sizing: border-box;
      padding: 8px 12px;
      background: #2363ff;
      border-radius: 12px 0px 12px 12px;
    }
  }
  .InputBox {
    width: 100%;
    height: 90px;
    box-sizing: border-box;
    padding-bottom: 56px;
    background: #fff;
    position: fixed;
    bottom: 0;
    left: 0;
    .inputContent {
      width: 100%;
      height: 56px;
      box-sizing: border-box;
      padding: 8px 16px 4px;
      display: flex;
      align-items: center;
      .voiceIcon {
        width: 24px;
        height: 24px;
        margin-right: 8px;
      }
      .input {
        width: calc(100% - 32px);
        height: 100%;
        background: #f2f3f7;
        border-radius: 22px;
        display: flex;
        align-items: center;
        box-sizing: border-box;
        padding-left: 5px;
        .el-input {
          width: 95%;
          ::v-deep(.el-input__wrapper) {
            background-color: transparent !important;
            box-shadow: none !important;
          }
        }
      }
    }
  }
}
</style>