<template>
  <div>
    <el-select v-model="value" placeholder="请选择">
      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
    </el-select>
    <el-button @click="handleClickDownload" type="primary" :loading="loading" style="margin-left: 15px;margin-right: 10px;">导出Excel</el-button>
  </div>
</template>

<script>
import jsontoExcel from "@/utils/JOSNToExcel"
import { State, Family } from "@/data/mapTable"
import {  queryProperty } from "@/api/scan/request"
import { queryEquip } from "@/api/object/equip";
import { mapGetters, mapActions } from "vuex"

export default {
  data() {
    return {
      loading: false,
      value: '',
      dataList: [],
      options: [{
        value: 'header1',
        label: '已建立关联的设备资产'
      }, {
        value: 'header2',
        label: '未关联资产的设备'
      }, {
        value: 'header3',
        label: '未关联设备的资产'
      }],
      header1: [{
        fieldName: "Building.BuildLocalName",
        headerName: "建筑"
      }, {
        fieldName: "Floor.FloorLocalName",
        headerName: "楼层"
      }, {
        fieldName: "EquipCategory.EquipName",
        headerName: "设备类"
      }, {
        fieldName: "EquipLocalName",
        headerName: "设备本地名称"
      }, {
        fieldName: "EquipLocalID",
        headerName: "设备本地编码"
      }, {
        fieldName: "EquipID",
        headerName: "设备id"
      }, {
        fieldName: "BIMID",
        headerName: "设备BIMID"
      }, {
        fieldName: "Property.EquipFamilyList.0.FamilyName",
        headerName: "资产设备族"
      }, {
        fieldName: "Property.EquipLocalName",
        headerName: "资产本地名称"
      }, {
        fieldName: "Property.EquipLocalID",
        headerName: "资产本地编码"
      }, {
        fieldName: "Property.EquipID",
        headerName: "资产id"
      }, {
        fieldName: "Property.CreateTime",
        headerName: "资产创建时间"
      }, {
        fieldName: "Property.CodeType",
        headerName: "资产现场验证状态"
      }, {
        fieldName: "LedgerParam.Siteinstall.InstallLocation",
        headerName: "安装位置"
      }],
      header2: [{
        fieldName: "Building.BuildLocalName",
        headerName: "建筑"
      }, {
        fieldName: "Floor.FloorLocalName",
        headerName: "楼层"
      }, {
        fieldName: "EquipCategory.EquipName",
        headerName: "设备类"
      }, {
        fieldName: "EquipLocalName",
        headerName: "设备本地名称"
      }, {
        fieldName: "EquipLocalID",
        headerName: "设备本地编码"
      }, {
        fieldName: "EquipID",
        headerName: "设备id"
      }, {
        fieldName: "BIMID",
        headerName: "设备BIMID"
      }, {
        fieldName: "Property.EquipFamilyList.0.FamilyName",
        headerName: "资产设备族"
      }, {
        fieldName: "Property.EquipLocalName",
        headerName: "资产本地名称"
      }, {
        fieldName: "Property.EquipLocalID",
        headerName: "资产本地编码"
      }, {
        fieldName: "Property.EquipID",
        headerName: "资产id"
      }, {
        fieldName: "Property.CreateTime",
        headerName: "资产创建时间"
      }, {
        fieldName: "Property.CodeType",
        headerName: "资产现场验证状态"
      }, {
        fieldName: "LedgerParam.Siteinstall.InstallLocation",
        headerName: "安装位置"
      }],
      header3: [{
        fieldName: "Building.BuildLocalName",
        headerName: "建筑"
      }, {
        fieldName: "Floor.FloorLocalName",
        headerName: "楼层"
      }, {
        fieldName: "Equipment.EquipCategory.EquipName",
        headerName: "设备类"
      }, {
        fieldName: "Equipment.EquipLocalName",
        headerName: "设备本地名称"
      }, {
        fieldName: "Equipment.EquipLocalID",
        headerName: "设备本地编码"
      }, {
        fieldName: "Equipment.EquipID",
        headerName: "设备id"
      }, {
        fieldName: "Equipment.BIMID",
        headerName: "设备BIMID"
      }, {
        fieldName: "EquipFamilyList.0.FamilyName",
        headerName: "资产设备族"
      }, {
        fieldName: "EquipLocalName",
        headerName: "资产本地名称"
      }, {
        fieldName: "EquipLocalID",
        headerName: "资产本地编码"
      }, {
        fieldName: "EquipID",
        headerName: "资产id"
      }, {
        fieldName: "CreateTime",
        headerName: "资产创建时间"
      }, {
        fieldName: "CodeType",
        headerName: "资产现场验证状态"
      }, {
        fieldName: "LedgerParam.Siteinstall.InstallLocation",
        headerName: "安装位置"
      }]
    }
  },
  methods: {
    handleClickDownload() {
      this.loading = true
      this.dataList = []
      if (this.value == 'header1') {
        this.getheader1Data()
      } else if (this.value == 'header2') {
        this.getheader2Data()
      } else if (this.value == 'header3') {
        this.getheader3Data()
      }
    },
    getheader1Data(pageNum) {
      pageNum = pageNum ? pageNum : 1
      let params = {
        cascade: [
          {
            name: "equipCategory",
            projection: ["equipCode", "equipName"]
          },
          {
            name: "property",
            cascade: [
              {
                name: "equipFamilyList",
                projection: [ "family", "familyName" ]
              }
            ],
            projection: ["family", "equipLocalName", "equipLocalID", "equipID", "codeType", "createTime"]
          },
          {
            name: "building",
            projection: ["localName", "name", "id"]
          },
          {
            name: "floor",
            projection: ["localName", "name", "id"]
          }
        ],
        filters: " not propertyId isnull ",
        orders: "createTime desc, equipID asc",
        pageNumber: pageNum,
        pageSize: 1000,
        projection: ["createTime", "buildingId", "floorId", "classCode", "equipLocalName", "equipLocalID", "equipID", "bimId", "infos"]
      }
      queryEquip(params, res => {
        this.dataList = this.dataList.concat(res.content.map((item) => {
          item.property.codeType = State[item.property.codeType]
          // item.Property.Family = Family[item.Property.Family]
          return item
        }))
        if (res.total / (res.pageSize * res.pageNumber) > 1) {
          this.getheader1Data(res.pageNumber + 1)
        } else {
          let time = +new Date()
          this.loading = false
          jsontoExcel.downloadExl(this.header1, this.dataList, `已建立关联的资产设备-${time}`)
        }
      })
    },
    getheader2Data(pageNum) {
      pageNum = pageNum ? pageNum : 1
      let params = {
        cascade: [
          {
            name: "equipCategory",
            projection: ["equipCode", "equipName"]
          },
          {
            name: "building",
            projection: ["localName", "name", "id"]
          },
          {
            name: "floor",
            projection: ["localName", "name", "id"]
          }
        ],
        filters: " propertyId isnull ",
        orders: "createTime desc, equipID asc",
        pageNumber: pageNum,
        pageSize: 1000,
        projection: ["createTime", "buildingId", "floorId", "classCode", "equipLocalName", "equipLocalID", "equipID", "bimId", "infos"]
      }
      queryEquip(params, res => {
        this.dataList = this.dataList.concat(res.content)
        if (res.total / (res.pageSize * res.pageNumber) > 1) {
          this.getheader2Data(res.pageNumber + 1)
        } else {
          let time = +new Date()
          this.loading = false
          jsontoExcel.downloadExl(this.header2, this.dataList, `未关联资产的设备-${time}`)
        }
      })
    },
    getheader3Data(pageNum) {
      pageNum = pageNum ? pageNum : 1
      let params = {
        cascade: [
          {
            name: "equipFamilyList",
            projection: [ "family", "familyName" ]
          },
          {
            name: "building",
            projection: [ "localName", "name", "id" ]
          },
          {
            name: "floor",
            projection: [ "localName", "name", "id" ]
          }
        ],
        filters: " equipId isnull ",
        orders: "createTime desc, equipID asc",
        pageNumber: pageNum,
        pageSize: 1000,
        projection: [ "buildingId", "floorId", "family", "equipLocalName", "equipLocalId", "equipId", "createTime", "codeType", "infos"]
      }
      queryProperty(params, res => {
        this.dataList = this.dataList.concat(res.content.map((item) => {
          item.codeType = State[item.codeType]
          return item
        }))
        if (res.total / (res.pageSize * res.pageNumber) > 1) {
          this.getheader3Data(res.pageNumber + 1)
        } else {
          let time = +new Date()
          this.loading = false
          jsontoExcel.downloadExl(this.header3, this.dataList, `未关联设备的资产-${time}`)
        }
      })
    }
  }
}
</script>

<style scoped>
</style>
