<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.facility" :value="item.code"></el-option>
    </el-select>
    <!-- <el-cascader placeholder="请选择" :options="options" v-model="value" :props="props" filterable :style="isWidth ? '' : 'width:160px;'" size="small"
      @change="changeVal" change-on-select></el-cascader> -->
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
import { getEqCode, getEquipBelongs, queryPartsDie } from "@/api/scan/request";
export default {
  name: "getCode",
  props: {
    isWidth: {
      type: Boolean,
      default: true
    },
    Family: {
      type: String,
      default: '',
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  data() {
    return {
      value: "",
      options: [],
      props: {
        value: "code",
        label: "facility"
      },
      falg: true,
      content: []
    };
  },
  mounted() {
    if(this.Family != "") {
      this.getAllData()
    }
  },
  watch: {
    projectId() {
      this.value = '';
      this.getAllData()
    }
    // Family: {
    //   handler: function (val) {
    //     if(val != "") {
    //       this.getAllData()
    //     }
    //   },
    //   immediate: true
    // }
  },
  methods: {
    setValue(val) {
      if (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 param = {
        Distinct: true,
        PageNumber: 1,
        PageSize: 500,
        Projection: [
          "Category"
        ]
      }
      queryPartsDie(param, res => {
        this.content = res.Content
        this.filterForOptions()
        let val = this.options.length?this.options[0].code:""
        this.changeVal(val)
      })
    },
    //获取物理世界所有设备类型
    getAllData() {
      let param = {
        data: {
          Filters: `Family='${this.Family}'`,
          Distinct: true,
          Orders: "EquipName asc",
          PageNumber: 1,
          PageSize: 500,
          Projection: [
            "EquipCode", "EquipName"
          ]
        }
      }
      getEquipBelongs(param, res => {
        this.options = this.formatOptions(res.Content)
        this.getData()
      })
    },
    //格式化options数据
    formatOptions(arr) {
      let data = [];
      arr.map(t => {
        let temp = {};
        temp.code = t.EquipCode;
        temp.facility = t.EquipName;
        data.push(temp)
      })
      return data;
    },
    //过滤
    filterForOptions() {
      this.options = this.options.filter(item => {
        if (this.content.find(value => value.Category == item.code)) {
          return item
        }
      })
    }
  }
};
</script>
<style lang="less" scoped>
#cascaderMap {
  float: left;
  margin-left: 10px;
  .buildFloor {
    color: #999999;
    font-size: 14px;
  }
}
</style>
