<template>
  <div :class="style" v-show="companyshow">
    <label class="font_normal_body">公&emsp;&emsp;司</label>
    <right-tree @re-res="getorg"
                :mustselect="mustselect"
                :initresid='initres.org'></right-tree>
  </div>
  <div :class="style" v-show="departmentshow">
    <label class="font_normal_body" v-html="deptitle"></label>
    <res-select restype='department' v-ref:department
                @res-select="getdep"
                :parentresid="depresid"
                :specialquery="specialquery"
                :initresid='initres.dep'
                :mustselect="mustselect"
                :is-mul="mul">
    </res-select>
  </div>
  <div :class="style" v-show="operatorshow && (!cascade)">
    <label class="font_normal_body">人&emsp;&emsp;员</label>
    <res-select restype='user'
                @res-select="getuser"
                :parentresid="depresid"
                :initresid='initres.user'
                :mustselect="mustselect"
                :is-mul="mul">
    </res-select>
  </div>
  <div :class="style" v-show="operatorshow && cascade">
    <label class="font_normal_body">人&emsp;&emsp;员</label>
    <res-select restype='user'
                @res-select="getuser"
                :parentresid="userresid"
                :initresid='initres.user'
                :mustselect="mustselect"
                :is-mul="mul">
    </res-select>
  </div>
  <div :class="style " v-if="sliceareashow" >
    <label class="font_normal_body">片&emsp;&emsp;区</label>
    <v-select :value.sync="slice_area" v-model="slice_area"  @change='getarea'
              :options='sliceArea' placeholder='片区/管理站' filer-key="name"
              close-on-select v-ref:slice>
    </v-select>
  </div>
</template>
<script>
import plugin from '../../plugins/GetLoginInfoService'
import { HttpResetClass } from 'vue-client'
export default {
  title: '资源选择测试',
  props: {
    style: {
      type: String,
      default: 'col-sm-2 form-group'
    },
    mul: {
      type: Boolean,
      default: true
    },
    //初始值
    initres: {
      type: Object,
      default: null,
    },
    //是否只查询营业厅
    specialquery: {
      type: Boolean,
      default: false
    },
    showComponent:{
      default:['company','department','operator','slicearea']
    },
    //人员是否和部门关联
    cascade: {
      type: Boolean,
      default: true
    },
    selectin: {
      type: Boolean,
      default: true
    }
  },
  data () {
    return {
      orgresid:[this.$login.f.orgid],
      depresid:[],
      userresid:[],
      operatorsid: [],
      sliceArea: [],
      slice_area:[],
      companyshow:false,
      departmentshow:false,
      operatorshow:false,
      sliceareashow:false,
      mustselect:false,
      deptitle: this.specialquery ? '营&ensp;业&ensp;厅' : '部&emsp;&emsp;门',
      obj:{
        orgnames:[],
        depnames:[],
        operatornames:[]
      }
    }
  },
  ready () {
    if(this.$login.r.includes('部门默认选中')&& this.selectin){
      this.initres.dep = [this.$login.f.depids]
    }
    if(this.$login.r.includes('人员默认选中')&& this.selectin){
      this.initres.user = [this.$login.f.id]
      if(this.$login.r.includes('人员强制选中')){
        this.mustselect = true
      }
    }
    this.initComponent();
    this.initAreas(this.$login.f.orgid)
  },
  methods:{
    initComponent(){
      let self=this;
      this.showComponent.find((item)=>{
        switch(item){
          case 'company': self.companyshow=true; break;
          case 'department': self.departmentshow=true; break;
          case 'operator': self.operatorshow=true; break;
          case 'slicearea': self.sliceareashow=true; break;
        }
      })
    },
    // 初始化片区
    async initAreas (val) {
      if (val) {
        let http = new HttpResetClass()
        let getAllArea = await http.load('POST', '/rs/search', {
          source: 'this.getParentByType($organization$).getAllChildrens().where(row.getType() == $zone$)',
          userid: this.$login.f.id
        }, {resolveMsg: null, rejectMsg: '获取片区出错！！！'})
        let arr = getAllArea.data.filter((res) => {
          return res.parentid == val
        })
        this.sliceArea = []
        this.slice_area = []
        arr.forEach((res) => {
          this.sliceArea.push({label: res.name, value: {name: res.name, code:res.number}})
        })
      }
    },
    returnOrg(ids){
      let limit = this.$login.r.includes('数据授权限定')
      let deplimit = this.$login.r.includes('部门筛选')

      let condition;
      if(this.depresid.length > 0)
        condition = " and f_orgid in " + plugin.convertToIn(this.depresid);
      else
        condition = " and f_orgid = " + this.$login.f.orgid;
      if(this.userresid.length > 0){
        condition += " and f_depid in " + plugin.convertToIn(this.userresid);
      } else {
        if (limit) {
          let depids = []
          for (let row of this.$refs.department.resoptions) {
            depids.push(row.value)
          }
          let depid = depids.length ? plugin.convertToIn(depids) : ('')
          condition += " and f_depid in " + depid;
        }
      }
      if(ids && ids.length > 0){
        condition += " and f_operatorid in " + plugin.convertToIn(ids);
      }
      if(this.slice_area.length>0){
        condition += " and f_zones = '" + this.slice_area[0].name+"'"
      }
      this.$dispatch('re-res',condition,this.obj)
      // 只显示自身部门的人员
      if (deplimit) {
        if (this.$refs.department.resoptions.length>0) {
          let resoptionsDepar = []
          this.$refs.department.resoptions.forEach(item => {
            if (item.value === this.$login.f.depids) {
              resoptionsDepar.push(item)
            }
          })
          this.$refs.department.resoptions = resoptionsDepar
        }
      }
    },
    getorg (obj) {
      this.depresid=obj.resids
      this.obj.orgnames = obj.res
      this.initAreas(obj.resids)
      this.returnOrg();
    },
    getdep(obj,val) {
      this.obj.depnames = val
      this.userresid=obj
      this.returnOrg();
    },
    getuser(obj,val) {
      this.obj.operatornames = val
      this.operatorsid = obj
      this.returnOrg(obj);
    },
    getarea(val) {
      this.slice_area=val
      if(this.operatorsid){
        this.returnOrg(this.operatorsid);
      }else{
        this.returnOrg();
      }
    }
  },
  watch: {

  }
}
</script>
