<template>
  <PageWrapper :sidebarWidth="260">
    <template #sidebar>
      <DepartLeftTree ref="leftTree" @select="onTreeSelect" />
    </template>
    <BasicTable @register="registerTable">
      <template #post="{ text }">
        {{
          (text || '')
            .split(',')
            .map((t) => (positionInfo[t] ? positionInfo[t] : t))
            .join(',')
        }}
      </template>
    </BasicTable>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { provide, ref } from 'vue';
  import { PageWrapper, useListPage, ColEx, BasicTable } from '@jdlinker/ui';
  import { useDesign } from '@jdlinker/func';
  import DepartLeftTree from './components/DepartLeftTree.vue';
  import { columns, searchFormSchema } from './address.data';
  import { list, positionList } from './address.api';

  // 自适应列配置
  const adaptiveColProps: Partial<ColEx> = {
    xs: 24, // <576px
    sm: 24, // ≥576px
    md: 24, // ≥768px
    lg: 12, // ≥992px
    xl: 8, // ≥1200px
    xxl: 8 // ≥1600px
  };

  const { prefixCls } = useDesign('address-list');
  provide('prefixCls', prefixCls);

  // 给子组件定义一个ref变量
  const leftTree = ref();

  // 当前选中的部门code
  const orgCode = ref('');
  const positionInfo = ref({});

  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    tableProps: {
      api: list,
      columns,
      rowKey: 'userId',
      showIndexColumn: true,
      formConfig: {
        schemas: searchFormSchema,
        baseColProps: adaptiveColProps
      },
      actionColumn: null,
      showTableSetting: false,
      // 请求之前对参数做处理
      beforeFetch(params) {
        params.orgCode = orgCode.value;
      }
    }
  });
  //注册table数据
  const [registerTable, { reload }] = tableContext;

  // 左侧树选择后触发
  function onTreeSelect(data) {
    orgCode.value = data.orgCode;
    reload();
  }

  // 查询职务信息
  async function queryPositionInfo() {
    const result = await positionList({ pageSize: 99999 });
    if (result) {
      let obj = {};
      result.records.forEach((position) => {
        obj[position['code']] = position['name'];
      });
      positionInfo.value = obj;
    }
  }
  queryPositionInfo();
</script>

<style lang="less" scoped>
  @import './index.less';
</style>
