<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
        :rules="rulesThis"
        ref="ruleForm"
        label-width="110px"
        class="demo-ruleForm"
      >
        <el-form-item label="采购单编号" prop="cgdNo">
          <el-input v-model="ruleForm.cgdNo" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品分类" prop="goods_class">
          <el-input v-model="ruleForm.goods_class" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品名称" prop="good_name">
          <el-input v-model="ruleForm.good_name" disabled></el-input>
        </el-form-item>
        <el-form-item label="商品总克重" prop="weight">
          <el-input v-model="ruleForm.weight" disabled>
            <template slot="append">g</template></el-input
          >
        </el-form-item>
        <el-form-item label="工差总克重" prop="num">
          <el-input v-model="ruleForm.num" :disabled="isDetail"
            ><template slot="append">g</template></el-input
          >
        </el-form-item>
        <div style="text-align: right">
          <el-button v-if="!isDetail" type="primary" @click="submitForm"
            >保 存
          </el-button>
          <el-button @click="showModelThis = false">{{
            isDetail ? "关 闭" : "取 消"
          }}</el-button>
        </div>
      </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";
import addEdit from "./addEdit";
export default {
  components: { addEdit },
  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 {
        if (isnumber(value)) {
          callback();
        } else if (isnumber3(value)) {
          callback();
        } else {
          callback(new Error("总工差克重支持整数或三位小数!"));
        }
      }
    };
    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) {
      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 resetForm() {
      // 重置
      await this.$nextTick(() => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          const { loginName, goods_class, good_name, cgdNo, weight } =
            this.sitem;
          this.ruleForm = {
            cgdNo: cgdNo || "",
            loginName: loginName || "", // 账号
            goods_class: goods_class || "",
            good_name: good_name || "",
            weight: weight || "",
            num: "",
          };
        }
      });
    },

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