<template>
  <ht-user-dialog
    :single="single"
    :data="data"
    :table-columns="tableColumns"
    :pagination="pagination"
    :demensions="demensions"
    :default-demension="defaultDemension"
    :orgs="orgs"
    :load-org-tree="loadOrgTree"
    :append-to-body="appendToBody"
    :destroy-on-close="destroyOnClose"
       :queryUserWithMainOrgPost="queryUserWithMainOrgPost"
    @load="handleLoad"
    @changeDemension="changeDemension"
    @loadOrgUser="loadOrgUser"
    @onConfirm="onConfirm"
    @reset="reset"
    quick-search-props="fullname,account"
    select-label="fullname"
    ref="htUserDialog"
  />
</template>
<script>
import utils from '@/hotent-ui-util.js'

export default {
  name: 'eip-user-dialog',
  props: {
    value: Array,
    name: String,
    single: Boolean,
    appendToBody: {
      type: Boolean,
      default: false
    },
    destroyOnClose: {
      type: Boolean,
      default: false
    },
    queryUserWithMainOrgPost: {
      type: Boolean,
      default: false
    }
    
  },
  data() {
    return {
      data: [],
      demensions: [],
      defaultDemension: null,
      orgs: [],
      tableColumns: [
        {prop: 'fullname', label: '名称', width: '120'},
        {prop: 'account', label: '账号', width: '120'},
        {prop: 'orgname', label: '主组织', width: '160', tipField: 'pathname'},
        {prop: 'postname', label: '岗位', width: '160'}
      ],
      pagination: {
        page: 1,
        pageSize: 50,
        total: 0,
        showTotal: true
      },
      curCheckedOrgPath: ''
    }
  },
  mounted() {},
  methods: {
    showDialog(selectors) {
      this.$http
        .get('${uc}/api/demension/v1/dems/getAll')
        .then(rep => {
          let data = rep.data
          this.demensions = data
          data.forEach(element => {
            if (element.isDefault == 1) {
              this.defaultDemension = element.id
            }
          })
          if (!this.defaultDemension) {
            this.defaultDemension = data[0].id
          }
          this.$refs.htUserDialog.showDialog(selectors)
        })
        .catch(function(error) {})
    },
    reset(org) {
      // 根据组织获取用户
      this.curCheckedOrgPath = ''
    },
    handleLoad(param, cb) {
      let queryFilter = param
      queryFilter.querys = queryFilter.querys || []
      // if (queryFilter.querys.length > 0 && queryFilter.pageBean) {
      //   queryFilter.pageBean.page = 1;
      // }
      if (this.curCheckedOrgPath) {
        queryFilter.pageBean.page = 1
        let query = {
          property: 'path_',
          value: this.curCheckedOrgPath,
          group: 'orgQuery',
          operation: 'RIGHT_LIKE',
          relation: 'AND'
        }
        queryFilter.querys.push(query)
      }

      this.$http
        .post('${uc}/api/user/v1/users/queryUserWithMainOrgPost', queryFilter)
        .then(rep => {
          let data = rep.data
          this.data = data.rows
          this.pagination.page = data.page
          this.pagination.pageSize = data.pageSize
          this.pagination.total = data.total
        })
        .finally(() => {
          cb()
        })
    },
    loadOrgTree(node, resolve) {
      if (node && node.data && node.data.isParent) {
        if (node.data.children) {
          resolve(node.data.children)
        } else {
          this.$http
            .post('${uc}/api/org/v1/orgs/getByParentAndDem', {
              demId: node.data.demId,
              parentId: node.data.id
            })
            .then(rep => {
              resolve(rep.data)
            })
            .catch(function(error) {})
        }
      } else {
        resolve([])
      }
    },
    changeDemension(currentDemensionId) {
      this.$http
        .post('${uc}/api/org/v1/orgs/getByParentAndDem', {
          demId: currentDemensionId
        })
        .then(rep => {
          this.orgs = utils.tile2nest(rep.data)
        })
        .catch(function(error) {})
    },
    loadOrgUser(org) {
      // 根据组织获取用户
      this.curCheckedOrgPath = org.path
      if (this.$refs.htUserDialog && this.$refs.htUserDialog.$refs.selector) {
        this.$refs.htUserDialog.$refs.selector.load()
      }
    },
    onConfirm(selection) {
      this.$emit('onConfirm', selection, this.name)
      this.$emit('input', selection)
    }
  }
}
</script>
