<template>
 <div class="task-item">
<ul>
  <li class="cat"><span>分类一/分类二/分类三</span></li>
  <li></li>
  <li></li>
  <li></li>
</ul>
 </div>
</template>
   <script>
export default {
  name: "taskItem",
  data() {
    return {
      
    };
  },
  mounted() {
    this.initForm();
  },
  methods: {
    async initForm() {
      this.loading = true;
      this.resetFormData();
      this.rulesThis = this.rules;
      await this.resetForm();
      this.loading = false;
    },
    handleClick(row) {
      console.log(row);
    },
    openEdit(type, sitem) {
      this.sitem = sitem;
      this.modelId = type;
      this.showModel = true;
    },
    async initData() {
      this.loading = true;
      const { code, data, message } = await asyncRequest.detail({
        id: this.id,
      });
      this.loading = false;
      if (code === 0) {
        await this.resetForm(data);
      } else if (code >= 100 && code <= 104) {
        await this.logout();
      } else {
        this.$message.warning(message);
      }
    },
    async resetForm() {
      // 重置
      await this.$nextTick(async () => {
        if (this.$refs.ruleForm) {
          this.$refs.ruleForm.resetFields();
          this.$refs.ruleForm.clearValidate();
          this.resetFormData();
        }
      });
    },
    resetFormData() {
      this.ruleForm = {
        a1: "",
        a2: [],
        a3: "",
        a4: "",
        a5: "",
        a6: [],
        a7: "",
        a8: "",
        b1: "2018春秋新款V领修身丝绒西装女 双排扣复古极简中长款工装外套",
        b2: "",
        b3: "",
        b4: "",
        b5: "",
        b6: "",
        b7: "",
        b8: "",
        b9: "",
        bb1: "",
      };
    },
    refreshEdit(e) {
      console.log(e);
    },
    //商品分类选择
    goods_class_change(e) {
      this.ruleForm.a1 = e;
      this.$refs.ruleForm.validateField("a1");
    },
    //商品品牌选择
    a2searchChange(e) {
      const { id, code, label } = e;
      if (id) {
        this.ruleForm.a2 = [id];
      } else {
        this.ruleForm.a2 = [];
      }
      this.$refs.ruleForm.validateField("a2");
    },
    //供应商选择
    a6searchChange(e) {
      const { id, code, label } = e;
      if (id) {
        this.ruleForm.a6 = [code];
      } else {
        this.ruleForm.a6 = [];
      }
      this.$refs.ruleForm.validateField("a6");
    },
    //税点选择
    a8searchChange(e) {
      // console.log(e);
      const { id, code, label } = e;
      if (id) {
        this.ruleForm.a8 = label;
      } else {
        this.ruleForm.a8 = "";
      }
      this.$refs.ruleForm.validateField("a8");
    },
    //单位选择
    b2searchChange(e) {
      const { id, code, label } = e;
      if (id) {
        this.ruleForm.b2 = [code];
      } else {
        this.ruleForm.b2 = [];
      }
      this.$refs.ruleForm.validateField("b2");
    },
    //贵金属种类选择
    b4searchChange(e) {
      const { id, code, label } = e;
      if (id) {
        this.ruleForm.b4 = code;
      } else {
        this.ruleForm.b4 = "";
      }
      this.$refs.ruleForm.validateField("b4");
    },
    //税点选择
    b4searchChange(e) {
      const { id, code, label } = e;
      if (id) {
        this.ruleForm.b4 = code;
      } else {
        this.ruleForm.b4 = "";
      }
      this.$refs.ruleForm.validateField("b4");
    },
    async submitForm() {
      await this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let change = false;
          this.mock.forEach((v1) => {
            if (v1.change && v1.change.length > 0) {
              change = true;
            }
          });
          if (!change) {
            this.ruleForm.spec = "规格！";
            return;
          } else {
            this.ruleForm.spec = "";
            let table = false;
            let hasnot = false;
            let stock = [];
            this.right.specList.forEach((v1) => {
              if (v1) {
                v1.forEach((v2) => {
                  if (v2 && v2.limit && v2.limit.length > 0) {
                    let list = [];
                    v2.limit.forEach((v3) => {
                      if (v3.num != 0 && v3.price != 0) {
                        table = true;
                        list.push(v3);
                      }
                      if (
                        (v3.num != 0 && v3.price == 0) ||
                        (v3.num == 0 && v3.price !== 0)
                      ) {
                        hasnot = true;
                      }
                    });
                    if (list.length > 0) {
                      let model = Object.assign({}, v2);
                      model.limit = list;
                      stock.push(model);
                    }
                  }
                });
              }
            });

            if (hasnot) {
              this.ruleForm.table =
                "起订量与成本价需成对填写，未成对请设为零！";
            } else {
              if (!table) {
                this.ruleForm.table = "至少填写一堆起订量与成本价！";
              } else {
                this.ruleForm.table = "";
                this.loading = true;
                let obj = JSON.parse(JSON.stringify(this.ruleForm));
                obj.stock = stock;
                obj.a1 = obj.a1[obj.a1.length - 1];
                obj.image = obj.image.join();
                //split(",");
                console.log(obj);
                let res = {};
                if (this.id === "add") {
                  delete obj["id"];
                  res = await asyncRequest.add(obj);
                } else {
                  res = await asyncRequest.update(obj);
                }
                this.loading = false;
                if (res.code === 0) {
                  let title = this.id === "add" ? "添加成功" : "修改成功";
                  this.$notify.success({
                    title,
                    message: "",
                  });
                  // 刷新
                  this.$emit("refresh");
                }
              }
            }
          }
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },

    doExchange(arr) {
      let len = arr.length;
      // 当数组大于等于2个的时候
      if (len >= 2) {
        // 第一个数组的长度
        let len1 = arr[0].length;
        // 第二个数组的长度
        let len2 = arr[1].length;
        // 2个数组产生的组合数
        let lenBoth = len1 * len2;
        //  申明一个新数组
        let items = new Array(lenBoth);
        // 申明新数组的索引
        let index = 0;
        for (let i = 0; i < len1; i++) {
          for (let j = 0; j < len2; j++) {
            if (arr[0][i] instanceof Array) {
              items[index] = arr[0][i].concat(arr[1][j]);
            } else {
              items[index] = [arr[0][i]].concat(arr[1][j]);
            }
            index++;
          }
        }
        let newArr = new Array(len - 1);
        for (let i = 2; i < arr.length; i++) {
          newArr[i - 1] = arr[i];
        }
        newArr[0] = items;
        return this.doExchange(newArr);
      } else {
        return arr[0];
      }
    },
  },
  watch: {},
};
</script>
   <style lang="scss" scoped>
.activeAdd {
  .activeAdd-title {
    border-top: 1px solid #d8dce5;
    span {
      height: 50px;
      line-height: 50px;
      font-family: "微软雅黑", sans-serif;
      font-weight: 400;
      font-style: normal;
      font-size: 16fpx;
      text-align: left;
    }
  }
}
</style>
   