import { buildingQuery } from '@/api/object/build';
import { floorQuery } from '@/api/object/floor';

const buildFloor = {
  getData: function (buildFloorData) {
    let data, buildParams = {
        pageNumber: 1,
        pageSize: 1000,
        orders: "localName asc",
        projection: [
            "id",
            "localName"
        ]
    }, floorParams = {
        orders: "floorSequenceId desc",
        pageNumber: 1,
        pageSize: 1000,
        projection: [
            "buildingId",
            "id",
            "localName",
            "floorSequenceId"
        ]
    }

    let promise1 = new Promise((resolve, reject) => {
      buildingQuery(buildParams, res => {
        resolve(res)
      })
    })
    let promise2 = new Promise((resolve, reject) => {
      floorQuery(floorParams, res => {
        resolve(res)
      })
    })

    Promise.all([promise1, promise2]).then(function(values) {
      let builData = values[0].content, floorData = values[1].content
      data = builData.map(build => {
          return {
              value: build.id,
              label: build.localName
          }
      })
      data.unshift({
        value: "",
        label: "未明确建筑"
      })
      data.forEach(build => {
        floorData.forEach(floor => {
          if (build.value == floor.buildingId && floor.id && floor.localName) {
            if (build.children) {
              build.children.push({
                value: floor.id,
                label: floor.localName
              })
            } else {
              build.children = []
              build.children.push({
                value: "",
                label: "未明确楼层"
              },{
                value: floor.id,
                label: floor.localName 
              })
            }
          }
        })
      })
      data.forEach(item => {
        if (item.children && item.children.length) {
          item.children.forEach(child => {
            if (child.value != "") {
              buildFloorData.push({
                Code: item.value + "-" + child.value,
                Name: item.label + "-" + child.label
              })
            } else {
              buildFloorData.push({
                Code: item.value,
                Name: item.label + "-" + child.label
              })
            }
          })
        } else {
          buildFloorData.push({
            Code: item.value,
            Name: item.label
          })
        }
      })
    })
    // buildingQuery(buildParams, res => {
    //   let builData = res.Content
    //   data = builData.map(build => {
    //       return {
    //           value: build.BuildID,
    //           label: build.BuildLocalName
    //       }
    //   })
    //   data.unshift({
    //     value: "",
    //     label: "未明确建筑的设备"
    //   })
    //   floorQuery(floorParams, res => {
    //       let floorData = res.Content
    //       data.forEach(build => {
    //           floorData.forEach(floor => {
    //               if (build.value == floor.BuildID && floor.FloorID && floor.FloorLocalName) {
    //                   if (build.children) {
    //                       build.children.push({
    //                           value: floor.FloorID,
    //                           label: floor.FloorLocalName
    //                       })
    //                   } else {
    //                       build.children = []
    //                       build.children.push({
    //                           value: "",
    //                           label: "未明确楼层的设备"
    //                       })
    //                   }
    //               }
    //           })
    //       })
    //       data.forEach(item => {
    //         if (item.children && item.children.length) {
    //           item.children.forEach(child => {
    //             buildFloorData.push({
    //               Code: item.value + "-" + child.value,
    //               Name: item.label + "-" + child.label
    //             })
    //           })
    //         } else {
    //           buildFloorData.push({
    //             Code: item.value,
    //             Name: item.label
    //           })
    //         }
    //       })
    //   })
    // })
  }
}

export default buildFloor