<template>
  <el-dialog
    :visible.sync="visible"
    v-bind="modalOptions"
    append-to-body
  >
    <el-form
      ref="formData"
      :model="formData"
      :rules="formRules"
      size="mini"
      label-suffix=":"
      label-width="150px"
    >
      <el-form-item label="商品ID" prop="productId">
        <el-input
          v-model="formData.productId" maxlength="20" placeholder="商品ID" disabled
        />
      </el-form-item>
      <el-form-item label="虚拟销量" prop="goodsNum">
        <el-input-number v-model="formData.fictitiousNumber" :precision="0" :min="1" :max="999999999" />
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button size="mini" @click="handleClose">取 消</el-button>
        <el-button type="primary" size="mini" @click="handleSubmit">确 定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script>
import { setFictitious } from '@/api/commodity'

export default {
  name: 'RegistrationReview',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '620px',
        title: ''
      },
      visible: false,
      formData: {
        productId: '',
        fictitiousNumber: 0
      },
      formRules: {
        productId: [
          { required: true, message: '缺少商品ID' }
        ],
        fictitiousNumber: [
          { type: 'number', required: true, message: '请输入虚拟销量' }
        ]
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = '自定义虚拟销量'
      this.$refs.formData && this.$refs.formData.resetFields()
      this.formData.productId = params.productId
      this.formData.fictitiousNumber = params.fictitiousNumber || 0
      // this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      this.initList()
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            await setFictitious(params)
            loading.close()
            this.$message({ message: `虚拟销量已设置：${this.formData.fictitiousNumber}!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

