<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogFormVisible"
    width="500px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item :label="__('标签名称')" prop="product_tag_name">
        <el-input
          v-model="form.product_tag_name"
          maxlength="5"
          :placeholder="__('标签名称')"
          show-word-limit
        />
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">{{ __('取消') }}</el-button>
      <el-button type="primary" @click="save">{{ __('确定') }}</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import { translateTitle as __ } from '@/utils/i18n'
  import { edit } from '@/api/store/product/tag'

  export default {
    name: 'StoreProductSellerTagEdit',
    data() {
      return {
        form: {
          product_tag_name: '',
        },
        rules: {
          product_tag_name: [
            { required: true, trigger: 'blur', message: this.__('标签名称') },
          ],
        },
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          let obj = Object.assign({}, row)
          this.form = {
            store_product_tag_id: obj.store_product_tag_id,
            product_tag_id: obj.product_tag_id,
            product_tag_name: obj.product_tag_name,
          }
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = {
          product_tag_name: '',
        }
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await edit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
