export default {
  methods: {

    // 提交表单
    submitForm(ref) {
      if (this.submitButtonDisabled) {
        return;
      }
      const el = this.$refs.bomDirForm;
      el.validate((valid) => {
        if (!valid) {
          return;
        }
        this.submitButtonDisabled = true;
        this.$post("/materia/bom/tree/save", {
          entity: this.bomDirForm,
        })
          .then((res) => {
            //添加成功 刷新树节点
            let parentId=this.bomDirForm.parentId;
            this.initForm();
            this.bomDirDialog.visible = false;
            if (!parentId) {
              //刷新顶层节点数据
              this.$post("/materia/bom/tree/getListByParentId", {
                parentId: -1,
                type: 0,
              }).then(({ data }) => {
                this.data = data.result;
              });
              return;
            }

            if (parentId&&parentId != -1) {

              console.log("parentId:"+parentId);
              this.refreshNodeBy(parentId);
            } else {
              //刷新顶层节点数据
              this.$post("/materia/bom/tree/getListByParentId", {
                parentId: -1,
                type: 0,
              }).then(({ data }) => {
                this.data = data.result;
              });
            }

            if (this.isChildDir) {

              //this.refreshNodeBy(this.thisformdata.id);
            } else {
              
            }
          })
          .catch((error) => {
            this.submitButtonDisabled = false;
            this.$message.error(error.message);
          });
      });
    },

    // 提交bom表单
    submitBomForm(ref) {
      if (this.submitButtonDisabled) {
        return;
      }
      const el = this.$refs.bomForm;
      el.validate((valid) => {
        if (!valid) return;
        this.submitButtonDisabled = true;
        this.$post("/materia/bom/save", {
          entity: this.bomForm,
        })
          .then((res) => {
            //添加成功 刷新树节点和table
            this.$post("/materia/bom/tree/queryBoms", {
              id: this.bomForm.bomTreeId,
            }).then(({ data }) => {
              // this.refreshNodeBy(this.bomForm.bomTreeId);
              this.initForm();
              this.bomDialog.visible = false;

              this.bomTableData = data.bomList;
            });

          })
          .catch((error) => {
            this.submitButtonDisabled = false;
            this.$message.error(error.message);
          });
      });
    },
    // 提交bom-item表单
    
    submitBomItemForm(ref) {
      if (this.submitButtonDisabled) {
        return;
      }
      const el = this.$refs.bomItemForm;
      let bomId = this.bomItemForm.bomId;
      el.validate((valid) => {
        if (!valid) return;
        this.submitButtonDisabled = true;
        this.$post("/materia/bom/item/save", {
          entity: this.bomItemForm,
        })
          .then((res) => {
            //添加成功 刷新bom-item table

            this.$post("/materia/bom/item/list", {
              "query.bomId": this.bomItemForm.bomId,
            }).then(({ data }) => {
              this.bomItemTableData = data.result;
              // this.bomTableData = data.bomList;
            });
            this.initForm();
            this.bomItemForm.bomId = bomId;
            this.bomItemDialog.visible = false;

          })
          .catch((error) => {
            this.submitButtonDisabled = false;
            this.$message.error(error.message);
          });
      });
    },
    // 提交表单
    submitAttsForm(ref) {
      if (this.submitButtonDisabled) {
        return;
      }
      const el = this.$refs.attform;
      el.validate((valid) => {
        if (!valid) return;
        this.submitButtonDisabled = true;
        this.$post("/attach/save", {
          entity: this.attform,
        })
          .then((res) => {
           
            if (res && res.code && res.code == 1) {
              this.$message.success("添加附件成功！");

              this.$post("/materia/bom/tree/queryBoms", {
                id: this.bomForm.bomTreeId,
              }).then(({ data }) => {
                this.initForm();
                this.materiaAttsDialog.visible = false;
                this.bomTableData = data.bomList;
              });

            }
          })
          .catch((error) => {
            this.materiaAttsDialog.visible = false;
            this.submitButtonDisabled = false;
            this.$message.error(error.message);
          });
      });
    },
    initForm() {
      this.bomDirForm = {
        id: null,
        name: "",
        code: "",
        parentId: "",
        bomId: "",
        remark: "",
        isLeaf: "1",
        materiaName:null,
        materiaId:null
      };
      this.bomForm = {
        id: null,
        materiaId: "",
        materiaCode: "",
        materiaName: "",
        parentId: "",
        version: "",
        status: "401",
        useType: "",
        remark: "",
        bomTreeId: "",
      };
      this.bomItemForm = {
        id: null,
        bomId: "",
        materiaId: "",
        materiaCode: "",
        materiaName: "",
        orderNo: "1",
        groupName: "1",
        useNum: "",
        lossRate: "",
        required: "",
        supplyType: "",
        supplierId: "",
      };
      this.attform = {
        id: null,
        tableCode: "mortals_iot_materia_bom",
        tableDataId: "",
        attachType: "",
        attachName: "",
        attachUrl: "",
        remark: "",
      }

      this.$post("/materia/bom/tree/add", {});
    },

  },

  data() {
    return {
      bomDirForm: {},
      bomForm: {},
      bomItemForm: {},
      attform: {},
      attRules: {
        attachType: [
          {
            required: true,
            message: "请选择附件类型",
            trigger: "blur",
          },
        ],
        attachUrl: [
          {
            required: true,
            message: "请上传附件",
            trigger: "blur",
          }
        ],
      },
      bomDirRules: {
        code: [
          {
            required: true,
            message: "请输入分类编码",
            trigger: "blur",
          },
        ],
        name: [
          {
            required: true,
            message: "请输入分类名称",
            trigger: "blur",
          },
          { max: 50, message: "分类名称最大长度50个字符", trigger: "blur" },
        ],
      },

      bomRules: {
        selfCode: [
          {
            required: true,
            message: "请输入BOM短代码",
            trigger: "blur",
          },
          { max: 50, message: "短代码最大长度50个字符", trigger: "blur" },
        ],
        materiaName: [
          {
            required: true,
            message: "请选择物料",
            trigger: "blur",
          },
        ],
        useType: [
          {
            required: true,
            message: "请选择BOM类型",
            trigger: "blur",
          },
        ],
      },

      bomItemRules: {
        materiaCode: [
          {
            required: true,
            message: "请输入物料编码",
            trigger: "blur",
          },
        ],
        required: [
          {
            required: true,
            message: "请选择是否必选料",
            trigger: "blur",
          },
        ],

        materiaBomId: [
          {
            required: true,
            message: "请选择物料BOM版本",
            trigger: "blur",
          },
        ],
        supplyType: [
          {
            required: true,
            message: "请选择补给方式",
            trigger: "blur",
          },
        ],
        useNum: [
          {
            required: true,
            message: "请输入用量",
            trigger: "blur",
          },
        ],
        lossRate: [
          {
            required: true,
            message: "请输入损耗率",
            trigger: "blur",
          },
        ],
        orderNo: [
          {
            required: true,
            message: "请输入排序号",
            trigger: "blur",
          },
        ],
        groupName: [
          {
            required: true,
            message: "请输入分组名称",
            trigger: "blur",
          },
        ],

        supplierId: [
          {
            required: false,
            message: "请选择供应商",
            trigger: "blur",
          },
        ],
      },
    }
  }
}



