<template>
  <div style="width: 100%;height: 100%">
    <div class="col-sm-6 form-group" style="margin-bottom: 0px">
      <label class="font_normal_body">公&emsp;&emsp;司</label>
      <right-tree-safe :width="leftWidth" @re-res="getRes" :resobjprop.sync="resobjprop"></right-tree-safe>
    </div>
    <div class="col-sm-6 form-group" style="margin-bottom: 0px">
      <label class="font_normal_body">{{ roleLable }}</label>
      <v-select :value="selVal" v-model='selVal'
                :value-single="!valueMultiple"
                class="select_list select"
                :options='users' :placeholder='roleName'
                :close-on-select = 'valueClose'
                :multiple="valueMultiple"
                :width="rightWidth"
                @change="valuechange">
      </v-select>
    </div>
  </div>
</template>

<script>
  import {HttpResetClass} from 'vue-client'

  export default {
    name: "RoleSelector",
    props: {
      valueMultiple: {
        type: Boolean,
        default: false
      },
      roleName: {
        type: String,
        default: '安检员'
      },
      roleLable: {
        type: String,
        default: '安检员'
      },
      leftWidth: {
        type: String,
        default: '60%'
      },
      rightWidth: {
        type: String,
        default: '60%'
      },
      value: {
        default: ''
      },
      resobjprop: {
        default: {}
      },
      valueClose: {
        type: Boolean,
        default: true
      }
    },
    data() {
      return {
       source: `this.getParentByType($department$).getAllChildrens().where(row.getAttributes().get($rolestr$).indexOf($${this.roleName}$) != -1))`,
      //  source: `root.getResourceById($${this.$login.f.orgid}$,$organization$).getSpecialResByType($user$).where(row.getAttributes().get($rolestr$).indexOf($${this.roleName}$) != -1)`,
        allUsers: [],
        users: [],
        selVal: '',
        organizationname:this.$login.f.organizationname,
        resids: [],
        changeval: ''
      }
    },
    watch: {
      'value'(val) {
        console.log("969696", val)
        if (val) {
          if (this.valueMultiple) {

          } else {
            this.selVal = val
          }

        }
      }
    },
    async ready() {
      await this.search()
      await this.filterUser(this.resids)
    },
    methods: {
      async search() {
        let http = new HttpResetClass()
        const res = await http.load('POST', '/rs/search', {
          data: {
            source: this.source,
            userid: this.$login.f.id
          }
        }, {resolveMsg: null, rejectMsg: null})
        this.allUsers = res.data
      },
      valuechange(val) {
        console.log(`val instanceof Array======${!val instanceof Array}`)
        if ((typeof val === 'object' && !val instanceof Array) || (val instanceof Event)) {
          return
        }
        if (this.valueMultiple && Array.isArray(val)) {
          if (val && val.length > 0) {
            let valuesele = `(`
            for (let i = 0; i < val.length; i++) {
              valuesele += `'${val[i]}',`
            }
            this.value = valuesele.substring(0, valuesele.length - 1) + ')'
          } else {
            this.value = ''
          }
        } else {
          this.value = val
        }
        // this.changeval = val
      },
      getRes(obj) {
        this.resids = obj.resids
        this.organizationname = obj.res[0]
        this.filterUser(this.resids)
        this.$dispatch('re-res', obj)
      },
      async filterUser(resids) {
        // 处理第一次进入页面值异常问题
        if (resids.length > 0 && typeof resids[0] == "object") {
          resids = resids[0]
        }
        if (resids[0]) {
          this.source = `root.getResourceById($${resids[0]}$,$organization$).getSpecialResByType($user$).where(row.getAttributes().get($rolestr$).indexOf($${this.roleName}$) != -1)`
          await this.search()
        }
        this.users = []
        this.allUsers.forEach(user => {
          if (this.organizationname == user.organizationname || !user.organizationname){
            this.users.push({label: user.name, value: user.name})
          }
        })
        if (!this.valueMultiple && this.users.length > 0) {
          this.users = [{label: '全部', value: ''}, ...this.users]
        }
        let temp = []
        this.users = this.users.filter(item => {
          if (!temp.includes(item.label)) {
            temp.push(item.label)
            return true
          }
          return false
        })
        this.$emit('alluser', this.allUsers)
        //tag
      }
    }
  }
</script>

<style scoped>

</style>
