<template>
  <div class="p-2">
    <div class="mb-[10px]">
      <el-card shadow="hover">
        <el-form ref="queryFormRef" :model="queryParams" :inline="true" label-width="140px">
          <el-form-item :label="t('online.search.ipaddr')" prop="ipaddr">
            <el-input v-model="queryParams.ipaddr" :placeholder="t('online.search.ipaddrPlaceholder')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item :label="t('online.search.userName')" prop="userName">
            <el-input v-model="queryParams.userName" :placeholder="t('online.search.userNamePlaceholder')" clearable @keyup.enter="handleQuery" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="Search" @click="handleQuery">{{ t('online.search.search') }}</el-button>
            <el-button icon="Refresh" @click="resetQuery">{{ t('online.search.reset') }}</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
    <el-card shadow="hover">
      <el-table
        v-loading="loading"
        border
        :data="onlineList.slice((queryParams.pageNum - 1) * queryParams.pageSize, queryParams.pageNum * queryParams.pageSize)"
        style="width: 100%"
      >
        <el-table-column :label="t('online.table.index')" width="80" type="index" align="center">
          <template #default="scope">
            <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="t('online.table.tokenId')" align="center" prop="tokenId" :show-overflow-tooltip="true" width="180" />
        <el-table-column :label="t('online.table.userName')" align="center" prop="userName" :show-overflow-tooltip="true" width="150" />
        <el-table-column :label="t('online.table.clientKey')" align="center" prop="clientKey" :show-overflow-tooltip="true" width="150" />
        <el-table-column :label="t('online.table.deviceType')" align="center" width="150">
          <template #default="scope">
            <dict-tag :options="sys_device_type" :value="scope.row.deviceType" />
          </template>
        </el-table-column>
        <el-table-column :label="t('online.table.deptName')" align="center" prop="deptName" :show-overflow-tooltip="true" width="180" />
        <el-table-column :label="t('online.table.ipaddr')" align="center" prop="ipaddr" :show-overflow-tooltip="true" width="150" />
        <el-table-column :label="t('online.table.loginLocation')" align="center" prop="loginLocation" :show-overflow-tooltip="true" width="180" />
        <el-table-column :label="t('online.table.os')" align="center" prop="os" :show-overflow-tooltip="true" width="180" />
        <el-table-column :label="t('online.table.browser')" align="center" prop="browser" :show-overflow-tooltip="true" width="150" />
        <el-table-column :label="t('online.table.loginTime')" align="center" prop="loginTime" width="180">
          <template #default="scope">
            <span>{{ proxy.parseTime(scope.row.loginTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="t('online.table.operation')" align="center" fixed="right" class-name="small-padding fixed-width" width="140">
          <template #default="scope">
            <el-button
              v-hasPermi="['monitor:online:forceLogout']"
              type="danger"
              icon="Delete"
              style="padding: 0 5px; font-size: 10px; height: 24px; --el-button-icon-span-gap: 2px"
              @click="handleForceLogout(scope.row)"
            >
              {{ t('online.tooltip.forceLogout') }}
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination v-show="total > 0" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize" :total="total" />
    </el-card>
  </div>
</template>

<script setup name="Online" lang="ts">
import { forceLogout, list as initData } from '@/api/monitor/online';
import { OnlineQuery, OnlineVO } from '@/api/monitor/online/types';
import api from '@/api/system/user';
import { to } from 'await-to-js';
import { useI18n } from 'vue-i18n';

const { t } = useI18n();
const { proxy } = getCurrentInstance() as ComponentInternalInstance;
const { sys_device_type } = toRefs<any>(proxy?.useDict('sys_device_type'));

const onlineList = ref<OnlineVO[]>([]);
const loading = ref(true);
const total = ref(0);

const queryFormRef = ref<ElFormInstance>();

const queryParams = ref<OnlineQuery>({
  pageNum: 1,
  pageSize: 10,
  ipaddr: '',
  userName: ''
});

/** 查询登录日志列表 */
const getList = async () => {
  loading.value = true;
  const res = await initData(queryParams.value);
  onlineList.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
/** 搜索按钮操作 */
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};
/** 重置按钮操作 */
const resetQuery = () => {
  queryFormRef.value?.resetFields();
  handleQuery();
};
/** 强退按钮操作 */
const handleForceLogout = async (row: OnlineVO) => {
  const [err] = await to(proxy?.$modal.confirm(t('online.message.forceLogoutConfirm', { userName: row.userName })) as any);
  if (!err) {
    await forceLogout(row.tokenId);
    await getList();
    proxy?.$modal.msgSuccess(t('online.message.forceLogoutSuccess'));
  }
};

onMounted(() => {
  getList();
});
</script>
