<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="service_tax">
        <digital-input
          :values="ruleForm.service_tax"
          :placeholder="'服务费比例'"
          :min="0"
          :max="100"
          :position="'right'"
          size="default"
          :precision="0"
          :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>
import asyncRequest from "@/apis/service/operate/supplier";
export default {
  props: ["visible", "row"],
  computed: {
    _visible: {
      get() {
        return this.visible;
      },
      set(newVal) {
        this.$emit("update:visible", newVal);
      },
    },
  },
  data() {
    return {
      ruleForm: {
        service_tax: "",
      },
      rules: {
        service_tax: [
          {
            required: true,
            trigger: "change",
            message: "请输入服务费比例",
          },
        ],
      },
    };
  },
  watch: {
    _visible: function (val) {
      if (val) {
        this.resetForm();
      }
    },
  
  },
  methods: {
    handleClose() {
      this._visible = false;
      this.ruleForm = {
        service_tax: "",
      };
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { service_tax } = this.row;
          this.ruleForm = {
            service_tax: service_tax || "0",
          
          };
        }
      });
    },
    rate_change(e) {
      this.ruleForm.service_tax = e + "";
      this.$refs.ruleForm.validateField("service_tax");
    },
    async onSubmit() {
      try {
        await this.$refs.ruleForm.validate();
        const model = {
          ...this.ruleForm,
          id: this.row.id,
          public_bank: this.row.public_bank,
          public_bankNo: this.row.public_bankNo,
        };
        const { code } = await asyncRequest.save(model);
        this.loading = false;
        if (code === 1) {
          this.$notify.success({
            title: "设置成功！",
            message: "",
          });
          this.handleClose();
          this.$emit("refresh");
        }
      } catch (e) {
        console.log(e);
      }
    },
  },
};
</script>
