<template>
  <div id="deviceList">
    <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="change" 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/assets";
import hansonTable from "@/components/ledger/handsontables/assets";
import tableTransfers from "@/components/ledger/tableTransfers";
import { mapGetters, mapActions } from "vuex";
import { countProperty, queryProperty } from "@/api/scan/request"
import "./../index.scss"

export default {
  components: {
    sagaTitle,
    floorCascader,
    myCascader,
    hansonTable,
    tableTransfers
  },
  computed: {
    ...mapGetters("layout", ["projectId", "projects", "secret", "userId"])
  },
  data() {
    return {
      mess: {
        perjectName: "",
        name: [
          {
            key: "资产",
            num: 0
          },
          {
            key: "岗位",
            num: 0
          }
        ]
      },
      param: {
        buildId: "",
        floorId: "",
        deviceId: "",
        ProjId: '',
        secret: ''
      },
      list: []
    };
  },
  created() {
    this.param.ProjId = this.projectId
    this.param.secret = this.secret
    this.getProjName()
    this.getNumber()
    let Family = this.$route.params.Family
    if (Family) {
      this.$nextTick(() => {
        this.$refs.cascader.changeVal(Family)
      })
    }
  },
  watch: {
    projectId() {
      this.param.ProjId = this.projectId
      this.param.secret = this.secret
      this.getProjName()
      this.getNumber()
    }
  },
  methods: {
    getProjName() {
      this.projects.forEach((item) => {
        if (item.id == this.projectId)
          this.mess.perjectName = item.name
      })
    },
    //获取header的list
    getNumber() {
      countProperty({}, res => {
        this.mess.name[0].num = res.count
      })
      countProperty({ "filters": "not equipId isNull" }, res => {
        this.mess.name[1].num = res.count
      })
    },
    //修改楼层
    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;
      }
    },

    //新建资产后进行刷新
    change(val) {      
      this.param.deviceId = val.code
      this.getNumber()
      if (val && this.$refs.cascader) {
        this.$refs.cascader.changeVal(val.code)
      }
    //   if (!!this.param.deviceId) {
    //     if (this.$refs.tableMain)
    //       this.$refs.tableMain.getHeaderData(this.param);
    //   }
    },

    //修改设备族
    changeDevice(value) {
      if (value.code && value.facility) {
        this.param.deviceId = value.code
        if (this.$refs.tableMain)
          this.$refs.tableMain.getHeaderData(this.param);
      }
    }
  }
};
</script>
<style lang="scss" 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-bottom: 10px;
    margin: 0 10px;
    border-bottom: 1px solid #bcbcbc;
  }
}
</style>
