<template>
  <el-dialog :title="title" :close-on-click-modal="false" :visible.sync="open" width="700px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="80px">
      <el-form-item label="兑换金额" prop="money">
        <el-input v-model="form.money" placeholder="请输入充值金额" />
      </el-form-item>
      <el-form-item label="所需货币" prop="coinNum">
        <el-input v-model="form.coinNum" placeholder="请输入云贝数量" />
      </el-form-item>
      <el-form-item label="说明" prop="remark">
        <el-input v-model="form.remark" placeholder="请输入说明" type="textarea" :rows="2"/>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="open = false">取 消</el-button>
    </div>


  </el-dialog>
</template>

<script>
import { addWithdrawExchange, getWithdrawExchange, updateWithdrawExchange } from '@/api/cai/withdrawExchange'

export default {
  components: {
  },
  data () {
    return {
      open: false,
      title: '',
      form:{
        id: undefined,
        money: undefined,
        coinNum: undefined,
        remark: undefined,
      },
      // 表单校验
      rules: {
        money: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        coinNum: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
      },
      buttonLoading: false,
    }
  },
  created() {
  },
  methods: {
    init (id) {
      this.form.id = id || undefined;
      this.title = (id ? "修改" : "新增") + "提现兑换配置";
      this.open = true;
      this.$nextTick(() => {
        this.$refs['form'].resetFields();
        if(this.form.id){
          getWithdrawExchange(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateWithdrawExchange(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addWithdrawExchange(this.form).then(data => {
              this.$modal.msgSuccess("新增成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
