/**
 * toLevelArr
 * 组合出[{
 * ...,
 * pid,
 * id
 * }]
 * 数据结构
 * */
const toLevelArr = res => {
  let periodList = [];
  if (
    res &&
    res.name !== "TypeError" &&
    res.code === 0 &&
    res.data &&
    res.data.arrays &&
    res.data.arrays.length > 0
  ) {
    let array = res.data.arrays;
    if (res.data.showNames.length < 5) {
      res.data.showNames.unshift("期");
    }
    array.forEach((item, index1) => {
      let obj = JSON.parse(JSON.stringify(item));
      delete obj.nexts;
      obj.label = obj.numberShow + res.data.showNames[0]; // 期号
      obj.idThis = index1 + "";
      obj.pid = 0;
      obj.isLeaf = false;
      periodList.push(obj);
      item.nexts.forEach((vv, index2) => {
        let obj = JSON.parse(JSON.stringify(vv));
        delete obj.nexts;
        obj.label = obj.numberShow + res.data.showNames[1]; // 楼号
        obj.idThis = index1 + "-" + index2;
        obj.pid = index1 + "";
        obj.isLeaf = false;
        periodList.push(obj);
        vv.nexts.forEach((vv3, index3) => {
          let obj = JSON.parse(JSON.stringify(vv3));
          delete obj.nexts;
          obj.label = obj.numberShow + res.data.showNames[2]; // 单元号
          obj.idThis = index1 + "-" + index2 + "-" + index3;
          obj.pid = index1 + "-" + index2;
          obj.isLeaf = false;
          periodList.push(obj);
          vv3.nexts.forEach((vv4, index4) => {
            let obj = JSON.parse(JSON.stringify(vv4));
            delete obj.nexts;
            obj.label = obj.numberShow + res.data.showNames[3]; // 层号
            obj.idThis = index1 + "-" + index2 + "-" + index3 + "-" + index4;
            obj.pid = index1 + "-" + index2 + "-" + index3;
            periodList.push(obj);
            if (vv4.nexts && vv4.nexts.length > 0) {
              obj.isLeaf = false;
              vv4.nexts.forEach((vv5, index5) => {
                let obj = JSON.parse(JSON.stringify(vv5));
                delete obj.nexts;
                obj.label = obj.numberShow + res.data.showNames[4]; // 门牌号
                // obj.label = obj.numberShow; // 门牌号：01
                obj.idThis =
                  index1 +
                  "-" +
                  index2 +
                  "-" +
                  index3 +
                  "-" +
                  index4 +
                  "-" +
                  index5;
                obj.pid = index1 + "-" + index2 + "-" + index3 + "-" + index4;
                obj.isLeaf = true;
                periodList.push(obj);
              });
            } else {
              obj.isLeaf = true;
            }
          });
        });
      });
    });
  }
  return periodList;
};

export default {
  toLevelArr
};
