<template>
  <div class="table-box">
    <ProTable
      ref="proTable"
      :columns="columns"
      row-key="infoId"
      :request-api="listLogininforApi"
      :init-param="initParam"
      :data-callback="dataCallback"
    >
      <!-- 表格 header 按钮 -->
      <template #tableHeader="scope">
        <el-button
          type="primary"
          v-auth="['monitor:logininfor:add']"
          :icon="CirclePlus"
          @click="openDialog(1, '系统访问记录新增')"
        >
          新增
        </el-button>
        <el-button type="primary" v-auth="['monitor:logininfor:import']" :icon="Upload" plain @click="batchAdd"> 导入 </el-button>
        <el-button type="primary" v-auth="['monitor:logininfor:export']" :icon="Download" plain @click="downloadFile">
          导出
        </el-button>
        <el-button
          type="danger"
          v-auth="['system:user:remove']"
          :icon="Delete"
          plain
          :disabled="!scope.isSelected"
          @click="batchDelete(scope.selectedListIds)"
        >
          批量删除
        </el-button>
      </template>
    </ProTable>
    <FormDialog ref="formDialogRef" />
    <ImportExcel ref="dialogRef" />
  </div>
</template>

<script setup lang="tsx" name="Logininfor">
import { ref, reactive } from 'vue'
import { useHandleData } from '@/hooks/useHandleData'
import { useDownload } from '@/hooks/useDownload'
import { ElMessageBox } from 'element-plus'
import ProTable from '@/components/ProTable/index.vue'
import ImportExcel from '@/components/ImportExcel/index.vue'
import FormDialog from '@/components/DialogOld/form.vue'
import { ProTableInstance, ColumnProps } from '@/components/ProTable/interface'
import { Delete, Download, Upload, CirclePlus } from '@element-plus/icons-vue'
import {
  listLogininforApi,
  delLogininforApi,
  addLogininforApi,
  updateLogininforApi,
  importTemplateApi,
  importDataApi,
  exportApi,
  getLogininforApi
} from '@/api/modules/monitor/logininfor'
import { getDictsApi } from '@/api/modules/system/dictData'

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

// 如果表格需要初始化请求参数，直接定义传给 ProTable (之后每次请求都会自动带上该参数，此参数更改之后也会一直带上，改变此参数会自动刷新表格数据)
const initParam = reactive({ type: 1 })

// dataCallback 是对于返回的表格数据做处理，如果你后台返回的数据不是 list && total && pageNum && pageSize 这些字段，可以在这里进行处理成这些字段
const dataCallback = (data: any) => {
  return data
}

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

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

// 批量添加系统访问记录
const dialogRef = ref<InstanceType<typeof ImportExcel> | null>(null)
const batchAdd = () => {
  const params = {
    title: '系统访问记录',
    tempApi: importTemplateApi,
    importApi: importDataApi,
    getTableList: proTable.value?.getTableList
  }
  dialogRef.value?.acceptParams(params)
}

const formDialogRef = ref<InstanceType<typeof FormDialog> | null>(null)
// 打开弹框的功能
const openDialog = async (type: number, title: string, row?: any) => {
  let res = { data: {} }
  if (row?.infoId) {
    res = await getLogininforApi(row?.infoId || null)
  }
  // 重置表单
  setFieldList()
  const params = {
    title,
    width: 580,
    isEdit: type !== 3,
    fieldList: fieldList,
    model: type == 1 ? {} : res.data,
    api: type == 1 ? addLogininforApi : updateLogininforApi,
    getTableList: proTable.value?.getTableList
  }
  formDialogRef.value?.openDialog(params)
}

// 表格配置项
const columns = reactive<ColumnProps<any>[]>([
  { type: 'selection', fixed: 'left', width: 70 },
  { prop: 'infoId', label: '访问ID' },
  {
    prop: 'userName',
    label: '用户账号',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'ipaddr',
    label: '登录IP地址',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'loginLocation',
    label: '登录地点',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'browser',
    label: '浏览器类型',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'os',
    label: '操作系统',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'status',
    label: '登录状态',
    tag: true,
    enum: () => getDictsApi('sys_common_status'),
    fieldNames: {
      label: 'dictLabel',
      value: 'dictValue'
    },
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'msg',
    label: '提示消息',
    search: {
      el: 'input'
    },
    width: 120
  },
  {
    prop: 'loginTime',
    label: '访问时间',
    search: {
      el: 'date-picker',
      props: { type: 'datetimerange', valueFormat: 'YYYY-MM-DD HH:mm:ss' }
    },
    width: 120
  }
])
// 表单配置项
let fieldList: Form.FieldItem[] = []
const setFieldList = () => {
  fieldList = [
    {
      label: '用户账号',
      field: 'userName',
      placeholder: '请输入用户账号'
    },
    {
      label: '登录IP地址',
      field: 'ipaddr',
      placeholder: '请输入登录IP地址'
    },
    {
      label: '登录地点',
      field: 'loginLocation',
      placeholder: '请输入登录地点'
    },
    {
      label: '浏览器类型',
      field: 'browser',
      placeholder: '请输入浏览器类型'
    },
    {
      label: '操作系统',
      field: 'os',
      placeholder: '请输入操作系统'
    },
    {
      label: '登录状态',
      field: 'status',
      placeholder: '请输入登录状态'
    },
    {
      label: '提示消息',
      field: 'msg',
      placeholder: '请输入提示消息'
    },
    {
      label: '访问时间',
      field: 'loginTime',
      type: 'date-picker',
      placeholder: '请选择访问时间'
    }
  ]
}
</script>
