<template>
  <div :class="style" v-show="companyshow">
    <label class="font_normal_body">公&emsp;&emsp;司</label>
    <res-select-new restype='organization'
                @res-select="getorg"
                :is-mul="mul"
                :initresid='initres.org'></res-select-new>
  </div>
  <div :class="style" v-show="departmentshow">
    <label class="font_normal_body">部&emsp;&emsp;门</label>
    <res-select restype='department'
                @res-select="getdep"
                :parentresid="firstdepresid"
                :initresid='initres.dep'
                :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="firstdepresid"
                :initresid='initres.user'
                :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'
                :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,
      },
      showComponent:{
        default:['company','department','operator','slicearea']
      },
      //人员是否和部门关联
      cascade: {
        type: Boolean,
        default: false
      }
    },
    data () {
      return {
        // 首次默认选择的公司
        firstdepresid: [],
        orgresid:[this.$login.f.orgid],
        depresid:[],
        userresid:[],
        sliceArea: [],
        slice_area:[],
        companyshow:false,
        departmentshow:false,
        operatorshow:false,
        sliceareashow:false,
        obj:{
          orgnames:[],
          depnames:[],
          operatornames:[]
        }
      }
    },
    ready () {
      console.log('开始获取组织结构。。。')
      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) => {
            if(Array.isArray(val)){
              for (let i = 0; i <val.length ; i++) {
                if(res.parentid == val[i]){
                  return true
                }
              }
            }
            return res.parentid == val
          })
          console.log('过滤之后的片区', arr)
          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(this.userresid.length > 0){
          condition += " and f_depid in " + plugin.convertToIn(this.userresid);
        }
        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,val) {
        console.log("选中公司的数据11",obj)
        if (this.firstdepresid.length <= 0) {
          this.firstdepresid = JSON.parse(JSON.stringify(obj))
        }
        this.depresid=obj
        this.obj.orgnames = val
        this.initAreas(obj)
        this.returnOrg();
      },
      getdep(obj,val) {
        console.log("选中部门的数据22",obj)
        this.obj.depnames = val
        this.userresid=obj
        this.returnOrg();
      },
      getuser(obj) {
        this.obj.operatornames = obj
        console.log("选中用户的数据33",obj)
        this.returnOrg(obj);
      },
      getarea(val) {
        this.slice_area=val
        console.log("选中的大区",val)
        if(this.obj.operatornames){
          this.returnOrg(this.obj.operatornames);
        }else{
          this.returnOrg();
        }
      }
    },
    watch: {

    }
  }
</script>
