<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 valArr = Array.isArray(val) ? val : [val]
        let arr = getAllArea.data.filter((res) => {
          return valArr.includes(res.parentid)
        })
        this.sliceArea = []
        this.slice_area = []
        arr.forEach((res) => {
          this.sliceArea.push({label: res.name, value: {name: res.name, code:res.number}})
        })
      }
    },
    returnOrg(ids){
      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(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)
    },
    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>
