<template>
  <div id="deviceList">
    <router-link to="/ledger/facility/addfacility"></router-link>
    <router-view></router-view>
    <saga-title :mess="mess"></saga-title>
    <div class="table-main">
      <div class="search-header">
        <floor-cascader @change="changeFloor"></floor-cascader>
        <my-cascader ref="cascader" @change="changeDevice"></my-cascader>
      </div>
      <hanson-table @close="close" :graphyId="graphyId" ref="tableMain"></hanson-table>
    </div>
    <!-- <table-transfers></table-transfers> -->
  </div>
</template>
<script>
import sagaTitle from "@/components/ledger/lib/title";
import floorCascader from "@/components/ledger/lib/floorCascader";
import myCascader from "@/components/ledger/lib/cascader";
import hansonTable from "@/components/ledger/handsontables/device";
import tableTransfers from "@/components/ledger/tableTransfers";
import { getNumber, getFamilyList, getSpaceName, getGraphyId, getEquipmentFamily } from "@/api/scan/request"
import {
  mapGetters,
  mapActions
} from "vuex";
export default {
  components: {
    sagaTitle,
    floorCascader,
    myCascader,
    hansonTable,
    tableTransfers
  },
  data() {
    return {
      mess: {
        perjectName: "",
        name: [
          {
            key: "设备",
            num: 0
          },
          {
            key: "资产",
            num: 0
          }
        ]
      },
      param: {
        buildId: "",
        floorId: "",
        deviceId: "",
        ProjId: "",
        secret: ""
      },
      assetGroupList: [],
      graphyId: null
    };
  },
  computed: {
    ...mapGetters("layout", [
      "projectId",
      "secret",
      "userId"
    ])
  },
  created() {
    this.param.ProjId = this.projectId
    this.param.secret = this.secret
    this.getProjName()

    let deviceId = this.$route.query.deviceId;
    if (deviceId) {
      this.$nextTick(() => {
        this.$refs.cascader.changeVal([deviceId])
      })
    }
  },
  watch: {
    projectId() {
      this.param.ProjId = this.projectId
      this.param.secret = this.secret
      this.getProjName()
    }
  },
  methods: {
    getProjName() {
      let param = {
        list: [{ id: this.param.ProjId }],
        perjectId: this.param.ProjId,
        secret: this.param.secret,
      }
      getSpaceName(param).then(res => {
        if (res.data.Result == "success") {
          this.mess.perjectName = res.data.Content[0].infos.ProjLocalName
          this.getEqCode()
        } else {
          this.$message.error("请求失败：" + res.data.ResultMsg)
        }
      }).catch(_ => {
        this.$message.error("请求失败")
      })
    },
    close(val) {
      this.getEqCode()
      if (val && this.$refs.cascader)
        this.$refs.cascader.changeVal([val.code])
      //   this.$refs.tableMain.getHeaderData(this.param);
    },
    getEqCode() {
      getEquipmentFamily(res => {
        this.assetGroupList = res.Content
        this.getGraphyId()
      })
    },
    getFamilyList() {
      let param = {
        ProjId: this.param.ProjId,
        secret: this.param.secret,
        data: { type: "Eq" }
      }
      getFamilyList(param, res => {
        if (this.$refs.cascader) {
          this.$refs.cascader.pushData(res.Content)
        }

      })
    },
    //获取header的list
    getNumber() {
      let param = {
        ProjId: this.param.ProjId,
        secret: this.param.secret,
        data: { type: "Eq" }
      }
      getNumber(param, res => {
        this.mess.name[0].num = res.content.count
        this.mess.name[1].num = res.content.bindingCount
        this.getFamilyList()
      })
    },

    getGraphyId() {
      let param = {
        ProjId: this.param.ProjId,
        secret: this.param.secret,
        type: "SystemEquip"
      }
      getGraphyId(param).then(res => {
        if (res.data.Result == "success") {
          this.graphyId = res.data.graph_id
          if (this.$refs.tableMain)
            this.$refs.tableMain.setGraphyId(this.graphyId, this.assetGroupList);
          this.getNumber()
        } else {
          this.$message.error("请求错误：" + res.data.ResultMsg)
        }
      }).catch(_ => {
        this.$message.error("请求错误")
      })
    },
    //修改楼层
    changeFloor(value) {
      if (value[0]) {
        this.param.buildId = value[0]
      }
      if (value[1]) {
        this.param.floorId = value[1]
      } else {
        this.param.floorId = null
      }
      if (!!this.param.deviceId) {
        if (this.$refs.tableMain)
          this.$refs.tableMain.getHeaderData(this.param);
      } else {
        return;
      }
    },

    //修改设备族
    changeDevice(value) {
      this.param.deviceId = value.code
      this.param.name = value.facility
      if (!!value) {
        if (this.$refs.tableMain)
          this.$refs.tableMain.getHeaderData(this.param);
      } else {
        return;
      }

    }
  }
};
</script>
<style lang="less" scoped>
#deviceList {
  overflow: hidden;
  height: 100%;
  background-color: #fff;
  position: relative;
  .table-main {
    position: absolute;
    top: 87px;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #fff;
    // border-radius: 5px;
    // padding: 10px 0;
  }
  .search-header {
    overflow: hidden;
    padding: 0 10px 10px 10px;
    border-bottom: 1px solid #bcbcbc;
  }
}
</style>