1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
<template>
  <div class="rule-selector">
    <el-autocomplete size="mini" v-model="inputValue" :fetch-suggestions="querySearch" placeholder="一句话生成算法"
      :trigger-on-focus="true" value-key="fileName" @select="handleSelect" @blur="handleBlur" clearable
      @clear="handleClear"
      class="rule-input">
      <template slot-scope="{ item }">
        <div class="rule-item">
          <!-- <div v-if="item.iconUrl" class="rule-icon">
            <img :src="item.iconUrl" alt="icon" />
          </div> -->
          <div class="rule-text" v-html="highlightMatch(item.fileName)"></div>
        </div>
      </template>
    </el-autocomplete>
  </div>
</template>
 
<script>
export default {
  name: 'RuleSelector',
  props: {
    selectedRule: {
    type: Object,
    default: null
  },
    // 初始化的规则列表
    initialRules: {
      type: Array,
      default: () => [
      ]
    }
  },
  data() {
    return {
      inputValue: this.selectedRule ? this.selectedRule.fileName : '', // 输入框的值
      rules: [],      // 规则列表(包含系统内置和新增)
      searchTimeout: null,
      currentQuery: ''
    };
  },
  watch: {
    selectedRule: {
    immediate: true,
    handler(newRule) {
      if (newRule && newRule.fileName) {
        this.inputValue = newRule.fileName;
      } else {
        this.inputValue = '';
      }
    }
  },
    initialRules: {
      immediate: true,
      handler(newVal) {
        this.rules = [...newVal];
      }
    }
  },
  methods: {
    // 查询匹配的规则
    querySearch(queryString, cb) {
      this.currentQuery = queryString;
      clearTimeout(this.searchTimeout);
 
      // 同步处理空查询场景
      if (!queryString.trim()) {
        cb([...this.rules]);
        return;
      }
 
      // 仅对实际输入进行防抖
      this.searchTimeout = setTimeout(() => {
        const results = this.rules.filter(this.filterRule.bind(this, queryString));
        cb(results);
      }, 300);
    },
 
    // 过滤规则
    filterRule(queryString, rule) {
      return rule.warningRules.toLowerCase().includes(queryString.toLowerCase());
    },
 
    // 高亮匹配文本
    highlightMatch(text) {
      if (!this.currentQuery) return text;
 
      const escapedQuery = this.currentQuery.replace(
        /[.*+?^${}()|[\]\\]/g, '\\$&'
      );
      const regex = new RegExp(`(${escapedQuery})`, 'gi');
 
      return text.replace(regex, '<span class="highlight">$1</span>');
    },
 
    // 清空输入框
    handleClear() {
      this.$emit('rule-created');
    },
    // 选择规则
    handleSelect(item) {
      this.$emit('rule-selected', item);
    },
 
    // 处理输入框失焦
    handleBlur() {
      if (!this.inputValue) return;
 
      // 检查是否已有相同规则
      const existingRule = this.rules.find(
        rule => rule.warningRules === this.inputValue
      );
 
      if (existingRule) {
        this.handleSelect(existingRule);
        return;
      }
 
      // 添加新规则
      const newRule = {
        ruleId: Date.now(), // 临时ID(实际应由服务器生成)
        // fileName: `自定义规则-${new Date().toLocaleDateString()}`,
        fileName: this.inputValue,
        warningRules: this.inputValue,
        iconUrl: '/opt/smart/icon/task_icon.png',
        rangeValue: 50,
        createTime: new Date().toISOString(),
        createUser: 1 // 假设当前用户ID为1
      };
 
      this.rules.push(newRule);
      this.$emit('rule-created', newRule);
    }
  }
};
</script>
 
<style scoped>
.rule-selector {
  width: 100%;
}
 
.rule-input {
  width: 100%;
}
 
.rule-item {
  text-align: left;
  display: flex;
  align-items: center;
  padding: 8px 0;
}
 
.rule-icon {
  margin-right: 10px;
}
 
.rule-icon img {
  width: 24px;
  height: 24px;
  border-radius: 4px;
  object-fit: cover;
}
 
.rule-text {
  flex: 1;
}
</style>
 
<style>
.highlight {
  color: blue;
  font-weight: bold;
}
</style>