<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, orgTree} from '@/api/systemInterface'
import {refineRequestBody, setQueryValue} from '@/utils/requestWrappers'
import SuperTableSelectorDialog from '@/components/SuperTableSelectorDialog/index.vue'

export default {
  name: 'OgnSelectorDialog',
  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 = await orgTree()
      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>
