<template>
  <div class="com-formual">
    <el-form>
      <el-form-item>
        <el-select
          v-model="tableCode"
          placeholder="请选择模型"
          @change="onChangeTab"
          size="large"
          style="width: 100%"
        >
          <el-option label="测试模型" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-input v-model="dataIntro" v-if="showSearch" @keyup.enter="retrieval"
          >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="retrieval"
          ></el-button
        ></el-input>
      </el-form-item>
      <el-form-item
        style="min-height: 200px"
        v-if="fileLoadStatus"
        v-loading="loading"
      >
        <div>当前回答内容：{{ textInfo }}</div>
        <div
          class="dialog-footer"
          style="display: flex; justify-content: flex-end"
        >
          <el-button type="primary" @click="onConfirm"> 确认选择 </el-button>
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>
  
  <script>
import { searchSourceData, deleteSourceData } from "@/api/sourceData";
import axios from "axios";
export default {
  name: "SourceAi",
  emits: ["onPicked"],

  components: {},
  data() {
    return {
      tableCode: "",
      tableList: [],
      showConfirm: false,
      fileLoadStatus: false,
      showSearch: false,
      position: {
        tb: "",
        c: 0,
        r: 0,
        sheet: "",
        value: "",
      },
      dataIntro: "",
      textInfo: "",
      loading: false, // 增加 loading 状态
      searchResult: {},
    };
  },
  mounted() {
    this.initTableList();
  },
  methods: {
    /* ai检索 */
    async retrieval() {
      this.fileLoadStatus = true;
      this.loading = true; // 开始加载动画
      try {
        const response = await axios.post(
          "http://58.246.234.210:7860/api/v1/run/c7ea3a2d-f50c-4936-b96c-3fe7c34fa062?stream=false",
          {
            input_value: this.dataIntro,
            output_type: "chat",
            input_type: "chat",
            tweaks: {
              "ChatInput-MbQRf": {},
              "ParseData-UiiZo": {},
              "Prompt-L8PiG": {},
              "ChatOutput-MdiWR": {},
              "SplitText-JIbWW": {},
              "File-1i88L": {},
              "OllamaEmbeddings-4TSqH": {},
              "Milvus-Wotjl": {},
              "OllamaModel-j2Vj0": {},
              "Milvus-fivEN": {},
              "OllamaEmbeddings-e409J": {},
            },
          },
          {
            headers: {
              "Content-Type": "application/json",
            },
          }
        );

        if (response.status === 200) {
          // Assuming the response structure is the same
          this.textInfo =
            response.data.outputs[0].outputs[0].results.message.data.text;
          this.searchResult = {
            searchTerm: this.dataIntro,
            result: this.textInfo,
          };
        }
      } catch (error) {
        console.error("Error during retrieval:", error);
        this.$message.error("检索失败，请稍后重试");
      } finally {
        this.loading = false; // 结束加载动画
      }
    },

    onConfirm(e) {
      /*       this.showConfirm = false; */
      this.$emit("onPicked", this.searchResult);
    },

    onChangeTab(e) {
      let _this = this;
      /*      let item = _this.tableList[e]; */
      _this.showSearch = true;
    },
    initTableList() {
      let _this = this;
      searchSourceData({ page: 1, pageSize: 999, status: 5 }).then((res) => {
        if (res.status != 200) return;
        _this.tableList = res.data.dataList;
      });
    },
  },
};
</script>
  
  <style lang="scss">
@import "./index.scss";
</style>
  