<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="warehouse_item_quantity_min">
        <el-input
          v-model="form.warehouse_item_quantity_min"
          :placeholder="__('库存上限')"
        />
      </el-form-item>
      <el-form-item :label="__('库存下限')" prop="warehouse_item_quantity_max">
        <el-input
          v-model="form.warehouse_item_quantity_max"
          :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/warehouse/base'

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

      this.rules = {
        warehouse_item_id: [
          { required: true, trigger: 'blur', message: this.__('仓库商品编号') },
        ],
        product_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('商品编号（冗余）'),
          },
        ],
        unit_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('商品单位id（冗余）'),
          },
        ],
        item_id: [
          {
            required: true,
            trigger: 'blur',
            message: this.__('goods_info_id'),
          },
        ],
        category_id: [
          { required: true, trigger: 'blur', message: this.__('') },
        ],
        warehouse_id: [
          { required: true, trigger: 'blur', message: this.__('仓库编号') },
        ],
        warehouse_item_cost: [
          { required: true, trigger: 'blur', message: this.__('成本') },
        ],
        warehouse_item_quantity: [
          { required: true, trigger: 'blur', message: this.__('数量') },
        ],
        warehouse_item_lock_quantity: [
          { required: true, trigger: 'blur', message: this.__('锁定库存') },
        ],
        warehouse_item_quantity_min: [
          { required: true, trigger: 'blur', message: this.__('') },
        ],
        warehouse_item_quantity_max: [
          { required: true, trigger: 'blur', message: this.__('') },
        ],
        store_id: [{ required: true, trigger: 'blur', message: this.__('') }],
      }

      this.$forceUpdate()
    },
    methods: {
      __,
      showEdit(row) {
        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>
