<template>
  <el-dialog
    title="预付款充值/扣款"
    :visible.sync="visible"
    width="400px"
    @close="handleClose">
    <el-form
      :model="form"
      ref="form"
      inline
      style="padding:20px;"
      label-width="80px">
      <el-form-item
        verify
        label="类型"
        prop="type">
        <el-select v-model="form.type">
          <el-option
            v-for="item in tradeType"
            :key="item.value"
            :value="item.value"
            :label="item.label">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item
        verify
        label="充值金额"
        prop="amount">
        <el-input-number
          :controls="false"
          v-model="form.amount"
          :precision="0"></el-input-number>
      </el-form-item>

      <el-form-item
        verify
        label="流水号"
        prop="trade_no">
        <el-input v-model="form.trade_no"></el-input>
      </el-form-item>
      <el-form-item
        label="备注"
        prop="remark">
        <el-input v-model="form.remark"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer">
      <el-button @click="visible = false">取 消</el-button>
      <el-button
        type="primary"
        @click="handleSubmit">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import { addTravelAgencyPrepayment } from '@/api/travelAgency'

const tradeType = [
  {
    label: '充值',
    value: 1,
    color: 'success'
  },
  {
    label: '扣款',
    value: 4,
    color: 'warning'
  }
]

export default {
  data () {
    return {
      id: 0,
      tradeType,
      visible: false,
      form: {
        amount: 0,
        trade_no: '',
        type: 1,
        remark: ''
      }
    }
  },
  methods: {
    show (id) {
      this.id = id
      this.visible = true
      this.$nextTick(() => {
        this.$refs.form.resetFields()
      })
    },
    handleClose () {
      this.visible = false
    },
    handleSubmit () {
      this.$refs.form.validate((valid) => {
        if (valid) {
          addTravelAgencyPrepayment({
            'type': this.form.type,
            'amount': this.form.amount,
            'trade_no': this.form.trade_no,
            'remark': this.form.remark,
            'travelAgencyId': this.id
          }).then(res => {
            this.$message.success('操作成功。')
            // 更新全局旅行社信息
            this.$store.dispatch('getAgencyList')
            this.handleClose()
            this.$emit('update')
          })
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>

</style>
