<template>
  <el-form
    ref="ruleForm"
    :model="ruleForm"
    status-icon
    :size="'mini'"
    :rules="rulesThis"
    label-width="112px"
  >
    <el-row>
      <el-col :span="12">
        <el-form-item label="项目毛利率" prop="rate">
          <digital-input
            :values="ruleForm.rate"
            :placeholder="'项目毛利率'"
            :min="0"
            :max="100000000000"
            :position="'right'"
            :precision="2"
            :size="'mini'"
            :disabled="false"
            :controls="false"
            :append="'%'"
            @reschange="rate_change"
          />
        </el-form-item>
      </el-col>
      <el-col :span="12" style="text-align: right">
        <el-button type="primary" :size="'mini'" @click="submitForm"
          >保 存
        </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/sheetOrder/project";
import resToken from "@/mixins/resToken";

export default {
  name: "Account",
  props: ["showModel", "sitem", "id", "type"],
  mixins: [resToken],
  watch: {
    // showModel: function (val) {
    //   this.showModelThis = val;
    //   if (val) {
    //     this.initForm();
    //   }
    // },
    // showModelThis(val) {
    //   if (!val) {
    //     this.$emit("cancel");
    //   }
    // },
  },
  data() {
    const validate_sale_price = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("项目毛利率不能为空！"));
      } else if (
        value === "0" ||
        value === "0." ||
        value === "0.0" ||
        value === "0.00"
      ) {
        callback(new Error("项目毛利率不能为零！"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      ruleForm: {
        projectNo: "",
        rate: "0",
      },
      rulesThis: this.rules,
      rules: {
        rate: [
          {
            required: true,
            validator: validate_sale_price,
            trigger: "blur",
          },
        ],
      },
    };
  },
  mounted() {
    this.initForm();
  },
  methods: {
    //项目毛利率编辑
    rate_change(e) {
      this.ruleForm.rate = e + "";
      this.$refs.ruleForm.validateField("rate");
    },

    //初始化整个组件
    async initForm() {
      this.loading = true;
      this.company = "";
      this.ruleForm = {
        projectNo: "",
        rate: "0",
      };
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },
    //初始化整个表单
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { rate, projectNo } = this.sitem;
          this.ruleForm = {
            projectNo: projectNo || "",
            rate: rate || "0",
          };
        }
      });
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          this.loading = true;
          let model = JSON.parse(JSON.stringify(this.ruleForm));
          let res = await asyncRequest.rateEdit(model);
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "设置成功！",
              message: "",
            });
            this.showModelThis = false;
            // 刷新
            this.$emit("refresh", res.data);
          } else if (res && res.code >= 100 && res.code <= 104) {
            await this.logout();
          } else {
            this.$message.warning(res.message);
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
  },
};
</script>

   <style lang="scss" scoped>
.account {
}
</style>
