<template>
  <div id="cascaderMap">
    <span class="buildFloor" style="margin-right: 12px;">设备类别</span>
    <el-select v-model="value" placeholder="请选择" :props="props" filterable :style="isWidth ? '' : 'width:160px;'"
               size="small" @change="changeVal">
      <el-option v-for="item in options" :key="item.code" :label="item.name" :value="item.code"></el-option>
    </el-select>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import { queryPhysicsAllType } from "@/api/dict";

import { queryEquip } from "@/api/object/equip";

export default {
  name: "getCode",
  props: {
    isWidth: {
      type: Boolean,
      default: true
    },
    all: {
      type: Boolean,
      default: false,
    },
    //系统
    params: Object
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  data() {
    return {
      value: "",
      options: [],
      props: {
        value: "code",
        label: "name"
      },
      falg: true,
      content: []
    };
  },
  created() {
    this.getData();
  },
  watch: {
    projectId() {
      this.value = '';
      this.getData();
    }
  },
  methods: {
    setValue(val) {
      this.value = val
    },
    //修改val
    changeVal(val) {
      let value = {}
      this.options.map(item => {
        if (item.code == val) {
          value = item
        }
      })
      this.value = val
      this.$emit("change", value)
    },
    //获取当前项目下的设备类型(只拿到编码-需要过滤)
    getData() {
      let param2 = {
        distinct: true,
        pageNumber: 1,
        pageSize: 1000,
        projection: [
          "classCode"
        ]
      }
      let param1 = { type: 'equipment' }
      let promise2 = new Promise((resolve, reject) => {
        queryEquip(param2, res => {
          resolve(res)
        })
      })
      let promise1 = new Promise((resolve, reject) => {
        queryPhysicsAllType(param1, res => {
          let tempArr = res.content;
          if (this.params.SysType) {
            tempArr = res.content.filter(t => {
              // todo parentId 这个字段后台没有返回
              return t.parentId == `${this.params.SysType}`
            })
          }
          resolve(tempArr)
        })
      })
      Promise.all([promise1, promise2]).then((res) => {
        let allData = res[0], data = res[1]
        // this.options = this.formatOptions(allData)
        this.options = allData
        if (this.value) {
          this.changeVal(this.value)
        }
        if (!this.all) {
          this.content = data.content.map(t => {
            return t.classCode
          });
          this.filterForOptions();
          if (this.value) {
            this.changeVal(this.value)
          }
        }
      })
    },
    //格式化options数据
    // formatOptions(arr) {
    //   let data = [];
    //   arr.map(t => {
    //     let temp = {};
    //     temp.code = t.code;
    //     temp.facility = t.name;
    //     data.push(temp)
    //   })
    //   return data;
    // },
    //过滤
    filterForOptions() {
      this.options = this.options.filter(item => {
        if (this.content.indexOf(item.code) > -1) {
          return item
        }
      })
    }
  }
};
</script>
<style lang="less" scoped>
#cascaderMap {
  float: left;
  margin-left: 10px;

  .buildFloor {
    color: #999999;
    font-size: 14px;
  }
}
</style>
