<template>
  <div class="app-container">
    <!-- 对话框(添加 / 修改) -->
    <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" width="45%" v-dialogDrag append-to-body>
      <el-form ref="formRef" :model="labelData" :rules="formRules" v-loading="formLoading" label-width="100px">
        <el-form-item label="标签名称" prop="labelName">
          <el-input v-model="labelData.labelName" placeholder="请输入标签名称"/>
        </el-form-item>
        <el-form-item label="标签样式" prop="labelType">
          <el-select v-model="labelData.labelType" placeholder="请选择标签样式">
            <el-option v-for="dict in this.getDictDatas(DICT_TYPE.DICT_BASE_LABEL_TYPE)"
                       :key="dict.value" :label="dict.label" :value="dict.value"/>
          </el-select>
           实例：<el-tag :type="labelData.labelType">{{ labelData.labelName }}</el-tag>
        </el-form-item>
        <el-form-item label="标签描述" prop="labelContent">
          <el-input type="textarea" v-model="labelData.labelContent" placeholder="请输入标签描述"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm" :disabled="formLoading">确 定</el-button>
        <el-button @click="dialogVisible = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>

export default {
  name: "BusiLabelForm",
  components: {},
  data() {
    return {
      // 弹出层标题
      dialogTitle: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
      formLoading: false,
      // 表单参数
      labelData: {
        id: undefined,
        labelName: undefined,
        labelContent: undefined,
        labelType: undefined
      },
      // 表单校验
      formRules: {
        labelName: [
          {required: true, message: '请输入标签名称', trigger: 'blur'},
        ],
        labelType: [
          {required: true, message: '请选择标签样式', trigger: 'blur'},
        ],
      },
    };
  },
  methods: {
    /** 打开弹窗 */
    async open(row) {
      this.dialogVisible = true;
      this.reset();
      this.dialogTitle = row.cusName
      this.labelData.mainId = row.id
      // 修改时，设置数据
      // if (row) {
      //   this.formLoading = true;
      //   try {
      //     this.labelData = row;
      //   } finally {
      //     this.formLoading = false;
      //   }
      // }
    },
    /** 提交按钮 */
    async submitForm() {
      await this.$refs["formRef"].validate();
      //生成随机数
      this.labelData.id = Math.random()*10
      const data = this.labelData
      this.$emit('ok',data)
      this.dialogVisible = false
    },
    /** 表单重置 */
    reset() {
      this.labelData = {
        id: undefined,
        labelType: undefined,
        labelName: undefined,
        labelContent: undefined,
      };
      this.resetForm("formRef");
    }
  }
};
</script>
