<template>
  <el-dialog title="核销优惠券" :visible.sync="dialogFormVisible" ref="dialog" width="450px" @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="优惠券编码" prop="code">
        <el-input v-model.trim="form.code" clearable autocomplete="off" onKeypress="return (/[\d]/.test(String.fromCharCode(event.keyCode)))"></el-input>
      </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>
export default {
  name: 'Verification',
  data() {
    return {
      form: {
        code: undefined,
      },
      rules: {
        code: [{ required: true, trigger: 'blur', message: '请输入优惠券编码' }],
      },
      dialogFormVisible: false,
    }
  },
  created() { },
  methods: {
    // 打开弹窗
    open() {
      this.form = Object.assign({}, {})
      this.dialogFormVisible = true
    },
    // 关闭弹窗
    close() {
      this.$refs['form'].resetFields()
      this.form = this.$options.data().form
      this.dialogFormVisible = false
    },
    // 确认核销
    save() {
      this.$refs['form'].validate(async (valid) => {
        if (valid) {
          if (this.form.hotelAssetId) {
            const form = Object.assign({}, this.form)
            form.depreciationRate = parseFloat(form.depreciationRate)
            form.netSalvage = parseFloat(form.netSalvage)
            doEdit(form).then((res) => this.callbackFun(res))
          } else {
            const form = Object.assign({}, this.form)
            form.depreciationRate = parseFloat(form.depreciationRate)
            form.netSalvage = parseFloat(form.netSalvage)
            doAdd(form).then((res) => this.callbackFun(res))
          }
        } else {
          return false
        }
      })
    },
    callbackFun(res) {
      if (res.success) {
        this.$baseMessage(res.msg, 'success')
        this.$emit('fetch-data')
        this.close()
      }
    },
  },
}
</script>
<style scoped>
</style>
