<template>
  <div :class="style" v-show="opCompanyShow">
    <label class="font_normal_body">操作员<br/>公司</label>
    <res-select-new
      restype="organization"
      @res-select="getOpOrg"
      :is-mul="mul"
      :initresid="operatorInitres.org"
      v-ref:oporg>
    </res-select-new>
  </div>
  <div :class="style" v-show="opDepartmentShow">
    <label class="font_normal_body">操作员<br/>部门</label>
    <res-select
      restype="department"
      @res-select="getOpDep"
      :parentresid="opFirstDepresid"
      :initresid="operatorInitres.dep"
      :is-mul="mul"
      v-ref:opdepartment>
    </res-select>
  </div>
  <div :class="style" v-show="opOperatorShow && (!cascade)">
    <label class="font_normal_body">操作员<br/>人员</label>
    <res-select
      restype="user"
      @res-select="getOpUser"
      :parentresid="opFirstDepresid"
      :initresid="operatorInitres.user"
      :is-mul="mul"
      v-ref:opuser>
    </res-select>
  </div>
  <div :class="style" v-show="opOperatorShow && cascade">
    <label class="font_normal_body">操作员人员</label>
    <res-select
      restype="user"
      @res-select="getOpUser"
      :parentresid="opUserresid"
      :initresid="operatorInitres.user"
      :is-mul="mul"
      v-ref:opuser>
    </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>
  <div :class="style" v-show="userCompanyShow">
    <label class="font_normal_body">用户公司</label>
    <res-select-new
      restype="organization"
      @res-select="getorg"
      :is-mul="mul"
      :initresid="initres.org"
      v-ref:org>
    </res-select-new>
  </div>
  <div :class="style" v-show="userDepartmentShow">
    <label class="font_normal_body">用户部门</label>
    <res-select
      restype="department"
      @res-select="getdep"
      :parentresid="firstdepresid"
      :initresid="initres.dep"
      :is-mul="mul"
      v-ref:department>
    </res-select>
  </div>
  <div :class="style" v-show="userOperatorShow && (!cascade)">
    <label class="font_normal_body">用户人员</label>
    <res-select
      restype="user"
      @res-select="getuser"
      :parentresid="firstdepresid"
      :initresid="initres.user"
      :is-mul="mul"
      v-ref:user>
    </res-select>
  </div>
  <div :class="style" v-show="userOperatorShow && cascade">
    <label class="font_normal_body">用户人员</label>
    <res-select
      restype="user"
      @res-select="getuser"
      :parentresid="userresid"
      :initresid="initres.user"
      :is-mul="mul"
      v-ref:user>
    </res-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 () {
          return {
            org: [],
            dep: [],
            user: []
          }
        }
      },
      // 操作员初始值
      operatorInitres: {
        type: Object,
        default () {
          return {
            org: [],
            dep: [],
            user: []
          }
        }
      },
      // 控制显隐
      showComponent: {
        default () {
          return ['company', 'department', 'operator', 'slicearea', 'user_company', 'user_department', 'user_operator']
        }
      },
      // 人员是否和部门关联
      cascade: {
        type: Boolean,
        default: false
      },
      // 是否默认选中当前登录人
      selectDefault: {
        type: Boolean,
        default: false
      },
      selectin: {
        type: Boolean,
        default: true
      }
    },
    data () {
      return {
        // 操作员选择
        opFirstDepresid: [],
        opDepresid: [],
        opUserresid: [],
        opCompanyShow: false,
        opDepartmentShow: false,
        opOperatorShow: false,
        sliceareashow: false,
        sliceArea: [],
        slice_area: [],
        opObj: {
          orgnames: [],
          depnames: [],
          operatornames: []
        },
        // 用户选择
        firstdepresid: [],
        depresid: [],
        userresid: [],
        userCompanyShow: false,
        userDepartmentShow: false,
        userOperatorShow: false,
        obj: {
          orgnames: [],
          depnames: [],
          operatornames: []
        }
      }
    },
    ready () {
      if (this.selectDefault && this.selectin) {
        if (!this.initres.dep.length) {
          this.initres.dep = [this.$login.f.depids]
        }
        if (!this.initres.user.length) {
          this.initres.user = [this.$login.f.id]
        }
        if (!this.operatorInitres.org.length) {
          this.operatorInitres.org = [this.$login.f.orgid]
        }
        if (!this.operatorInitres.dep.length) {
          this.operatorInitres.dep = [this.$login.f.depids]
        }
        if (!this.operatorInitres.user.length) {
          this.operatorInitres.user = [this.$login.f.id]
        }
      }
      this.initComponent()
      if (this.operatorInitres.org.length > 0) {
        this.initAreas(this.operatorInitres.org)
      } else if (this.$login.f.orgid) {
        this.initAreas([this.$login.f.orgid])
      }
    },
    methods: {
      initComponent () {
        let self = this
        this.showComponent.find((item) => {
          switch (item) {
            case 'company': self.opCompanyShow = true; break
            case 'department': self.opDepartmentShow = true; break
            case 'operator': self.opOperatorShow = true; break
            case 'slicearea': self.sliceareashow = true; break
            case 'user_company': self.userCompanyShow = true; break
            case 'user_department': self.userDepartmentShow = true; break
            case 'user_operator': self.userOperatorShow = true; break
          }
        })
      },
      // 初始化片区
      async initAreas (val) {
        if (val && val.length > 0) {
          let http = new HttpResetClass()
          try {
            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
            })
            this.sliceArea = []
            this.slice_area = []
            arr.forEach((res) => {
              this.sliceArea.push({label: res.name, value: {name: res.name, code: res.number}})
            })
          } catch (error) {
            console.error('获取片区失败:', error)
            this.sliceArea = []
            this.slice_area = []
          }
        }
      },
      // 操作员条件
      returnOp (ids, reset = false) {
        let condition = ''
        if (!reset && (this.opDepresid.length || this.opUserresid.length || (ids && ids.length) || this.slice_area.length > 0)) {
          if (this.opDepresid.length > 0) {
            condition = ' and f_orgid in ' + plugin.convertToIn(this.opDepresid)
          }
          if (this.opUserresid.length > 0) {
            condition += ' and f_depid in ' + plugin.convertToIn(this.opUserresid)
          }
          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.opObj)
      },
      // 用户条件
      returnOrg (ids, reset = false) {
        let condition = ''
        if (!reset && (this.depresid.length || this.userresid.length || (ids && ids.length))) {
          if (this.depresid.length > 0) {
            condition = ' and f_us_orgid in ' + plugin.convertToIn(this.depresid)
          }
          if (this.userresid.length > 0) {
            condition += ' and f_us_depid in ' + plugin.convertToIn(this.userresid)
          }
          if (ids && ids.length > 0) {
            condition += ' and f_us_operatorid in ' + plugin.convertToIn(ids)
          }
        }
        this.$dispatch('re-us-res', condition, this.obj)
      },
      // 操作员选择
      getOpOrg (obj, val) {
        if (this.opFirstDepresid.length <= 0) {
          this.opFirstDepresid = JSON.parse(JSON.stringify(obj))
        }
        this.opDepresid = obj
        this.opObj.orgnames = val
        this.initAreas(obj)
        this.returnOp()
      },
      getOpDep (obj, val) {
        this.opObj.depnames = val
        this.opUserresid = obj
        this.returnOp()
      },
      getOpUser (obj, val) {
        this.opObj.operatornames = val
        this.returnOp(obj)
      },
      // 用户选择
      getorg (obj, val) {
        if (this.firstdepresid.length <= 0) {
          this.firstdepresid = JSON.parse(JSON.stringify(obj))
        }
        this.depresid = obj
        this.obj.orgnames = val
        this.returnOrg()
      },
      getdep (obj, val) {
        this.obj.depnames = val
        this.userresid = obj
        this.returnOrg()
      },
      getuser (obj, val) {
        this.obj.operatornames = val
        this.returnOrg(obj)
      },
      getarea (val) {
        this.slice_area = val
        if (this.opObj.operatornames && this.opObj.operatornames.length > 0) {
          this.returnOp(this.opObj.operatornames)
        } else {
          this.returnOp()
        }
      },
      resetAll () {
        // 操作员
        this.opDepresid = []
        this.opUserresid = []
        this.opObj = {orgnames: [], depnames: [], operatornames: []}
        if (this.$refs.oporg && this.$refs.oporg.selectres !== undefined) {
          this.$refs.oporg.selectres = []
        }
        if (this.$refs.opdepartment) {
          this.$refs.opdepartment.selectres = []
          if (this.$refs.opdepartment.$children && this.$refs.opdepartment.$children[0]) {
            this.$refs.opdepartment.$children[0].isSelectAll = false
          }
        }
        if (this.$refs.opuser) {
          this.$refs.opuser.selectres = []
          if (this.$refs.opuser.$children && this.$refs.opuser.$children[0]) {
            this.$refs.opuser.$children[0].isSelectAll = false
          }
        }
        if (this.$refs.slice) {
          this.slice_area = []
        }
        this.returnOp([], true)
        // 用户
        this.depresid = []
        this.userresid = []
        this.obj = {orgnames: [], depnames: [], operatornames: []}
        if (this.$refs.org && this.$refs.org.selectres !== undefined) {
          this.$refs.org.selectres = []
        }
        if (this.$refs.department) {
          this.$refs.department.selectres = []
          if (this.$refs.department.$children && this.$refs.department.$children[0]) {
            this.$refs.department.$children[0].isSelectAll = false
          }
        }
        if (this.$refs.user) {
          this.$refs.user.selectres = []
          if (this.$refs.user.$children && this.$refs.user.$children[0]) {
            this.$refs.user.$children[0].isSelectAll = false
          }
        }
        this.returnOrg([], true)
      }
    }
  }
</script>

