<template>
  <el-dialog
    append-to-body
    title="设置退款金额"
    :visible.sync="visible"
    width="400px"
    @close="handleClose">
    <el-form
      :model="form"
      ref="form"
      label-width="80px"
      style="padding:20px">
      <el-form-item
        verify
        label="退款金额">
        <el-input-number
          v-model="form.refund_price"
          :precision="2"
          :step="0.01"
          :min="0.01"></el-input-number>
      </el-form-item>
    </el-form>

    <span slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        type="primary"
        :loading="loading"
        @click="submit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { cancelPartTicket } from '@/api/order'
export default {
  computed: {
    salesList () {
      return this.$store.state.user.salesList
    }
  },
  data () {
    return {
      visible: false,
      loading: false,
      data: {},
      form: {
        refund_price: 0.01,
        id: ''
      }
    }
  },
  methods: {
    show (data) {
      this.visible = true
      this.data = data
      this.form.id = data.id
    },
    handleClose () {
      this.visible = false
    },
    submit () {
      if (this.form.refund_price > this.data.price) {
        this.$message.error('退款金额不得大于票价')
        return false
      }
      this.$confirm('确认退款', '提示', {
        confirmButtonText: '确认',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$refs.form.validate(valid => {
          if (valid) {
            let list = []
            list.push(this.form)
            cancelPartTicket({ list }).then(res => {
              this.$message.success('退款成功')
            }).finally(() => {
              this.$emit('change')
              this.loading = false
            })
          }
        })
        this.handleClose()
      })
    }
  }
}
</script>

<style lang="scss" scoped>
::v-deep .el-dialog .el-form-item {
  display: inline-block;
}
::v-deep .el-form-item__content{
    width: 200px;
}
</style>
