<!--
 * @Description: 添加/编辑计算型标签弹出框组件
 * @Author: liusheng
 * @Date: 2024-07-03 16:30:56
-->
<template>
  <div>
    <el-form :ref="formName" :model="compTagForm" :rules="compTagRules" label-width="100px">
      <el-form-item label="标签名称：" prop="name">
        <el-input v-model="compTagForm.name" size="small" clearable placeholder="请输入标签名称" />
      </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: 'CompTagForm',
  props: ['mode', 'visible', 'tagSetIndex', 'tagSet', 'tagIndex'],

  data() {
    return {
      formName: 'compTagForm',

      compTagForm: {},
			compTagRules: {
				name: [
          { required: true, message: '请输入标签名称', trigger: 'blur' },
          { type: 'string', pattern: aupUtil.tagNamePattern, message: aupUtil.tagNameErrMsg, trigger: 'blur' },
					{ min: 1, max: 20, message: '最大长度不超过20个字符', trigger: 'blur' },
				],
			}
    }
  },

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

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

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