<template>
  <PageWrapper>
    <el-card :bordered="false" class="mb-3 proCard">
      <el-space align="center">
        <el-input
          :style="{ width: '320px' }"
          v-model="params.keyword"
          clearable
          placeholder="请输入用户名称"
          @keyup.enter="reloadTable"
        />
        <el-button type="primary" @click="reloadTable">
          <template #icon>
            <el-icon>
              <SearchOutlined />
            </el-icon>
          </template>
          查询
        </el-button>
      </el-space>
    </el-card>
    <el-card :bordered="false" class="proCard">
      <BasicTable
        :columns="columns"
        :request="loadDataTable"
        :row-key="(row) => row.userId"
        ref="basicTableRef"
        :actionColumn="actionColumn"
        virtual-scroll
      />
    </el-card>
  </PageWrapper>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { ElMessage } from 'element-plus';
  import { BasicTable, TableAction, BasicColumn } from '@/components/Table';
  import { onlineList, onlineLogOut } from '@/api/system/user';
  import { SearchOutlined } from '@vicons/antd';
  import { columns } from './columns';

  const message = ElMessage;
  const basicTableRef = ref();
  const tableData = ref();

  const params = reactive({
    keyword: '',
  });

  const actionColumn: BasicColumn = reactive({
    width: 100,
    title: '操作',
    prop: 'action',
    fixed: 'right',
    align: 'center',
    render(record) {
      return h(TableAction as any, {
        actions: [
          {
            label: '强退',
            type: 'danger',
            isConfirm: true,
            popConfirm: {
              onConfirm: handleLogOut.bind(null, record.row),
              title: '您确定要强退吗？',
              confirmButtonText: '确定',
              cancelButtonText: '取消',
            },
          },
        ],
      });
    },
  });

  const loadDataTable = async (res) => {
    const result = await onlineList({ ...params, ...res });
    tableData.value = result.list;
    return result;
  };

  function reloadTable() {
    basicTableRef.value.reload();
  }

  function handleLogOut(row) {
    onlineLogOut({ userId: row.userId }).then(() => {
      message.success('强退成功');
      reloadTable();
    });
  }
</script>

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