<template>
  <div>
    <div class="row header">
      <span class="type">序号</span>
      <span class="count">建筑名称</span>
      <span class="time" @click="sort('gd')"
        >建筑高度(m)
        <img
          src="../../../assets/images/Group 1321314644.png"
          alt=""
          v-if="sortGdAsc === true"
        />
        <img
          src="../../../assets/images/Group 1321314644.png"
          class="rotate"
          alt=""
          v-if="sortGdAsc === false"
        />
      </span>
      <span class="unit" @click="sort('mj')"
        >建筑面积(㎡)
        <img
          src="../../../assets/images/Group 1321314644.png"
          alt=""
          v-if="sortMjAsc === true"
        />
        <img
          src="../../../assets/images/Group 1321314644.png"
          class="rotate"
          alt=""
          v-if="sortMjAsc === false"
        />
      </span>
    </div>
    <VueSeamlessScroll
      :data="dataList"
      :class-option="classOption"
      class="warp"
    >
      <ul class="item">
        <li class="row" v-for="(item, index) in dataList" :key="index" @click="TounitInfo(item)">
          <span class="type">{{ index + 1 }}</span>
          <span class="count">{{ item.gcjzmc }}</span>
          <span class="time">{{ Number(item.gd).toFixed(0) }}</span>
          <span class="unit">{{ item.jzmj }}</span>
        </li>
      </ul>
    </VueSeamlessScroll>
  </div>
</template>

<script>
import VueSeamlessScroll from "vue-seamless-scroll";

export default {
  name: "BuildingStatistics",
  components: {
    VueSeamlessScroll,
  },
  props: {
    list: {
      type: Array,
      default: () => [],
    },
  },

  data() {
    return {
      dataList: [],
      sortGdAsc: null,
      sortMjAsc: null,
    };
  },
  watch: {
    list: {
      handler(val) {
        const temp = JSON.stringify(val)
        this.dataList = JSON.parse(temp).sort((a,b)=>{
          return Number(b.gd)-Number(a.gd)
        });

      },
      // immediate: true,
      deep: true,
    },
  },
  computed: {
    classOption() {
      return {
        singleHeight: 51,
      };
    },
  },
  created() {
    this.dataList = this.list.sort((a,b)=>{
          return Number(b.gd)-Number(a.gd)
        });
  },
  methods: {
    sort(type) {
      if (type === "gd") {
        if (this.sortGdAsc === null) {
          this.sortGdAsc = true;
          this.sortMjAsc = null;
        } else if (this.sortGdAsc === true) {
          this.sortGdAsc = false;
          this.sortMjAsc = null;
        } else if (this.sortGdAsc === false) {
          this.sortGdAsc = null;
          this.sortMjAsc = null;
        }
      } else if (type === "mj") {
        if (this.sortMjAsc === null) {
          this.sortMjAsc = true;
          this.sortGdAsc = null;
        } else if (this.sortMjAsc === true) {
          this.sortMjAsc = false;
          this.sortGdAsc = null;
        } else if (this.sortMjAsc === false) {
          this.sortMjAsc = null;
          this.sortGdAsc = null;
        }
      }

      let dataList = JSON.parse(JSON.stringify(this.list));
      const self = this;
      dataList = dataList.sort(function (a, b) {
        if (type === "gd") {
          if (self.sortGdAsc) {
            return parseFloat(a.gd) - parseFloat(b.gd);
          } else {
            return parseFloat(b.gd) - parseFloat(a.gd);
          }
        } else {
          if (self.sortMjAsc) {
            return parseFloat(a.jzmj) - parseFloat(b.jzmj);
          } else {
            return parseFloat(b.jzmj) - parseFloat(a.jzmj);
          }
        }
      });
      //   console.log(dataList);
      this.dataList = null;
      this.dataList = dataList;
    },
    TounitInfo(node){
      window.location.href = `/detail?id=${node.jzid}`;
    }
  },
};
</script>
<style scoped lang="less">
.warp {
  height: 521px;
  margin: 0 auto;
  overflow: hidden;

  .item {
    list-style: none;
    padding: 0;
    margin: 0 auto;
    cursor: pointer;
  }
  &:hover {
    overflow: auto;
  }
}

.header {
  color: #61dbff !important;
  height: 38px !important;
  background-color: rgba(0, 163, 255, 0.3) !important;
  margin-top: 2px;
  letter-spacing: 1px;

  .is_notice {
    line-height: 19px;
    font-size: 10px;
  }
}

li.row > span {
  text-align: center;
  font-size: 14x;
  position: relative;
  display: inline-block;
}

li.row {
  //   border-bottom: 1px dotted #1d525f;
  box-sizing: border-box;
}

.row,
li,
a {
  display: block;
  height: 46px;
  line-height: 46px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
  background-color: rgba(0, 0, 0, 0.2);
  color: #fff;
  margin-top: 5px;

  .time,
  .type,
  .count,
  .unit {
    display: flex;
    justify-content: center;
    align-items: center;
    display: inline-block;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
    text-align: center;
  }

  .time {
    cursor: pointer;
    flex: 0.25;
  }
  .count {
    flex:1;
  }
  .type {
    flex: 0.1;
  }

  .count {
    flex: 0.2;
  }

  .unit {
    cursor: pointer;
    flex: 0.25;
  }
}

.rotate {
  transform: rotate(180deg);
}
</style>
