<!--
 * @Description: 添加/编辑采集标签弹出框组件
 * @Author: liusheng
 * @Date: 2024-07-03 16:30:56
-->
<template>
  <div>
    <el-form :ref="formName" :model="gatherTagForm" :rules="gatherTagRules" label-width="100px">
      <el-form-item label="标签名称：" prop="name">
        <el-input v-model="gatherTagForm.name" style="width:175px" size="small" clearable placeholder="请输入标签名称" />
      </el-form-item>
      <el-form-item label="数据字段：" prop="field">
        <el-input v-model="gatherTagForm.field" style="width:175px" size="small" clearable placeholder="请输入数据字段名"/>
      </el-form-item>
      <el-form-item label="数据类型：" prop="dataType">
        <el-select style="width:175px;" v-model="gatherTagForm.dataType" filterable
          default-first-option size="small" clearable placeholder="请选择数据类型">
          <el-option v-for="(type, index) in dataTypelist" :key="index" :label="type" :value="type" />
        </el-select>
      </el-form-item>
    </el-form>
    <div style="text-align:center; margin:0" >
      <el-button type="text" size="mini" @click="handleCancel">取消</el-button>
      <el-button type="primary" size="mini" @click="handleConfirm">确定</el-button>
    </div>
  </div>
</template>

<script>
import aupUtil from '../../common/AupUtil.js';

export default {
  name: 'EditGatherTagForm',
  props: ['mode', 'visible', 'tagSetIndex', 'tagSet', 'tagIndex'],
  
  data() {
    return {
      formName: 'gatherTagForm',

      dataTypelist: ['INT', 'DOUBLE', 'STRING'],
      gatherTagForm: {},
			gatherTagRules: {
				name: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { type: 'string', pattern: aupUtil.tagNamePattern, message: aupUtil.tagNameErrMsg, trigger: 'blur' },
					{ min: 1, max: 20, message: '最大长度不超过20个字符', trigger: 'blur' },
				],
        field: [
          { required: true, message: '请输入采集字段名称', trigger: 'blur' },
          { type: 'string', pattern: /^([a-zA-Z0-9])+$/, message: '只能输入字母或数字', trigger: 'blur' },
					{ min: 1, max: 20, message: '最大长度不超过20个字符', trigger: 'blur' },
				],
        dataType: [
          { required: true, message: '请选择数据类型', trigger: 'blur' },
				]
			}
    }
  },

  methods: {
    handleConfirm() {
      this.$refs[this.formName].validate((valid) => {
        if (!valid) return false;
        this.$emit("confirm", this.tagSet, this.gatherTagForm);
      });
    },
    
    handleCancel() {
      this.$emit("close", this.tagSetIndex);
    },

    handleKeyDown(event) {
      if (event.key === 'Escape') {
        this.handleCancel();
      }
    },
  },

  watch: {
    visible(newVal) {
      if (!newVal) return;
      this.$refs[this.formName].clearValidate();
      this.gatherTagForm = (this.mode === 'add') ? {}
        : JSON.parse(JSON.stringify(this.tagSet.tagList[this.tagIndex]));
    }
  },

  mounted() {
    // 添加全局键盘监听事件
    window.addEventListener('keydown', this.handleKeyDown);
  },

  beforeDestroy() {
    // 移除全局键盘监听事件
    window.removeEventListener('keydown', this.handleKeyDown);
  },
}
</script>

<style scoped>
.text {
  padding: 12px 0 15px 5px;
  font-size: 15px;
}
</style>
