<template>
  <el-dialog
    :title="!dataForm.transactionId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户id" prop="userId">
      <el-input v-model="dataForm.userId" placeholder="用户id"></el-input>
    </el-form-item>
    <el-form-item label="充值 支付 赠送优惠券....." prop="orderType">
      <el-input v-model="dataForm.orderType" placeholder="充值 支付 赠送优惠券....."></el-input>
    </el-form-item>
    <el-form-item label="变动金额" prop="changeAmount">
      <el-input v-model="dataForm.changeAmount" placeholder="变动金额"></el-input>
    </el-form-item>
    <el-form-item label="关联id" prop="relationId">
      <el-input v-model="dataForm.relationId" placeholder="关联id"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
    </el-form-item>
    <el-form-item label="余额" prop="userBalance">
      <el-input v-model="dataForm.userBalance" placeholder="余额"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          transactionId: 0,
          userId: '',
          orderType: '',
          changeAmount: '',
          relationId: '',
          remark: '',
          userBalance: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          orderType: [
            { required: true, message: '充值 支付 赠送优惠券.....不能为空', trigger: 'blur' }
          ],
          changeAmount: [
            { required: true, message: '变动金额不能为空', trigger: 'blur' }
          ],
          relationId: [
            { required: true, message: '关联id不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ],
          userBalance: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.transactionId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.transactionId) {
            this.$http({
              url: this.$http.adornUrl(`/book/transactiondetails/info/${this.dataForm.transactionId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.transactionDetails.userId
                this.dataForm.orderType = data.transactionDetails.orderType
                this.dataForm.changeAmount = data.transactionDetails.changeAmount
                this.dataForm.relationId = data.transactionDetails.relationId
                this.dataForm.remark = data.transactionDetails.remark
                this.dataForm.userBalance = data.transactionDetails.userBalance
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/book/transactiondetails/${!this.dataForm.transactionId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'transactionId': this.dataForm.transactionId || undefined,
                'userId': this.dataForm.userId,
                'orderType': this.dataForm.orderType,
                'changeAmount': this.dataForm.changeAmount,
                'relationId': this.dataForm.relationId,
                'remark': this.dataForm.remark,
                'userBalance': this.dataForm.userBalance
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
