<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.classCode" :label="item.categoryName"
                 :value="item.classCode"></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 } from "@/api/scan/request";
import { queryPhysicsAllType } from "@/api/dict";


export default {
  name: "getCode",
  props: {
    isWidth: {
      type: Boolean,
      default: true
    },
    all: {
      default: false
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  data() {
    return {
      value: [],
      options: [],
      props: {
        value: "classCode",
        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.classCode == val) {
          value = item
        }
      })
      this.value = val
      this.$emit("change", value)
    },
    getData() {
      let param = {
        distinct: true,
        pageNumber: 1,
        pageSize: 1000,
        projection: [
          "classCode", "CategoryName"
        ]
      }
      queryLinkSys(param, res => {
        this.options = res.content;
        if (this.value) {
          this.changeVal(this.value)
        }
      })
    },
    getAllData() {
      let param = {
        type: 'system'
      }
      queryPhysicsAllType(param, res => {
        this.options = this.changeArr(res.content)
        if (this.value) {
          this.changeVal(this.value)
        }
      })
    },
    changeArr(arr) {
      let data = [];
      arr.forEach(item => {
        data.push({ classCode: item.classCode || item.code, categoryName: item.categoryName || item.name });
      });
      console.log(data, 'data')
      return data;
    }
  }
};
</script>
<style lang="less" scoped>
#cascaderMap {
  float: left;
  margin-left: 10px;

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