export default {

  methods: {
    addDir() {
      console.log("addDir 同级目录");
      // this.append(this.thisformdata);
      this.initForm();
      
      this.bomDirDialog.visible = true;
      this.bomDirForm.parentId = this.thisformdata.parentId;
      this.isChildDir = false;
    },

    addDirChild() {
      console.log("addDirChild",this.thisformdata);
      this.initForm();
      //新增BOM-Tree子树时，子树级别校验
      let parentId = this.thisformdata.id ? this.thisformdata.id : -1;
      this.$post("/materia/bom/tree/getLevel", {
        parentId: parentId,
      }).then((res) => {
        if(res.isNotEndLevel){
            this.bomDirForm.parentId = this.thisformdata.id;
  
            this.bomDirDialog.visible = true;
            this.isChildDir = true;
        }else{
            this.$message.error("添加的子目录超过最大目录数！");
        }
      })
        .catch((error) => {
          this.$message.error(error.message);
      });
    },
    editDir() {
      console.log("editDir");
      this.initForm();
       //检查是否选中的是否是目录
       if(this.thisformdata.type==1){
        this.$message.error("请选择节点目录编辑！");
        return;
       }

      //初始化获取根节点
      this.$post("/materia/bom/tree/edit", {
        id: this.thisformdata.id,
      }).then(({ data }) => {
        data.entity = this.util_toString(data.entity, this.toString);
        this.bomDirForm = Object.assign({}, this.bomDirForm, data.entity);

       if(data.entity.materiaId){

        this.$post("/materia/view", {
          id: data.entity.materiaId,
        }).then(({ data }) => {
          this.bomDirForm.materiaName=data.entity.name;
        });

       }

      });

      this.bomDirDialog.visible = true;
      this.isChildDir = false;
    },

    addMateriaBom() {
      console.log("addMateriaBom",this.thisformdata);
      //初始化bom
      this.initForm();
      if(this.bomTreeId==null||this.bomTreeId==''){
        this.$message.error("请选择产品树！");
        return;
      }

      if(this.thisformdata.materiaId==null||this.thisformdata.materiaId==''){
        this.$message.error("当前选择产品树未关联物料，请添加后再新建BOM！");
        return;
      }
      this.bomForm.bomTreeId =this.bomTreeId;
      this.bomForm.materiaId =this.thisformdata.materiaId;
      //初始化BOM 类型
      this.bomForm.useType = "300";
     
      this.bomDialog.visible = true;
      // if (this.thisformdata.type === 0) {
      //   //从目录添加
      // //  this.bomForm.bomTreeId = this.thisformdata.id;
      //  // this.bomDialog.visible = true;
      // } else if (this.thisformdata.type === 1) {
      //   //从bom添加bom
      //   this.bomForm.bomTreeId = this.thisformdata.parentId;
      //   this.bomDialog.visible = true;
        
      // }else{
      //   console.log("未选择目录");

      //    this.$message.error("请选择目录");

      // //  this.bomDialog.visible = true;
      // }
    },

    editMateriaBom() {
      console.log("editMateriaBom",this.thisformdatan,this.bomForm);
      if(!this.bomForm.id){
       this.$message.error("请选择bom表单！");
       return;

      }
      this.bomDialog.visible = true;
    },

    addMateriaBomItem() {
      console.log("addMateriaBomItem");

      if (
        this.bomItemForm.bomId === "undefined" ||
        this.bomItemForm.bomId === ""
      ) {
        this.$message.error("请选择对应的bom！");
        return;
      }

      let bomId=this.bomItemForm.bomId;

      this.bomItemForm = {
        id: null,
        bomId: bomId,
        materiaId: "",
        materiaCode: "",
        materiaName: "",
        orderNo: "1",
        groupName: "1",
        useNum: "",
        lossRate: "",
        required: "",
        supplyType: "",
        supplierId: "",
      };

      this.bomItemDialog.visible = true;

    },

    editMateriaBomItem() {
      console.log("editMateriaBomItem");
      if(this.bomItemForm.id==""&&this.bomItemForm.id==null){
        this.$message.error("请选择bomitem！");
        return;
 
       }
      this.$post("/materia/bom/item/edit", {
        id:  this.bomItemForm.id,
      }).then(({ data }) => {
        data.entity = this.util_toString(data.entity, this.toString);
        this.bomItemForm = Object.assign({}, this.bomItemForm, data.entity);
        this.materiaSupply = {};

        //根据物料id获取供应商
        this.$post("/materia/bom/item/materiaSupply", {
          id: this.bomItemForm.materiaId,
        })
          .then((res) => {
            if (res && res.code && res.code == 1) {
            
              // if (Object.keys(res.materiaSupply).length == 0) {
              //   this.$message.error(
              //     "当前物料未配置供应商，请配置物料供应后再选择！"
              //   );
              //   return;
              // }
              this.materiaSupply = res.materiaSupply;
            }
          })
          .catch((error) => {
            this.$message.error(error.message);
          });
      
      });

      this.bomItemDialog.visible = true;
    },


    addMateriaAtts() {
      console.log("addBomAtts");
      if (JSON.stringify(this.tableRowData) == "{}") {
        this.$message.error("请选择产品BOM！");
        return;
      }

    if(this.tableRowData.status!=401){
      this.$message.error("当前BOM为非修订状态，不能编辑！");
      return;
    }

    this.fileList=[];

      this.initForm();
      this.attform.tableDataId = this.tableRowData.id;
      this.materiaAttsDialog.visible = true;

      console.log(this.materiaAttsDialog.visible);
    },


  },
 
  data() {
    return {
      bomParams: {
        "BOM短代码": "selfCode",
        "物料代码": "materiaCode",
        "BOM类型": "useTypeName",
        "备注": "remark",
      },
      bomItemParams: {
        "物料代码": "materiaCode",
        "默认用量": "useNum",
        "损耗率": "lossRate",
        "必选料": "requiredName",
        "补给方式": "supplyTypeName",
        "供应商": "supplyName",
      },
    }
  }
}



