<template>
  <v-select :options='resoptions' placeholder='请选择'
            :value.sync="selectres"
            :multiple="isMul"
            search="true"
            :disabled="mustselect"
            :close-on-select="!isMul"
            @change="resChange"
  >
  </v-select>
</template>
<script>
  import {HttpResetClass} from 'vue-client'

  export default {
    title: '资源选择',
    props: {

      //资源类型
      restype:'',
      //是否只查询营业厅
      specialquery: {
        type: Boolean,
        default: false
      },
      //资源数据
      resObj:{},
      isMul: {
        type: Boolean,
        default: true
      },
      //资源初始化数据
      initresid: {
        type: Array,
        default() { return [] },
      },
      //父层id
      parentresid: {
        type: Array,
        default() { return [] },
      },
      mustselect: {
        type: Boolean,
        default: false
      },
      filterIds: {
        type: Array,
        default() { return null }
      }
    },
    data () {
      return {
        //资源数据
        resObj:{},
        //资源数据列表
        resoptions:[],
        //选中资源数据
        selectres: [],
      }
    },
    ready () {
      //获取资源列表
      this.getResList()
    },
    methods:{
      //树形结构变成list
      findById(val) {
        if(val){
          if(this.checkidres(val)){
            this.treetoList(val,'')
          }else if(val.children.length>0){
            for (let value of val.children) {
              this.findById(value)
            }
          }
        }
      },
      //树形结构变成list
      treetoList(val,parentname) {
        for (let value of val.children) {
          this.ergodicList(value,parentname)
        }
      },
      //找到跟节点
      ergodicList (val,parentname) {
        if(val.children.length > 0 ){
          parentname=parentname+val.name
          this.treetoList(val,parentname)
        }else {
          if(this.filterIds && !this.filterIds.includes(val.id)){
            return
          }
          if(this.restype=='organization'){
            parentname=parentname.replace("组织机构","")
            this.resoptions.push({label: val.name+' - '+parentname, value: val.id})
          }else{
            if (this.specialquery && this.restype == 'department') {
              if (val.f_dep_type === '营业厅') {
                this.resoptions.push({label: val.name, value: val.id})
              }
            }else {
              this.resoptions.push({label: val.name, value: val.id})
            }
          }
        }
      },
      //检查id是否存在
      checkidres(val){
        let flag=false
        //如果没传值得话 不过滤
        if(this.parentresid.length==0&&this.restype=='organization'){
          flag=true
        }
        this.parentresid.forEach((item) => {
          if(item==val.id){
            flag=true
          }
        })
        return flag;
      },
      //资源改变
      resChange(val){
        let orgnames=[]
        Object.keys(this.resoptions).forEach((key) => {
          if(this.selectres.includes(this.resoptions[key].value))
            orgnames.push(this.resoptions[key].label)
        })
        console.log('资源改变。。', val, orgnames)
        this.$dispatch('res-select', this.selectres, orgnames)
      },
      //获取资源数据
      async getResList() {

        let http = new HttpResetClass()

        let req = await http.load('POST', '/rs/search', {data: {
            source: `tool.getFullTree(this.getRights().where(row.getType()==$${this.restype}$))`,
            userid: this.$login.f.id
          }}, {resolveMsg: null, rejectMsg: '获取组织结构数据出错'})
        this.resObj=req.data[0]
        this.dealdata()
      },
      dealdata(){
        this.resoptions=[]
        this.findById(this.resObj);
        //赋值资源数据
        let arryselect=[]
        this.resoptions.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()
      },
      'filterIds'(newVal, oldVal) {
        if (JSON.stringify(newVal) === JSON.stringify(oldVal)) {
          return
        }
        this.dealdata()
      },
      initresid: {
        handler(newVal, oldVal) {
          // 避免重复触发
          if (JSON.stringify(newVal) === JSON.stringify(oldVal)) {
            return;
          }
          this.dealdata();
        },
      }
    },
  }
</script>
