<template>
  <div v-loading="loading">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="150px" class="order_form">
      <el-form-item label="标签名称" prop="TagName">
        <el-input v-model="ruleForm.TagName" placeholder="请输入标签名称" :disabled="addEditJudgment" />
      </el-form-item>
      <el-form-item v-if="addEditJudgment" label="新标签名称" prop="NewTagName">
        <el-input v-model="ruleForm.NewTagName" placeholder="请输入标签名称"/>
      </el-form-item>
      <el-form-item label="标签分组" prop="CLass">
        <el-select v-model="ruleForm.CLass" class="form_select" :disabled="addEditJudgment" clearable placeholder="请选择标签分组">
          <el-option label="请选择标签分组" value=""></el-option>
          <el-option label="系统标签" value="1"></el-option>
          <el-option label="基础标签" value="2"></el-option>
          <el-option label="活动标签" value="3"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm">保存</el-button>
        <el-button @click="resetForm" v-if="addEditReset" >重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { addCommodityLabel, editCommodityLabel } from '@/api/commodityManagement/commodityLabel'
import { validateTel } from '@/utils/validate'
import { filterContent } from '@/utils'

export default {
  name: 'AddOrEdit',
  props: {
    rowData: {
      type: Object,
      default: {}
    },
    layerid: {
      type: String,
      default: ''
    }
  },
  data() {
    return {
      props: { // 自定义省市下拉数据的key值
        value: 'code',
        label: 'name',
        children: 'entityJson'
      },
      sourceOptions: [], // 工单来源下拉数据
      ruleForm: {
        Type: 1, //商品标签
        TagName: '', //标签名称
        NewTagName: '', //新标签名称
        CLass: '', //标签分组
      },
      rules: {
        TagName: [{
          required: true,
          trigger: 'blur',
          message: '请输入标签名称',
        }],
        NewTagName: [{
          required: true,
          trigger: 'blur',
          message: '请输入新标签名称',
        }],
        CLass: [{
          required: true,
          trigger: 'blur',
          message: '请选择标签分组',
        }]
      },
      loading: false,
      addEditJudgment: false,
      addEditReset: true
    }
  },
  created() {
    Promise.all([])
      .then(() => {
        if (this.rowData.F_TagName) {
          this.addEditJudgment = true
          this.addEditReset = false
          this.ruleForm.TagName = this.rowData.F_TagName
          this.ruleForm.CLass = this.rowData.F_CLass + ""
        }
      })
  },
  methods: {
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          this.ruleForm.content = filterContent.delHtmlTag(this.ruleForm.content)// 保存内容时去掉 html 标签
          this.loading = true
          // 添加
          if (!this.rowData.F_TagName) {
            addCommodityLabel(this.ruleForm).then(response => {
              this.loading = false
              if (response.state.toLowerCase() === 'success') {
                this.$parent.$layer.close(this.layerid)
                this.$parent.getList() // 重新加载父级数据
                this.$message.success('恭喜你，添加成功!')
              }
            }).catch(() => {
              this.loading = false
            })
            return
          }
          // 编辑
          editCommodityLabel(this.ruleForm).then(response => {
            this.loading = false
            if (response.state.toLowerCase() === 'success') {
              this.$parent.$layer.close(this.layerid)
              this.$parent.getList() // 重新加载父级数据
              this.$message.success('恭喜你，编辑成功!')
            }
          }).catch(() => {
            this.loading = false
          })
        } else {
          this.$message.error('请输入有效的必填项信息!')
          return false
        }
      })
    },
    resetForm() {
      this.$refs.ruleForm.resetFields()
    },
  }
}
</script>

<style rel="stylesheet/scss" lang="scss">
.order_form .form_select{
	width: 100%;
}
</style>