<template>
  <div class="app-container">

    <el-card style="margin-bottom: 10px;">
      <h3>转赠</h3>
      <el-form ref="dataForm" :rules="rules" :model="dataForm" label-width="150px">
        <el-form-item label="商家ID（赠与方）" prop="shopId">
          <el-input v-model="dataForm.shopId" type="number" />
        </el-form-item>
        <el-form-item label="用户ID（被赠与方）" prop="buyerUserId">
          <el-input v-model="dataForm.buyerUserId" type="number" />
        </el-form-item>
        <el-form-item label="数量" prop="number">
          <el-input v-model="dataForm.number" type="number" />
        </el-form-item>

      </el-form>
    </el-card>

    <div class="op-container">
      <el-button type="primary" @click="handlePublish">
        转赠
      </el-button>
    </div>
  </div>
</template>

<script>
import { operateVoucherShopHold } from '@/api/voucherManagement/voucherTransfer'

export default {
  name: 'VoucherTransfer',
  data() {
    return {
      dataForm: {
        shopId: '',
        buyerUserId: '',
        number: ''
      },
      rules: {
        shopId: [ { required: true, message: '赠与方id不能为空', trigger: 'blur' } ],
        buyerUserId: [ { required: true, message: '被赠与方id不能为空', trigger: 'blur' } ],
        number: [ { required: true, message: '数量不能为空', trigger: 'blur' } ]
      }
    }
  },
  mounted() {
    this.initData()
  },
  methods: {
    initData() {
      this.dataForm.shopId = this.$route.query.shopId || ''
    },
    handlePublish() {
      this.$refs.dataForm.validate((valid) => {
        if (valid) {
          operateVoucherShopHold(this.dataForm)
            .then((res) => {
              this.$notify.success({
                title: '操作成功',
                message: res.data.message
              })
              this.resetForm()
            })
            .catch((err) => {
              console.log(err)
            })
        }
      })
    },
    resetForm() {
      this.dataForm.buyerUserId = ''
      this.dataForm.number = ''
    }
  }

}
</script>
