<template>
  <el-dialog :visible="visible" width="400px" @close="handleClose" center :close-on-click-modal="false" title="设置服务费比例">
    <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="100px">
      <el-form-item label="服务费比例" prop="serviceRate">
        <digital-input
            :values="ruleForm.serviceRate"
            :placeholder="'服务费比例'"
            :min="0"
            :max="100"
            :position="'right'"
            size="default"
            :precision="2"
            :controls="false"
            :append="'%'"
            @reschange="rate_change($event)"
          />
      </el-form-item>

      <el-form-item class="flex-end">
        <el-button size="mini" type="primary" @click="onSubmit">提交</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
export default {
  props:['visible'],
  computed:{
    _visible:{
      get(){
        return this.visible
      },
      set(newVal){
        this.$emit("update:visible", newVal)
      }
    }
  },
  data(){
    return {
      ruleForm:{
        serviceRate:"",
      },
      rules:{
        serviceRate:[
          {
            required:true,
            trigger:'change',
            message:'请输入服务费比例'
          }
        ]
      }
    }
  },
  methods:{
    handleClose(){
      this._visible = false
      this.ruleForm = {
        serviceRate:""
      }
    },
    rate_change(e) {
      this.ruleForm.serviceRate = e + "";
      this.$refs.ruleForm.validateField("serviceRate");
    },
    async onSubmit(){
      try{
        await this.$refs.ruleForm.validate()

        this.handleClose()
        this.$emit('refresh')
      }catch(e){
        console.log(e)
      }
    }
  }
}
</script>
