<template>
  <div class="m-subbox m-rfsjtj">
    <div class="m-rfsjtj-item">
      <div class="m-rfsjtj-item-des">采集房屋数</div>
      <div class="m-rfsjtj-item-jian">
        <span>{{ numFormat(house.sumNum) }}</span>
        间
      </div>

      <div class="m-rfsjtj-item-change">
        <div class="m-rfsjtj-item-change-today">今日</div>
        <div class="m-rfsjtj-item-change-new">新增<span>{{ house.dayAddNum }}</span></div>
        <div class="m-rfsjtj-item-change-update">更新<span>{{ house.dayUpdateNum }}</span></div>
      </div>
    </div>
    <div class="m-rfsjtj-item">
      <div class="m-rfsjtj-item-des">采集人口数</div>
      <div class="m-rfsjtj-item-ren">
        <span>{{ numFormat(resident.sumNum) }}</span>
        人
      </div>
      <div class="m-rfsjtj-item-change">
        <div class="m-rfsjtj-item-change-today">今日</div>
        <div class="m-rfsjtj-item-change-new">新增<span>{{ resident.dayAddNum }}</span></div>
        <div class="m-rfsjtj-item-change-update">更新<span>{{ resident.dayUpdateNum }}</span></div>
      </div>
    </div>
  </div>
</template>
<script>
var timer
export default {
  name: "rfsjtj",
  data() {
    return {
      org: this.$store.state.chooseArea.chooseName,
      house: {
        dayAddNum: 0,
        dayUpdateNum: 0,
        sumNum: 0,
      },
      resident: {
        dayAddNum: 0,
        dayUpdateNum: 0,
        sumNum: 0,
      },
    }
  },
  watch: {
    "$store.state.chooseArea.chooseName"(val) {
      if (val.orgId) {
        this.getData(this.$store.state.chooseArea.chooseName)
      }
    }
  },
  mounted() {
    if (this.org.orgId) {
      this.getData(this.org)
    }
  },
  activated() {
    if (this.$store.state.chooseArea.chooseName.orgId) {
      this.getData(this.$store.state.chooseArea.chooseName)
    }
  },
  destroyed() {
    clearInterval(timer)
    console.log('xiaohui')
  },
  deactivated() {
    clearInterval(timer)
    console.log('xiaohui1')
  },
  methods: {
    numFormat(num) {
      num = num.toString().split("."); // 分隔小数点
      var arr = num[0].split("").reverse(); // 转换成字符数组并且倒序排列
      var res = [];
      for (var i = 0, len = arr.length; i < len; i++) {
        if (i % 3 === 0 && i !== 0) {
          res.push(","); // 添加分隔符
        }
        res.push(arr[i]);
      }
      res.reverse(); // 再次倒序成为正确的顺序
      if (num[1]) {
        // 如果有小数的话添加小数部分
        res = res.join("").concat("." + num[1]);
      } else {
        res = res.join("");
      }
      return res;
    },
    getData({orgId, level}) {
      clearInterval(timer)
        this.$http.get('/actual/base/streetOverview/residentHouseGroup?orgId=' + orgId + '&level=' + level).then(({data: {data}}) => {
          data.forEach(item => {
            if (item.type === 'house') {
              this.house = item
            }
            if (item.type === 'resident') {
              this.resident = item
            }
          });
        })
      timer = setInterval(() => {
        this.getData({orgId, level})
      },1000 * 60)

    }
  },
};
</script>

<style
    lang="scss"
    src="@/assets/scss/dataBoard/overview/index.scss"
    scoped
></style>
