<template>
    <div style="width: 100%;height: 100%">
        <div class="col-sm-6">
            <label class="font_normal_body">&nbsp;公&nbsp;&nbsp;&nbsp;&nbsp;司&nbsp;</label>
            <right-tree :width="leftWidth" @re-res="getRes"></right-tree>
        </div>
        <div class="col-sm-6 ">
            <label class="font_normal_body">{{roleLable}}</label>
            <v-select :value.sync="value" v-model='model.f_checker_id'
                      :value-single="true"
                      class="select_list select"
                      :options='users' :placeholder='roleName'
                      close-on-select
                      :width="rightWidth">
            </v-select>
        </div>
    </div>
</template>

<script>
  export default {
    name: "RoleSelector",
    props: {
      roleName: {
        type: String,
        default: '安检员'
      },
      roleLable: {
        type: String,
        default: '安检员'
      },
      leftWidth: {
        type: String,
        default: '60%'
      },
      rightWidth: {
        type: String,
        default: '60%'
      },
      value: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        source: `tool.getChildrenOfResName($${this.roleName}$)`,
        allUsers: [],
        users: [],
        resids: []
      }
    },
    ready() {
      this.$resetpost('/rs/search', {data: {source: this.source, userid: this.$login.f.id}}, {resolveMsg: null, rejectMsg: null})
        .then(res => {
          console.log('res-->' + JSON.stringify(res))
          this.allUsers = res.data
        })
    },
    methods: {
      getRes(obj) {
        this.resids = obj.resids
        console.log(this.resids)
        this.filterUser(this.resids)
        this.$dispatch('re-res',obj)
      },
      filterUser(resids) {
        // 处理第一次进入页面值异常问题 
        if(resids.length > 0 && typeof resids[0] == "object"){
          resids = resids[0]
        }
        this.users = []
        this.allUsers.forEach(user => {
          if (resids.indexOf(user.orgid) != -1) {
            this.users.push({label: user.name, value: user.name})
          }
        })
        if(this.users.length > 0){
          this.users = [{label: '全部', value: ''},...this.users]
        }
        console.log(this.users)
      }
    }
  }
</script>

<style scoped>

</style>