<template>
  <div id="cascaderMap" :style="{'justify-content': all ? 'space-around' : ''}">
    <div>
      <span class="buildFloor" style="padding:0 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>
    </div>
    
    <el-checkbox v-if='all' v-model="isDelivery" @change="deliverChange" >仅项目交付范围内</el-checkbox>
  </div>
</template>
<script>
import { mapGetters } from 'vuex';
// import { queryLinkSys } from "@/api/scan/request";
// import { queryPhysicsAllType } from "@/api/dict";
import Dic from '@/controller/old-adm/dicController'
import Scan from '@/controller/old-adm/ScanController'

const { queryPhysicsAllType } = Dic;
const { queryLinkSys } = Scan;


export default {
  name: "getCode",
  props: {
    isWidth: {
      type: Boolean,
      default: true
    },
    all: {
      default: false
    }
  },
  computed: {
    ...mapGetters("layout", ["projectId"])
  },
  data() {
    return {
      value: [],
      options: [],
      full_options: [],
      props: {
        value: "classCode",
        label: "categoryName"
      },
      isDelivery: true,
      query: {},
    };
  },
  created() {
    this.init()
  },
  watch: {
    projectId() {
      this.value = ''
      this.init()
    }
  },
  methods: {
    init() {
      if (this.all) {
        //物理世界所有系统
        this.getAllData();
      } else {
        //当前项目下已有的系统
        this.getData();
      }
    },
    //修改val
    changeVal(val) {
      let value = {}
      this.options.map(item => {
        if (item.classCode == val) {
          value = item
        }
      })
      this.value = val;
      this.query = {...this.query, ...value};
      this.$emit("change", this.query)
    },
    async getData() {
      let param = {
        distinct: true,
        pageNumber: 1,
        pageSize: 1000,
        projection: [
          "classCode", "CategoryName"
        ]
      }
      try {
        const res = await  queryLinkSys(param);
        this.options = this.changeArr(res.content)
        // console.log({res})
        if (this.value && this.options.find(item => item.classCode === this.value)) {
          this.changeVal(this.value)
        } else {
            this.changeVal("")
        }
      } catch (error) {
        console.log({error})
      }
      
      
    },
   async getAllData() {
      let param = {
        type: 'system'
      }
      const res = await queryPhysicsAllType(param);
      this.full_options = this.changeArr(res.content);
      this.options = this.isDelivery ? this.full_options.filter(item => item?.isDelivery) : this.full_options
      if (this.value && this.options.find(item => item.classCode === this.value)) {
        this.changeVal(this.value)
      } else {
          this.changeVal("")
      }
      
    },
    changeArr(arr) {
      let data = [];
      arr.forEach(item => {
        data.push({ 
          classCode: item.classCode || item.code, 
          categoryName: item.categoryName || item.name,
          isDelivery: item?.isDelivery
        });
      });
      // console.log(data, 'data')
      return data;
    },
    deliverChange(val) {
      this.query = {...this.query, isDelivery: val ? 'Visible' : 'all'}; 
      this.options = val ? this.full_options.filter(item => item?.isDelivery === 1) : this.full_options;   
      this.$emit("change", this.query)
    }
  }
};
</script>
<style lang="less" scoped>
#cascaderMap {
  display: flex;
  // justify-content: space-around;
  align-items: center;

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