<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="
              (status === '5' || status === '6') && powers.some((i) => i == '060')
            "
            :controls="false"
            :append="'%'"
            @reschange="rate_change"
          />
        </el-form-item>
      </el-col>
      <el-col
        :span="12"
        style="text-align: right"
        v-if="(status !== '5' || status !== '6') && powers.some((i) => i == '060')"
      >
        <el-button type="primary" :size="'mini'" @click="submitForm">保 存 </el-button>
      </el-col>
    </el-row>
  </el-form>
</template>
<script>
import asyncRequest from "@/apis/service/sellOut/project";
import resToken from "@/mixins/resToken";

export default {
  name: "Account",
  props: ["showModel", "sitem", "id", "type", "newTime"],
  mixins: [resToken],
  computed: {
    powers() {
      const tran =
        this.$store.getters.btnList.find((item) => item.menu_route == "projectDetail") ||
        {};
      const { action } = tran ?? {};
      return action ?? [];
    },
  },
  watch: {
    // showModel: function (val) {
    //   this.showModelThis = val;
    //   if (val) {
    //     this.initForm();
    //   }
    // },
    newTime: function (val) {
      if (val) {
        this.initForm();
      }
    },
  },
  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 { low_rate, projectNo, status } = this.sitem;
          this.status = status || "";
          this.ruleForm = {
            projectNo: projectNo || "",
            rate: low_rate || "0",
          };
        }
      });
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          if (this.loading) {
            return;
          }
          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>
