<script setup lang="tsx">
import { reactive, ref, unref } from 'vue'
import { getLogList } from '@/api/system/log'
import { useTable } from '@/hooks/web/useTable'
import { useI18n } from '@/hooks/web/useI18n'
import { Table, TableColumn } from '@/components/Table'
import { Search } from '@/components/Search'
import { FormSchema } from '@/components/Form'
import { ContentWrap } from '@/components/ContentWrap'
import Detail from './components/Detail.vue'
import { Dialog } from '@/components/Dialog'
import { BaseButton } from '@/components/Button'
import { formatToDateTime } from '@/utils/dateUtil'
import { getUserRole } from '@/api/system/department'
import { searchTime } from '@/utils/searchTime'
const { t } = useI18n()

const { tableRegister, tableState, tableMethods } = useTable({
  fetchDataApi: async () => {
    const res = await getLogList({
      page: unref(currentPage) || 1,
      limit: unref(pageSize) || 10,
      ...unref(searchParams)
    })
    return {
      list: res.data.list,
      total: res.data.count || 0
    }
  }
})

const { dataList, loading, total, currentPage, pageSize } = tableState
const { getList } = tableMethods

const tableColumns = reactive<TableColumn[]>([
  {
    field: 'id',
    label: 'ID',
    align: 'center',
    width: 70
  },
  {
    field: 'admin_id',
    label: 'ID/名称',
    minWidth: 120,
    slots: {
      default: (data: any) => {
        return (
          <>
            {data.row.admin_id}/{data.row.admin_name}
          </>
        )
      }
    }
  },
  {
    field: 'ip',
    label: 'IP地址',
    minWidth: 140
  },
  {
    field: 'page',
    label: '行为',
    width: 120
  },
  {
    field: 'path',
    label: '链接',
    minWidth: 120
  },
  {
    field: 'type',
    label: '类型',
    width: 80
  },
  {
    field: 'type',
    label: '操作时间',
    width: 170,
    slots: {
      default: (data: any) => {
        return <>{formatToDateTime(data.row.add_time * 1000)}</>
      }
    }
  },
  {
    field: 'action',
    label: t('userDemo.action'),
    width: 60,
    fixed: 'right',
    align: 'center',
    headerAlign: 'center',
    slots: {
      default: (data: any) => {
        const row = data.row
        return (
          <>
            <BaseButton link size="small" type="success" onClick={() => action(row)}>
              {t('exampleDemo.detail')}
            </BaseButton>
          </>
        )
      }
    }
  }
])

const searchSchema = reactive<FormSchema[]>([
  {
    field: 'admin_id',
    label: '管理员',
    component: 'Select',
    optionApi: async () => {
      const { data } = await getUserRole({ limit: 9999, page: 1 })
      return data.list.map((res) => {
        return {
          label: res.real_name,
          value: res.id
        }
      })
    }
  },
  {
    field: 'ip',
    label: 'ip地址',
    component: 'Input',
    value: '',
    componentProps: {
      placeholder: '请输入IP地址搜索'
    }
  },
  {
    field: 'path',
    label: '链接',
    component: 'Input',
    value: '',
    componentProps: {
      placeholder: '请输入链接搜索'
    }
  },
  searchTime
])

const searchParams = ref<{
  admin_id?: number | string
  ip?: string
  path?: string
  time?: string
}>({
  admin_id: '',
  ip: '',
  path: '',
  time: ''
})
const setSearchParams = (data: any) => {
  if (data.time) {
    data.time = data.time.join(' - ')
  } else {
    data.time = ''
  }
  searchParams.value = data
  getList()
}

const dialogVisible = ref(false)
const currentRow = ref()
const action = async (row: any) => {
  currentRow.value = row
  dialogVisible.value = true
}
</script>

<template>
  <ContentWrap>
    <Search :schema="searchSchema" @reset="setSearchParams" @search="setSearchParams" />
    <Table
      v-model:current-page="currentPage"
      v-model:page-size="pageSize"
      :columns="tableColumns"
      default-expand-all
      node-key="id"
      stripe
      :data="dataList"
      :loading="loading"
      @register="tableRegister"
      :pagination="{
        total
      }"
    />
  </ContentWrap>

  <Dialog v-model="dialogVisible" title="日志详情" width="60%">
    <Detail :current-row="currentRow" />
    <template #footer>
      <BaseButton @click="dialogVisible = false">{{ t('dialogDemo.close') }}</BaseButton>
    </template>
  </Dialog>
</template>
