<template>
  <div>
    <el-row style="margin-top: 30px;">
      <span>请选择要导出的数据：</span>
      <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;">导出Excel</el-button>
    </el-row>
    <div style="text-align:center;font-size:25px;margin-top:20%;">当前页面中的为临时功能，完整版正在设计中……</div>
  </div>
</template>

<script>
import jsontoExcel from "@/utils/JOSNToExcel"
import { State, Family } from "@/data/mapTable"
import { queryEquip, queryProperty } from "@/api/scan/request"
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: "资产现场验证状态"
      }],
      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: "资产现场验证状态"
      }],
      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: "资产现场验证状态"
      }
      ]
    }
  },
  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: ["BuildLocalName", "BuildName", "BuildID"]
          },
          {
            Name: "floor",
            Projection: ["FloorLocalName", "FloorName", "FloorID"]
          }
        ],
        Filters: " not propertyId isnull ",
        Orders: "CreateTime desc, EquipID asc",
        PageNumber: pageNum,
        PageSize: 1000,
        Projection: ["CreateTime", "BuildingId", "FloorId", "Category", "EquipLocalName", "EquipLocalID", "EquipID", "BIMID"]
      }
      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: ["BuildLocalName", "BuildName", "BuildID"]
          },
          {
            Name: "floor",
            Projection: ["FloorLocalName", "FloorName", "FloorID"]
          }
        ],
        Filters: " propertyId isnull ",
        Orders: "CreateTime desc, EquipID asc",
        PageNumber: pageNum,
        PageSize: 1000,
        Projection: ["CreateTime", "BuildingId", "FloorId", "Category", "EquipLocalName", "EquipLocalID", "EquipID", "BIMID"]
      }
      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: [ "BuildLocalName", "BuildName", "BuildID" ]
          },
          {
            Name: "floor",
            Projection: [ "FloorLocalName", "FloorName", "FloorID" ]
          }
        ],
        Filters: " EquipmentId isnull ",
        Orders: "CreateTime desc, EquipID asc",
        PageNumber: pageNum,
        PageSize: 1000,
        Projection: [ "BuildingId", "FloorId", "Family", "EquipLocalName", "EquipLocalID", "EquipID", "CreateTime", "CodeType" ]
      }
      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>
