<template>
  <el-dialog
    :title="'填写采购单总工差'"
    :center="true"
    align="left"
    top="17vh"
    width="640px"
    @close="showModelThis = false"
    :close-on-click-modal="false"
    :visible.sync="showModelThis"
    v-loading="loading"
    element-loading-text="拼命加载中"
    element-loading-spinner="el-icon-loading"
    element-loading-background="rgba(0, 0, 0, 0.8)"
  >
    <el-card style="margin: -20px 0 0 0">
      <el-form
        :model="ruleForm"
        status-icon
        :size="'mini'"
        :rules="rulesThis"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-row>
          <el-col :span="12">
            <el-form-item label="商品总克重" prop="weight">
              <digital-input
                :values="ruleForm.weight"
                :placeholder="'商品总克重'"
                :min="0"
                :disabled="true"
                :max="100000000000"
                :position="'right'"
                :precision="3"
                :size="'mini'"
                :controls="false"
                :append="'g'"
                @reschange="number_change($event, 'weight')"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="工差总克重" prop="num">
              <digital-input
                :values="ruleForm.num"
                :placeholder="'工差总克重'"
                :min="0"
                :disabled="false"
                :max="ruleForm.weight"
                :position="'right'"
                :precision="3"
                :size="'mini'"
                :controls="false"
                :append="'g'"
                @reschange="number_change($event, 'num')"
              />
            </el-form-item>
          </el-col>

          <el-col :span="24" style="text-align: right">
            <el-button
              v-if="!isDetail"
              :size="'mini'"
              type="primary"
              @click="submitForm"
              >保 存
            </el-button>
            <el-button :size="'mini'" @click="showModelThis = false">{{
              isDetail ? "关 闭" : "取 消"
            }}</el-button>
          </el-col>
        </el-row>
      </el-form>
    </el-card>
  </el-dialog>
</template>
   <script>
import asyncRequest from "@/apis/service/purchaseIn/purchaseDiffOrder";
import resToken from "@/mixins/resToken";
import { isnumber, isnumber3 } from "@/utils/validate";
export default {
  name: "purchaseDiffOrder",
  props: ["showModel", "id", "isDetail", "sitem"],
  mixins: [resToken],
  data() {
    const validateWeight = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("总工差克重不能为空!"));
      } else if (
        value === "0" ||
        value === "0.0" ||
        value === "0.00" ||
        value === "0.000"
      ) {
        callback(new Error("总工差克重不能为0!"));
      } else {
        callback();
      }
    };
    return {
      loading: false,
      showModelThis: this.showModel,
      ruleForm: {
        num: "",
        loginName: "",
        goods_class: "",
      },
      rulesThis: this.rules,
      rules: {
        num: [
          {
            required: true,
            validator: validateWeight,
            trigger: "blur",
          },
        ],
      },
    };
  },
  watch: {
    showModel: function (val) {
      console.log(val);
      this.showModelThis = val;
      if (val) {
        this.initForm();
      }
    },
    showModelThis(val) {
      if (!val) {
        this.$emit("cancel");
      }
    },
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },
    async initData() {
      const res = await asyncRequest.detail({ id: this.id });
      if (res && res.code === 0 && res.data) {
        this.ruleForm = res.data;
        this.ruleForm.role_id = this.ruleForm.role;
      } else if (res && res.code >= 100 && res.code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(res.message);
      }
    },
    async number_change(e, key) {
      this.ruleForm[key] = e + "" || "0";
      this.$refs.ruleForm.validateField(key);
    },
    async resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { cgdNo, weight, good_num } = this.sitem;
          // console.log(good_num)
          const t_weight = weight * 1;
          const t_good_num = good_num * 1;
          // console.log(t_good_num, t_weight);
          this.ruleForm = {
            cgdNo: cgdNo || "",
            weight: t_good_num * t_weight || "0",
            num: "0",
          };
        }
      });
    },

    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
            if (this.loading) {
            return;
          }
          this.loading = true;
          const { cgdNo, num } = this.ruleForm;
          const model = {
            cgdNo: cgdNo,
            diff_weight: num,
          };
          let res = await asyncRequest.add(model);
          this.loading = false;
          if (res && res.code === 0) {
            this.$notify.success({
              title: "添加成功！",
              message: "",
            });
            this.showModelThis = false;
            // 刷新

            this.$emit("refresh");
          } 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>
.purchaseDiffOrder {
}
</style>
   