<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="buyerUserId">
        <el-input
          v-model="formData.buyerUserId" maxlength="20" placeholder="客户ID" disabled
        />
      </el-form-item>
      <el-form-item label="代金券类型" prop="shelveState">
        <el-radio-group v-model="formData.voucherType">
          <el-radio label="1">充值代金券</el-radio>
          <el-radio label="2">兑换代金券</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item label="数量" prop="number">
        <el-input
          v-model="formData.number" clearable size="mini" type="number"
          style="width: 200px;"
          placeholder="请输入代金券数量"
        />
      </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 { updateTransferRechargeShopVoucher, updateTransferExchangeShopVoucher } from '@/api/voucherManagement/voucherOperation'

export default {
  name: 'VoucherTransfer',
  components: {
  },
  data() {
    return {
      modalOptions: {
        closeOnClickModal: false,
        width: '620px',
        title: ''
      },
      visible: false,
      formData: {
        voucherType: '1',
        buyerUserId: '',
        number: ''
      },
      formRules: {
        voucherType: [ { required: true, message: '请选择代金券类型' } ],
        buyerUserId: [ { required: true, message: '被赠与方id不能为空', trigger: 'blur' } ],
        number: [ { required: true, message: '数量不能为空', trigger: 'blur' } ]
      }
    }
  },
  methods: {
    handleClose() {
      this.visible = false
    },
    initList() {
    },
    handleOpen(params = {}) {
      this.modalOptions.title = '代金券转赠'
      this.formData.buyerUserId = params.buyerUserId
      // this.formData = Object.assign(this.$options.data().formData, params)
      this.visible = true
      this.initList()
      this.$refs.formData && this.$refs.formData.resetFields()
    },
    handleSubmit() {
      this.$refs.formData.validate(async (valid) => {
        if (valid) {
          const loading = this.$loading({ text: '加载中' })
          try {
            const { ...otps } = this.formData
            const params = {
              ...otps
            }
            let _url
            if (this.formData.voucherType === '1') {
              _url = updateTransferRechargeShopVoucher
            } else if (this.formData.voucherType === '2') {
              _url = updateTransferExchangeShopVoucher
            }
            await _url(params)
            loading.close()
            this.$message({ message: `操作成功!`, type: 'success' })
            this.$emit('success')
            this.visible = false
          } catch (e) {
            loading.close()
          } finally {
            loading.close()
          }
        } else {
          this.$message({ message: '请输入相关信息', type: 'warning' })
          return false
        }
      })
    }
  }
}
</script>

