<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-option
                          v-for="(item,index) in tableList"
                          :key="item.id"
                          :label="item.name"
                          :value="index"
                        /> -->
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-input v-model="dataIntro" v-if="showSearch" @keyup.enter="retrieval"
            ><!-- v-model="dataForm.intro" -->
            <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>
  
      <!-- <el-dialog
                  :visible.sync="showConfirm"
                  title="数据确认"
                  width="500"
                  align-center
                  :append-to-body="true"
                >
                  <div>当前工作表：{{position.sheet}} </div>
                  <div>当前取值：{{position.value}}</div>
                  <template #footer>
                    <div class="dialog-footer">
                      <el-button @click="showConfirm = false">关闭</el-button>
                      <el-button type="primary" @click="onConfirm">
                        确认选择
                      </el-button>
                    </div>
                  </template>
                </el-dialog> -->
    </div>
  </template>
    
    <script>
  import { searchSourceData, deleteSourceData } from "@/api/sourceData";
  /* import { search } from "core-js/fn/symbol"; */
  import LuckyExcel from "luckyexcel"; //引入LuckyExcel
  import axios from "axios";
  export default {
    name: "SourceEs",
    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/3f84a841-cefd-44b3-9555-568cc3b6c2d2?stream=false",
            {
              input_value: this.dataIntro,
              output_type: "chat",
              input_type: "chat",
              tweaks: {
                "ChatInput-em6qC": {},
                "ParseData-yO3YQ": {},
                "Prompt-Wj75b": {},
                "ChatOutput-zy9na": {},
                "SplitText-O1knk": {},
                "File-4j6Zd": {},
                "OllamaEmbeddings-xbYXX": {},
                "Chroma-OIejP": {},
                "OllamaModel-04mEO": {},
                "OllamaEmbeddings-UGvLP": {},
                "Chroma-HzukO": {},
              },
            },
            {
              headers: {
                "Content-Type": "application/json",
              },
            }
          );
  
          if (response.status === 200) {
            
            // 假设返回的数据在 response.data.outputs[0].outputs[0].results.message.data.text 中
            this.textInfo =
              response.data.outputs[0].outputs[0].results.message.data.text;
              // 将搜索词和生成的内容存储在 searchResult 对象中
            /* 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;
  
        /*  item.sourceData = JSON.parse(item.source_data);
  
        _this.position.tb = item.code; */
        /*      _this.$nextTick(() => {
          luckysheet.destroy();
          luckysheet.create({
            container: "luckysheet", //dom id
            data: item.sourceData.exportJson.sheets, //excel数据
            lang: "zh",
            showinfobar: false, // 是否显示顶部信息栏
            showstatisticBar: false, // 是否显示底部计数栏
            sheetBottomConfig: false, // sheet页下方的添加行按钮和回到顶部按钮配置
            allowEdit: false, // 是否允许前台编辑
            enableAddRow: false, // 是否允许增加行
            enableAddCol: false, // 是否允许增加列
            sheetFormulaBar: false, // 是否显示公式栏
            enableAddBackTop: false, // 返回头部按钮
            showsheetbar: true, // 是否显示底部sheet页按钮
            enableAddRow: false, //允许添加行
            // 自定义配置底部sheet页按钮
            showsheetbarConfig: {
              add: false,
              menu: false,
            },
            hook: {
              cellMousedown: function (cell, position, sheetFile, ctx) {
                _this.position.sheet = sheetFile.name;
  
                _this.position.r = position.r;
                _this.position.c = position.c;
  
                if (cell.v) {
                  _this.position.value = cell.v;
                } else {
                  _this.position.value = "";
                  cell.ct.s.map((res) => {
                    _this.position.value += res.v;
                  });
                }
                _this.showConfirm = true;
              },
              cellUpdated: function (r, c, oldValue, newValue, isRefresh) {
              },
            },
          });
        }); */
      },
      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">
 
  </style>
    