<!--
 * @Author: your name
 * @Date: 2022-03-29 09:41:23
 * @LastEditTime: 2022-03-29 11:08:25
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: /gassafety-progress/gassafetyprogress-web/src/views/operationMonitor/monitorData/component/charsData/Filter.vue
-->
<template>
  <div class="fliter-vue">
    <div class="filter" v-for="(item, index) in filterList" :key="item.value">
      <!-- 全部 -->
      <template v-if="index == 0">
        <span
          class="text"
          :class="{
            active: deviceTypeArr.length == filterList.length - 1
          }"
          @click="fliterTypeChange(item)"
          >{{ item.value }}
          <i class="iconfont icon-bingo"></i>
        </span>
        <span class="number">({{ item.count }})</span>
      </template>
      <template v-else>
        <span
          class="text"
          :class="{ active: deviceTypeArr.indexOf(item.type) >= 0 }"
          @click="fliterTypeChange(item)"
          >{{ item.value }}
          <i class="iconfont icon-bingo"></i>
        </span>
        <span class="number">({{ item.count }})</span>
      </template>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      deviceTypeArr: [],
      allArray: [],
    };
  },
  props: {
    // 传进来的参数
    filterList: Array,
  },
  created() {
    this.deviceTypeArr = this.filterList
      .filter((item) => item.type > 0)
      .map((item) => item.type);
    // 全选时的数组内容
    this.allArray = [...this.deviceTypeArr];
  },
  methods: {
    fliterTypeChange(data) {
      const { type } = data;
      // 如果点的是全选
      if (type == "all") {
        if (this.deviceTypeArr.length == this.filterList.length - 1) {
          this.deviceTypeArr = [];
        } else {
          this.deviceTypeArr = [...this.allArray];
        }
      } else {
        const index = this.deviceTypeArr.indexOf(type);
        if (index >= 0) {
          this.deviceTypeArr.splice(index, 1);
        } else {
          this.deviceTypeArr.push(type);
        }
      }
      this.$emit("fliterTypeChange", [...this.deviceTypeArr]);
    },
  },
};
</script>

<style lang="scss" scoped>
.fliter-vue {
  display: flex;
}
.filter {
  margin-right: 30px;
  margin-top: 5px;
  font-size: 14px;
  .text {
    cursor: pointer;
    position: relative;
    .iconfont {
      font-size: 10px;
      position: absolute;
      left: -12px;
      top: 0px;
      color: rgba(0, 0, 0, 0);
    }
    &:before {
      content: "";
      position: absolute;
      width: 10px;
      height: 10px;
      border: 1px solid #000000;
      left: -12px;
      top: 50%;
      margin-top: -5px;
    }
    &:hover {
      .iconfont {
        font-size: 10px;
        position: absolute;
        left: -12px;
        color: rgba(0, 0, 0, .5);
      }
    }
    &.active {
       .iconfont {
        font-size: 10px;
        position: absolute;
        left: -12px;
        color: rgba(0, 0, 0, 1);
      }
    }
  }
  .number {
    font-weight: 500;
    color: #1890ff;
  }
}
</style>