<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :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="rechargeAmount">
      <el-input v-model="dataForm.rechargeAmount" placeholder="充值金额"></el-input>
    </el-form-item>
    <el-form-item label="充值渠道" prop="rechargeChannel">
      <el-input v-model="dataForm.rechargeChannel" placeholder="充值渠道"></el-input>
    </el-form-item>
    <el-form-item label="订单号" prop="orderId">
      <el-input v-model="dataForm.orderId" placeholder="订单号"></el-input>
    </el-form-item>
    <el-form-item label="实际充值金额" prop="realAmount">
      <el-input v-model="dataForm.realAmount" placeholder="实际充值金额"></el-input>
    </el-form-item>
    <el-form-item label="充值状态" prop="rechargeStatus">
      <el-input v-model="dataForm.rechargeStatus" placeholder="充值状态"></el-input>
    </el-form-item>
    <el-form-item label="下单时间" prop="createTime">
      <el-input v-model="dataForm.createTime" placeholder="下单时间"></el-input>
    </el-form-item>
    <el-form-item label="支付成功时间" prop="successTime">
      <el-input v-model="dataForm.successTime" 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: {
          id: 0,
          userId: '',
          rechargeAmount: '',
          rechargeChannel: '',
          orderId: '',
          realAmount: '',
          rechargeStatus: '',
          createTime: '',
          successTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '用户id不能为空', trigger: 'blur' }
          ],
          rechargeAmount: [
            { required: true, message: '充值金额不能为空', trigger: 'blur' }
          ],
          rechargeChannel: [
            { required: true, message: '充值渠道不能为空', trigger: 'blur' }
          ],
          orderId: [
            { required: true, message: '订单号不能为空', trigger: 'blur' }
          ],
          realAmount: [
            { required: true, message: '实际充值金额不能为空', trigger: 'blur' }
          ],
          rechargeStatus: [
            { required: true, message: '充值状态不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '下单时间不能为空', trigger: 'blur' }
          ],
          successTime: [
            { required: true, message: '支付成功时间不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/book/paypaymentorder/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.payPaymentOrder.userId
                this.dataForm.rechargeAmount = data.payPaymentOrder.rechargeAmount
                this.dataForm.rechargeChannel = data.payPaymentOrder.rechargeChannel
                this.dataForm.orderId = data.payPaymentOrder.orderId
                this.dataForm.realAmount = data.payPaymentOrder.realAmount
                this.dataForm.rechargeStatus = data.payPaymentOrder.rechargeStatus
                this.dataForm.createTime = data.payPaymentOrder.createTime
                this.dataForm.successTime = data.payPaymentOrder.successTime
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/book/paypaymentorder/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'userId': this.dataForm.userId,
                'rechargeAmount': this.dataForm.rechargeAmount,
                'rechargeChannel': this.dataForm.rechargeChannel,
                'orderId': this.dataForm.orderId,
                'realAmount': this.dataForm.realAmount,
                'rechargeStatus': this.dataForm.rechargeStatus,
                'createTime': this.dataForm.createTime,
                'successTime': this.dataForm.successTime
              })
            }).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>
