<template>
  <div class="app-container">
    <el-form v-show="showSearch" ref="queryRef" :model="queryParams" :inline="true">
      <el-form-item label="登录账号" prop="userName">
        <el-input v-model="queryParams.userName" clearable placeholder="请输入登录账号" @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          v-has-permi="['monitor:online:batchLogout']"
          type="danger"
          icon="Delete"
          plain
          :disabled="multiple"
          @click="handleForceLogoutAll"
        >强退所有设备</el-button>
      </el-col>
      <right-toolbar v-model:show-search="showSearch" @query-table="getList" />
    </el-row>

    <el-table v-loading="loading" :data="onlineList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="50" type="index" align="center">
        <template #default="scope">
          <span>{{ (queryParams.pageNumber - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="账号" align="center" prop="userName" />
      <el-table-column label="昵称" align="center" prop="nickName" />
      <el-table-column label="部门" align="center" prop="deptName" />
      <el-table-column label="登录设备数" align="center">
        <template #default="scope">
          <span>{{ scope.row.devices.length }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            v-has-permi="['monitor:online:query']"
            type="primary"
            icon="View"
            link
            @click="handleView(scope.row)"
          >详细</el-button>
          <el-button
            v-has-permi="['monitor:online:forceLogout']"
            type="danger"
            icon="Delete"
            link
            @click="handleForceLogoutAll(scope.row)"
          >强退所有设备</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      v-model:page="queryParams.pageNumber"
      v-model:limit="queryParams.pageSize"
      :total="total"
      @pagination="getList"
    />

    <!-- 登录设备详情对话框 -->
    <el-dialog v-model="open" title="登录设备详情" width="600px" append-to-body>
      <el-descriptions direction="vertical" border>
        <el-descriptions-item label="账号">{{ form.userName }}</el-descriptions-item>
        <el-descriptions-item label="昵称">{{ form.nickName }}</el-descriptions-item>
        <el-descriptions-item label="部门">{{ form.deptName }}</el-descriptions-item>
      </el-descriptions>
      <el-table :data="form.devices" style="margin-top: 30px;">
        <el-table-column label="设备" align="center" prop="deviceType">
          <template #default="scope">
            <dict-tag :options="login_device_type" :value="scope.row.deviceType" />
          </template>
        </el-table-column>
        <el-table-column label="登录IP地址" align="center" prop="loginIp" />
        <el-table-column label="登录时间" align="center" prop="loginTime" width="180">
          <template #default="scope">
            <span>{{ $parseTime(scope.row.loginTime) }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button
              v-has-permi="['monitor:online:forceLogout']"
              type="danger"
              icon="Delete"
              link
              @click="handleForceLogout(scope.row)"
            >强退</el-button>
          </template>
        </el-table-column>
      </el-table>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="open=false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script name="Online" setup>
import { listOnline, forceLogoutAll, forceLogout } from '@/api/monitor/online'

const { proxy } = getCurrentInstance()
const { login_device_type } = proxy.$useDict('login_device_type')

const onlineList = ref([])
const ids = ref([])
const loading = ref(true)
const showSearch = ref(true)
const multiple = ref(true)
const open = ref(false)
const total = ref(0)
const form = ref({})

const queryParams = reactive({
  pageNumber: 1,
  pageSize: 10,
  userName: undefined
})

/** 查询在线用户列表 */
function getList() {
  loading.value = true
  listOnline(queryParams).then(res => {
    onlineList.value = res.list
    total.value = Number(res.total)
    loading.value = false
  })
}
/** 搜索按钮操作 */
function handleQuery() {
  queryParams.pageNumber = 1
  getList()
}
/** 重置按钮操作 */
function resetQuery() {
  proxy.$resetForm('queryRef')
  handleQuery()
}
// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.userid)
  multiple.value = !selection.length
}
/** 详细按钮操作 */
function handleView(row) {
  proxy.$resetForm('form')
  open.value = true
  form.value = row
}
/** 强退所有按钮操作 */
function handleForceLogoutAll(row) {
  const userIds = row.userid || ids.value
  proxy.$modal.confirm('是否确认强退这 ' + userIds.length + ' 个用户的所有登录设备?').then(function() {
    return forceLogoutAll(userIds)
  }).then(() => {
    getList()
    open.value = false
    proxy.$modal.msgSuccess('强退成功')
  }).catch(() => {})
}
/** 强退按钮操作 */
function handleForceLogout(row) {
  proxy.$modal.confirm('是否确认强退账号为 "' + form.value.userName + '" 的用户?').then(function() {
    return forceLogout(row.token)
  }).then(() => {
    form.value.devices = form.value.devices.filter(item => item.token !== row.token)
    getList()
    if (form.value.devices.length === 0) {
      getList()
      open.value = false
    }
    proxy.$modal.msgSuccess('强退成功')
  }).catch(() => {})
}

getList()
</script>
