<template>
  <div :class="style" v-show="companyshow">
    <label class="font_normal_body">公&emsp;&emsp;司</label>
    <right-tree @re-res="getorg"
                :initresid='initres.org'></right-tree>
  </div>
  <div :class="style" v-show="departmentshow">
    <label class="font_normal_body">部&emsp;&emsp;门</label>
    <res-select restype='department'
                @res-select="getdep"
                :parentresid="depresid"
                :initresid='initres.dep'
                :is-mul="mul">
    </res-select>
  </div>
  <div :class="style" v-show="operatorshow">
    <label class="font_normal_body">人&emsp;&emsp;员</label>
    <res-select restype='user'
                @res-select="getuser"
                :parentresid="depresid"
                :initresid='initres.user'
                :is-mul="mul">
    </res-select>
  </div>
  <div :class="style " v-show="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']
      }
    },
    data () {
      return {
        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) => {
            return res.parentid == val
          })
          console.log('过滤之后的片区', arr)
          this.sliceArea = []
          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) {
        console.log("选中公司的数据11",obj.resids)
        this.depresid=obj.resids
        this.obj.orgnames = obj.res
        this.initAreas(obj.resids)
        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>
