<template>
  <div class="com-formual">
    <el-button @click="searchEx">检索</el-button>

    <!-- 显示匹配到的结果 -->
    <div v-if="matchResults.length" class="match-results">
      <div
        v-for="(item, index) in matchResults"
        :key="index"
        class="match-item"
      >
        <div class="match-content">
          <span>原始值: {{ item }}</span>
          <span>替换后: {{ item + "1" }}</span>
        </div>
        <div class="actions">
          <el-button size="small" @click="replaceItem(item)">替换</el-button>
        </div>
      </div>
      <el-button type="primary" @click="replaceAll">批量替换</el-button>
    </div>
  </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"],
  props: {
    TemList: {
      type: Array,
      default: [],
    },
  },
  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: {},
      matchResults: [], // 存储匹配到的结果
    };
  },
  mounted() {
    this.initTableList();
  },
  methods: {
    searchEx() {
      let allMatches = [];

      this.TemList.forEach((el) => {
        const matches = el.content.match(
          /\[[A-Z0-9]+-[A-Z0-9]+-[0-9]+-[0-9]+\]/g
        );
        if (matches) {
          allMatches = [...allMatches, ...matches];
        }
      });

      // 去重
      this.matchResults = [...new Set(allMatches)];
      console.log("匹配到的结果：", this.matchResults);
    },

    // 单个替换
    replaceItem(item) {
      this.$confirm(`确认将 ${item} 替换为 ${item}1?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 这里添加替换逻辑
          this.$message.success("替换成功");
        })
        .catch(() => {
          // 取消操作
        });
    },

    // 批量替换
    replaceAll() {
      this.$confirm(
        `确认批量替换 ${this.matchResults.length} 项内容?`,
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          // 这里添加批量替换逻辑
          this.$message.success("批量替换成功");
        })
        .catch(() => {
          // 取消操作
        });
    },
    /* 检索当前模版的所有内容 */
    /*  searchEx() {
      console.log("TemList", this.TemList);
      this.TemList.map((el) => {
         const matches = el.content.match(/\[[A-Z0-9]+-[A-Z0-9]+-[0-9]+-[0-9]+\]/g);
        console.log("matches",matches);
        return matches || []; // 如果没有匹配到返回空数组
        el.content = el.content.replace(
          /\[[A-Z0-9]+-[A-Z0-9]+-[0-9]+-[0-9]+\]/g,
          (match) => {
            // match 是匹配到的原始文本，例如 [FT7-RP-00-012]
            // 这里可以根据需求对 match 进行处理
            return `${match}1`; // 例如：替换后的[FT7-RP-00-012]
            // 或者返回其他处理结果
          }
        );
        return el;
      });
    }, */
    /* ai检索 */
    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" scoped>
.match-results {
  margin-top: 20px;
  
  .match-item {
    padding: 10px;
    border-bottom: 1px solid #eee;
    display: flex;
    justify-content: space-between;
    align-items: center;
    
    .match-content {
      flex: 1;
      display: flex;
      gap: 20px;
    }
    
    .actions {
      margin-left: 10px;
    }
  }
}
</style>
    