<template>
  <a-row :class="['p-4', `${prefixCls}--box`]" type="flex" :gutter="10" style="max-height: 800px">
    <a-col :xl="6" :lg="24" :md="24" style="margin-bottom: 10px">
      <DepartLeftTree ref="leftTree" @select="onTreeSelect" />
    </a-col>
    <a-col :xl="18" :lg="24" :md="24" style="margin-bottom: 10px">
      <div style="height: 100%" class="address-book">
        <!--引用表格-->
        <BasicTable @register="registerTable">
          <template #post="{ text }">
            {{
              (text || '')
                .split(',')
                .map((t) => (positionInfo[t] ? positionInfo[t] : t))
                .join(',')
            }}
          </template>
          <template #action="{ record }">
            <TableAction :actions="getTableAction(record)" />
          </template>
        </BasicTable>
      </div>
    </a-col>
  </a-row>
</template>

<script lang="ts" setup>
  import { provide, ref, unref } from 'vue';
  import { useDesign } from '/@/hooks/web/useDesign';
  import DepartLeftTree from './components/DepartLeftTree.vue';
  import { BasicTable, TableAction } from '/@/components/Table';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { columns, searchFormSchema } from './address.data';
  import { list, positionList } from './address.api';
  import { getCacheByDynKey } from '@/utils/auth';
  import { JEECG_CHAT_UID } from '@/enums/cacheEnum';

  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,
      // 代码逻辑说明: [VUEN-1485]进入系统管理--通讯录页面后，网页命令行报错------------
      rowKey: 'id',
      showIndexColumn: true,
      formConfig: {
        schemas: searchFormSchema,
      },
      canResize: false,
      showTableSetting: false,
      actionColumn: {
        width: 80,
      },
      // 请求之前对参数做处理
      beforeFetch(params) {
        params.orgCode = orgCode.value;
      },
    },
  });
  //注册table数据
  const [registerTable, { reload }] = tableContext;

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

  /**
   * 操作栏
   *
   * @param record
   */
  function getTableAction(record) {
    return [
      {
        label: '发消息',
        onClick: handleSendChat.bind(null, record),
      },
    ];
  }

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

  /**
   * 聊天
   *
   * @param record
   */
  function handleSendChat(record) {
    //获取messageId
    let cacheByDynKey = getCacheByDynKey(JEECG_CHAT_UID);
    let iframes: any = document.getElementById('jChatOnline');
    let id = record.id;
    //发送打开聊天窗口的请求
    iframes.contentWindow.postMessage(
      {
        type: 'open-chat',
        messageId: cacheByDynKey,
        data: { id: id, type: 'friend', groupName: '', avatar: record.avatar, username: record.realname },
      },
      '*'
    );
  }
</script>

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