<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="enableStatus">
        <el-select v-model="form.enableStatus" placeholder="状态" size="small">
          <el-option
              v-for="dict in enableStatusList"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
          />
        </el-select>
      </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 { enableStatusList } from '@/constant/statusMap'
import { addWithdrawSetting, getWithdrawSetting, updateWithdrawSetting } from '@/api/xq/withdrawSetting'

export default {
  components: {
  },
  data () {
    return {
      enableStatusList,
      open: false,
      title: '',
      form:{
        id: undefined,
        money: undefined,
        remark: undefined,
        enableStatus: undefined,
      },
      // 表单校验
      rules: {
        money: [
          { required: true, message: "数据不能为空", trigger: "blur" }
        ],
        enableStatus: [
          { required: true, message: "数据不能为空", trigger: "change" }
        ],
      },
      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){
          getWithdrawSetting(id).then(response => {
            this.form = response.data;
          });
        }
      })
    },
    // 表单提交
    submitForm () {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateWithdrawSetting(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.$modal.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }else{
            addWithdrawSetting(this.form).then(data => {
              this.$modal.msgSuccess("修改成功");
              this.buttonLoading = false;
              this.open = false
              this.$emit('refreshDataList')
            }).finally(() => {
              this.buttonLoading = false;
            });
          }

        }
      })
    },
  }
}
</script>
