<template>
  <div id="cascaderMap">
    <span class="buildFloor" style="padding-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.Category" :label="item.CategoryName" :value="item.Category"></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 { queryLinkSys, getEqCode } from "@/api/scan/request";
export default {
  name: "getCode",
  props: {
    isWidth: {
      type: Boolean,
      default: true
    },
    all: {
      default: false
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  data() {
    return {
      value: [],
      options: [],
      props: {
        value: "Category",
        label: "CategoryName"
      }
    };
  },
  created() {
    this.init()
  },
  watch: {
    projectId() {
      this.value = ''
      this.init()
    }
  },
  methods: {
    init() {
      if (this.all) {
        //物理世界所有系统
        this.getAllData();
      } else {
        //当前项目下已有的系统
        this.getData();
      }
    },
    setValue(val) {
      this.value = val
    },
    //修改val
    changeVal(val) {
      let value = {}
      this.options.map(item => {
        if (item.Category == val) {
          value = item
        }
      })
      this.value = val
      this.$emit("change", value)
    },
    getData() {
      let param = {
        Distinct: true,
        PageNumber: 1,
        PageSize: 500,
        Projection: [
          "Category", "CategoryName"
        ]
      }
      queryLinkSys(param, res => {
        this.options = res.Content;
        if (this.value) {
          this.changeVal(this.value)
        }
      })
    },
    getAllData() {
      getEqCode().then(res => {
        this.options = this.changeArr(res.data.Content)
        if (this.value) {
          this.changeVal(this.value)
        }
      })
    },
    changeArr(arr) {
      let data = [];
      arr.map(item => {
        if (item.content && item.content.length) {
          return item.content.map(children => {
            data.push({ Category: children.code, CategoryName: children.system });
          });
        }
      });
      return data;
    }
  }
};
</script>
<style lang="less" scoped>
#cascaderMap {
  float: left;
  margin-left: 10px;
  .buildFloor {
    color: #999999;
    font-size: 14px;
  }
}
</style>
