<template>
  <div class="table-box">
    <ProTable ref="proTable" :columns="columns" row-key="infoId" :request-api="listLogininforApi">
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button type="danger" v-auth="['monitor:logininfor:remove']" icon="WarnTriangleFilled" @click="handleClean"> 清空 </el-button>
        <el-button type="primary" v-auth="['monitor:logininfor:export']" icon="Download" plain @click="downloadFile"> 导出 </el-button>
        <el-button
          type="danger"
          v-auth="['monitor:logininfor:remove']"
          icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
      </template>
    </ProTable>
  </div>
</template>

<script setup lang="tsx" name="LoginInfor">
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import { listLogininforApi, delLogininforApi, exportLogininforApi, cleanLoginInfoApi } from '@/api/modules/monitor/logininfor'
const { proxy } = getCurrentInstance() as ComponentInternalInstance
const { sys_common_status, sys_device_type } = toRefs<any>(proxy?.useDict('sys_common_status', 'sys_device_type'))

// ProTable 实例
const proTable = ref<ProTableInstance>()

// 批量删除系统访问记录信息
const batchDelete = async (ids: string[]) => {
  await useHandleData(delLogininforApi, ids, '删除所选系统访问记录信息')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}

// 导出系统访问记录列表
const downloadFile = async () => {
  ElMessageBox.confirm('确认导出系统访问记录数据?', '温馨提示', { type: 'warning' }).then(() =>
    useDownload(exportLogininforApi, '系统访问记录列表', proTable.value?.searchParam)
  )
}

/** 清空按钮操作 */
const handleClean = async () => {
  await useHandleData(cleanLoginInfoApi, '', '清空所有登录日志数据项')
  proTable.value?.clearSelection()
  proTable.value?.getTableList()
}
// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  {
    prop: 'infoId',
    label: '访问编号',
    width: 120
  },
  {
    prop: 'userName',
    label: '用户账号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'clientKey',
    label: '客户端',
    width: 120
  },
  {
    prop: 'deviceType',
    label: '设备类型',
    tag: true,
    enum: sys_device_type,
    search: {
      el: 'select'
    },
    width: 120
  },
  {
    prop: 'ipaddr',
    label: '地址',
    width: 120
  },
  {
    prop: 'loginLocation',
    label: '登录地点',
    width: 120
  },
  {
    prop: 'browser',
    label: '浏览器',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'os',
    label: '操作系统',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'status',
    label: '登录状态',
    tag: true,
    enum: sys_common_status,
    search: {
      el: 'tree-select'
    }
  },
  {
    prop: 'msg',
    label: '描述',
    width: 120
  },
  {
    prop: 'loginTime',
    label: '访问时间',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  }
])
</script>
