<template>
  <BasicTable @register="registerTable">
    <template #action="{ record }">
      <TableAction :actions="getTableAction(record)" />
    </template>
  </BasicTable>
</template>

<script lang="ts" name="online-user" setup>
  import { BasicTable, TableAction, useListPage } from '@jdlinker/ui';
  import { columns, searchFormSchema } from './OnlineUser.data';
  import { list, forceLogout } from './OnlineUser.api';
  import { useMessage } from '@jdlinker/func';

  const { createMessage } = useMessage();
  // 列表页面公共参数、方法
  const { tableContext } = useListPage({
    designScope: 'online-user',
    tableProps: {
      title: '在线用户',
      api: list,
      // @ts-ignore
      columns,
      formConfig: {
        schemas: searchFormSchema
      }
    }
  });
  const [registerTable, { reload }] = tableContext;

  //操作栏
  function getTableAction(record) {
    return [
      {
        label: '强退',
        popConfirm: {
          title: '强制退出用户？',
          confirm: handleForce.bind(null, record)
        }
      }
    ];
  }

  /**
   * 强退
   * @param record
   */
  function handleForce(record) {
    forceLogout({ token: record.token }).then((res) => {
      if (res.success) {
        reload();
        createMessage.success('强制退出用户”' + record.realname + '“成功！');
      } else {
        createMessage.warn(res.message);
      }
    });
  }
</script>
