<template>
  <!-- 部门选择弹层 -->
  <SuperTableSelectorDialog
    v-on="$listeners"
    v-bind="$attrs"
    title="部门选择"
    select-title-name="已选部门"
    :treeTabList="['行政维度']"
    :isShow.sync="isShowDialog"
    hintLanguage="提示：请在列表中选择部门后，点击确认。"
    :search-option="optionList"
    :treeData="orgData"
    :tree-item="treeItem"
    :defaultTreeExpandLevel="0"
    :table-data="tableData"
    :table-options="tableOptions"
    :table-loading="tableLoading"
    :tableParams="params"
    @open="open"
    @closed="closed"
    @tableRefresh="getTableData"
    @treeChange="treeChange"
    ref="superTableSelectorDialog"
  />
</template>

<script>
import {getOrgPage, treeDataByDem} from '@/api/systemInterface'
import {refineRequestBody, setQueryValue} from '@/utils/requestWrappers'
import SuperTableSelectorDialog from '@/components/SuperTableSelectorDialog/index.vue'

export default {
  name: 'DeptSelectorDialog',
  components: {SuperTableSelectorDialog},
  props: {
    isShow: {
      default: false,
      type: Boolean,
    },
  },
  data() {
    return {
      orgData: [], // 组织树
      optionList: [
        {title: '部门名称', prop: 'name', type: 'input', default: true},
        {title: '部门编码', prop: 'code', type: 'input', default: false},
      ],
      tableOptions: [
        {label: '部门名称', minWidth: '80', prop: 'name'},
        {label: '部门编码', minWidth: '60', prop: 'code'},
        {label: '组织路径', minWidth: '100', prop: 'pathName'},
      ],
      params: {
        pageBean: {pageSize: 20, page: 1, total: 0},
        querys: [
          {operation: 'RIGHT_LIKE', property: 'path', value: ''},
          {operation: 'LIKE', property: 'name', value: ''},
          {operation: 'LIKE', property: 'code', value: ''},
        ],
        sorter: [{direction: 'DESC', property: 'updateTime'}],
      },
      tableData: [], // 部门数据
      treeItem: {}, // 树选中
      tableLoading: false, // 表格loading
    }
  },
  computed: {
    isShowDialog: {
      get() {
        return this.isShow
      },
      set(val) {
        this.$emit('update:isShow', val)
      },
    },
  },
  created() {
    this.getOrgTreeData()
  },
  methods: {
    // 打开弹层时触发
    open() {
      this.getTableData()
    },

    // 关闭弹层触发
    closed() {
      const {orgData} = this
      // 重置data中的数据
      Object.assign(this.$data, this.$options.data.call(this), {orgData})
      this.treeItem = this.orgData[0] || {}
      setQueryValue(this.params, 'path', this.treeItem?.path || '')
    },

    // 获取公司树
    async getOrgTreeData() {
      this.orgData = [
        {
          name: '中国铁建昆仑投资集团',
          id: '',
          path: '',
          children: await treeDataByDem(),
        },
      ]
      this.treeItem = this.orgData[0] || {}
      setQueryValue(this.params, 'path', this.treeItem?.path || '')
    },

    //获取部门列表数据
    async getTableData() {
      this.tableData = []
      this.tableLoading = true
      const {total = 0, rows = []} = await getOrgPage(
        refineRequestBody(this.params)
      )
      this.params.pageBean.total = total
      this.tableData = rows
      this.tableLoading = false
    },

    // 树改变
    treeChange(treeItem) {
      // 修改对应的字段值
      setQueryValue(this.params, 'path', treeItem?.path || '')
      this.getTableData()
    },
  },
}
</script>

<style lang="scss" scoped>
</style>
