<script setup lang="ts">
import AForm from '@/components/AForm/index.vue'
import { FormConfigType } from '@/components/AForm/type'
import { ToolbarConfigType } from '@/components/AToolbar/type'
import { ColumnConfigType } from '@/components/ATable/type'
import { StrAnyObj, StrAnyObjArr } from '@/typings'
import { useHandleData } from '@/utils/useHandleData'
import { getPageApi, deleteApi } from '@/api/log/access'

const queryRef = ref<InstanceType<typeof AForm>>()

const showQuery = ref<boolean>(true)
const selectKeys = ref<string[]>([])
const pageTotal = ref<number>(0)

const queryData = ref<StrAnyObj>({ pageNum: 1, pageSize: 10 })
const tableData = ref<StrAnyObjArr>([])

const queryConfig: FormConfigType[] = [
  {
    type: 'input',
    prop: 'username',
    label: '用户账号'
  },
  {
    type: 'select',
    prop: 'status',
    label: '访问结果',
    dict: 'success_fail'
  },
  {
    type: 'datePicker',
    prop: 'accessTime',
    label: '访问时间',
    datePickerType: 'daterange',
    defaultTime: [new Date(0, 0, 0, 0, 0, 0), new Date(0, 0, 0, 23, 59, 59)]
  }
]

const toolbarConfig: ToolbarConfigType[] = [
  {
    common: 'search',
    click() {
      queryData.value.pageNum = 1
      getPage()
    }
  },
  {
    common: 'reset',
    click() {
      queryRef.value?.resetFields()
      getPage()
    }
  },
  {
    common: 'delete',
    permissions: 'logAccess:delete',
    disabled() {
      return selectKeys.value.length == 0
    },
    click() {
      handleRemove(selectKeys.value)
    }
  }
]

const columnConfig: ColumnConfigType[] = [
  {
    prop: 'username',
    label: '用户账号',
    width: 180
  },
  {
    prop: 'ip',
    label: 'IP地址',
    width: 180
  },
  {
    prop: 'location',
    label: '地点',
    showOverflowTooltip: true
  },
  {
    prop: 'browser',
    label: '浏览器',
    showOverflowTooltip: true
  },
  {
    prop: 'os',
    label: '操作系统',
    showOverflowTooltip: true
  },
  {
    prop: 'status',
    label: '访问结果',
    dict: 'success_fail',
    width: 120
  },
  {
    prop: 'msg',
    label: '访问信息',
    showOverflowTooltip: true,
    width: 240
  },
  {
    prop: 'accessTime',
    label: '访问时间',
    width: 180
  },
  {
    width: 130,
    handleConfig: [
      {
        common: 'delete',
        permissions: 'logAccess:delete',
        click(row) {
          handleRemove([row.id])
        }
      }
    ]
  }
]

onMounted(() => {
  getPage()
})

function getPage() {
  getPageApi(queryData.value).then((resp) => {
    tableData.value = resp.records
    pageTotal.value = resp.total
  })
}

function tableSelectionChange(item: StrAnyObjArr) {
  selectKeys.value = item.map((item) => item.id)
}

function handleRemove(idList: string[]) {
  useHandleData('是否确认删除？', () => {
    deleteApi({ idList }).then(() => {
      ElMessage.success('删除成功')
      getPage()
    })
  })
}
</script>

<template>
  <div>
    <el-card v-if="showQuery">
      <a-form ref="queryRef" v-model="queryData" :config="queryConfig" :span="6"> </a-form>
    </el-card>

    <a-table
      selection
      :data="tableData"
      :page-total="pageTotal"
      :toolbar-config="toolbarConfig"
      :column-config="columnConfig"
      v-model:showQuery="showQuery"
      v-model:page-num="queryData.pageNum"
      v-model:page-size="queryData.pageSize"
      @page-num-change="getPage"
      @page-size-change="getPage"
      @selection-change="tableSelectionChange"
    >
    </a-table>
  </div>
</template>
