<template>
  <area-select :options='options' placeholder='请选择'
            :value.sync="selectres"
            :multiple="!isMul"
            :search="issearch"
            :close-on-select="isMul"
            @change="resChange"
            :value-single="true"
  >
  </area-select>
</template>
<script>
import {HttpResetClass} from 'vue-client'
import AreaSelect from "./MyAreaSelect.vue";
export default {
  title: '资源管理',
  components: {
    AreaSelect
  },
  props: {
    //资源类型
    restype:'',
    //资源数据
    resObj:{},
    options:[],
    isMul: {
      type: Boolean,
      default: true
    },
    issearch: {
      type: Boolean,
      default: true
    },
    //资源初始化数据 默认选中值
    initresid: {
      type: Array,
      default() { return [] },
    },
    //父层id
    parentresid: {
      type: Array,
      default() { return [] },
    },

  },
  data () {
    return {
      //资源数据
      resObj:{},
      //资源数据列表
      resoptions:[],
      //选中资源数据
      selectres: []
    }
  },
  ready () {
    //获取资源列表
    this.getResList()
  },
  methods:{
    //资源改变
    resChange(val){
      let orgnames=[]
      Object.keys(this.options).forEach((key) => {
        if(this.selectres.includes(this.options[key].value))
          orgnames.push(this.options[key].label)
      })
      // console.log("资源变化:",this.selectres, orgnames,this.options)
      this.$dispatch('res-select', this.selectres, orgnames,this.options)
    },
    //获取资源数据
    async getResList() {
      this.dealdata()
    },
    async dealdata(){
      //赋值资源数据选中初始值
      let arryselect=[]
      this.options.forEach((item) => {
        if(this.initresid.length>0){
          this.initresid.forEach((init) => {
            if(item.value==init){
              arryselect.push(item.value)
            }
          })
        }
      })
      //赋值资源选中初始值
      this.selectres=arryselect
    }
  },
  watch: {
    //监听初始化资源id
    'initresid.length'(){
      this.dealdata()
    },
    //监听初始化资源id
    'parentresid'(){
      this.dealdata()
    },
  },
}
</script>
