<template>
  <PageWrapper>
    <el-card :bordered="false" class="pt-3 mb-3 proCard">
      <BasicForm @register="register" @submit="handleSubmit" @reset="handleReset"></BasicForm>
    </el-card>
    <el-card :bordered="false" class="proCard">
      <el-table empty-text="暂无数据" border
      :height="fwbHeight"
        :data="onlineTableData.slice((pager.page - 1) * pager.size, pager.page * pager.size)" row-key="detailId">
        <el-table-column type="index"width="80" align="center" label="序号"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="会话编号" prop="tokenId"min-width="100"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="登录名称" prop="username"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="部门名称" prop="deptName"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="IP地址" prop="ipAddr"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="登录地点" prop="loginLocation"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="浏览器" prop="browser"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="操作系统" prop="os"></el-table-column>
        <el-table-column show-overflow-tooltip align="center" label="登录时间" prop="loginTime"></el-table-column>
        <el-table-column align="center" label="操作" width="100" fixed="right" v-perm="['sys:online:logout']">
          <template #default="{ row, $index }">
            <el-button @click="handleDelete(row)" type="danger">强退</el-button>
          </template>
        </el-table-column>
      </el-table>
      <pagination style="justify-content: flex-end" class="mt-10 flex" v-model="pager" />
    </el-card>
  </PageWrapper>
</template>

<script lang="ts" setup>
import { reactive, ref, onMounted } from 'vue';
import { ColProps } from 'element-plus';
import { schemas } from './querySchemas';
import { useForm } from '@/components/Form/index';
import { getOnlineList, onlineOut } from '@/api/monitor/online';
import { message, confirm } from "@/utils/auth";
const onlineTableData = ref([])
const formParams = reactive({
  ipAddr: '',
  username: ''
});
const pager = ref({
  page: 1,
  size: 10,
  count: onlineTableData.value.length
});
const fwbHeight = document.body.clientHeight - 370
const loadTable = async () => {
  onlineTableData.value = await getOnlineList({ ...formParams });
  pager.value.count = onlineTableData.value.length;
}
const [register, { }] = useForm({
  labelWidth: 80,
  layout: 'horizontal',
  colProps: { span: 6 } as ColProps,
  submitOnReset: true,
  schemas
});
function handleSubmit(values: Recordable) {
  handleReset()
  for (const key in values) {
    formParams[key] = values[key]
  }
  loadTable();
}

function handleReset() {
  for (const key in formParams) {
    formParams[key] = '';
  }
  pager.value.page = 1;
}

async function handleDelete(row) {
  await confirm('确定要强退？');
  await onlineOut(row.tokenId)
  message("强退成功");
  loadTable()
}
onMounted(() => {
  loadTable()
})
</script>

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