<template>
  <el-dialog
    custom-class="center-dialog"
    :title="title"
    top="0vh"
    :visible.sync="dialogFormVisible"
    width="600px"
    @close="close"
  >
    <el-form ref="form" label-width="80px" :model="form" :rules="rules">
      <el-form-item
        :label="__('商品计量单位父编号')"
        label-width="170px"
        prop="parent_unit_id"
      >
        <el-input
          v-model="form.parent_unit_id"
          :placeholder="__('商品计量单位父编号')"
        />
      </el-form-item>
      <el-form-item
        :label="__('商品计量单位名称')"
        label-width="170px"
        prop="unit_name"
      >
        <el-input
          v-model="form.unit_name"
          :placeholder="__('商品计量单位名称')"
        />
      </el-form-item>
      <el-form-item
        :label="__('商品计量单位注释')"
        label-width="170px"
        prop="unit_remark"
      >
        <el-input
          v-model="form.unit_remark"
          :placeholder="__('商品计量单位注释')"
        />
      </el-form-item>
      <el-form-item
        :label="__('换算关系')"
        label-width="170px"
        prop="unit_rate"
      >
        <el-input v-model="form.unit_rate" :placeholder="__('换算关系')" />
      </el-form-item>
      <el-form-item :label="__('gu编号')" label-width="170px" prop="unit_guid">
        <el-input v-model="form.unit_guid" :placeholder="__('gu编号')" />
      </el-form-item>
      <el-form-item
        :label="__('基本单位')"
        label-width="170px"
        prop="unit_is_base"
      >
        <el-input v-model="form.unit_is_base" :placeholder="__('基本单位')" />
      </el-form-item>
      <el-form-item
        :label="__('商品计量单位分类编号')"
        label-width="170px"
        prop="unit_type_id"
      >
        <el-input
          v-model="form.unit_type_id"
          :placeholder="__('商品计量单位分类编号')"
        />
      </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 { doEdit } from '@/api/base/product/unit'

  export default {
    name: 'BaseProductUnitEdit',
    data() {
      return {
        form: {},
        rules: {},
        title: '',
        dialogFormVisible: false,
      }
    },
    created() {},
    mounted() {
      this.form = {
        unit_name: '',
      }

      this.rules = {
        unit_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('商品计量单位编号'),
          },
        ],
        parent_unit_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('商品计量单位父编号'),
          },
        ],
        unit_name: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('商品计量单位名称'),
          },
        ],
        unit_remark: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('商品计量单位注释'),
          },
        ],
        unit_rate: [
          { required: true, trigger: 'blur', message: this.__('换算关系') },
        ],
        unit_guid: [
          { required: true, trigger: 'blur', message: this.__('gu编号') },
        ],
        unit_is_base: [
          { required: true, trigger: 'blur', message: this.__('基本单位') },
        ],
        unit_type_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('商品计量单位分类编号'),
          },
        ],
        unit_is_buildin: [
          { required: true, trigger: 'blur', message: this.__('系统内置') },
        ],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        if (!row) {
          this.title = this.__('添加')
        } else {
          this.title = this.__('编辑')
          this.form = Object.assign({}, row)
        }
        this.dialogFormVisible = true
      },
      close() {
        this.form = this.$options.data().form
        this.$refs['form'].resetFields()
        this.dialogFormVisible = false
      },
      save() {
        this.$refs['form'].validate(async (valid) => {
          if (valid) {
            const { msg, status } = await doEdit(this.form)
            if (200 == status) {
              this.$baseMessage(msg, 'success')
            } else {
              this.$baseMessage(msg, 'error')
            }
            this.$emit('fetch-data')
            this.close()
          } else {
            return false
          }
        })
      },
    },
  }
</script>
