import { ref, computed, onMounted } from 'vue'
import type { FormSchema } from '@/types/form'
import type { TableColumn } from '@/types/table'
import { useTable } from '@/hooks/web/useTable'
import { ElTag, dayjs } from 'element-plus'
import { h } from 'vue'
import * as ConversationApi from '@/api/new-ai/conversation'

export default function () {
  const searchParams = ref({})
  const infoRef = ref()

  const schema = ref<FormSchema[]>([
    {
      field: 'text',
      component: 'Input',
      label: '内容',
      componentProps: {
        placeholder: '请输入内容'
      },
      colProps: {
        span: 6
      }
    }
  ])

  const columns = ref<TableColumn[]>([
    {
      label: '用户名',
      field: 'username',
      align: 'center'
    },
    {
      label: '窗口标题',
      field: 'title',
      align: 'center'
    },
    {
      label: '对话次数',
      field: 'chatTotal',
      align: 'center',
      width: 180
    },
    {
      label: 'Token消耗量',
      field: 'tokenUsed',
      align: 'center',
      width: 180
    },
    {
      label: '最后一次对话时间',
      field: 'endTime',
      align: 'center',
      width: 180,
      formatter: (row: any) => {
        return dayjs(row.endTime).format('YYYY-MM-DD HH:mm:ss')
      }
    },
    {
      label: '创建时间',
      field: 'createTime',
      width: 180,
      align: 'center',
      formatter: (row: any) => {
        return dayjs(row.createTime).format('YYYY-MM-DD HH:mm:ss')
      }
    }
  ])

  const { register, tableObject, methods } = useTable({
    getListApi: ConversationApi.page,
    defaultParams: searchParams.value,
    delListApi: ConversationApi.del
  })

  const handleSearch = (values: any) => {
    methods.setSearchParams(values)
  }

  const pagination = computed(() => {
    return {
      total: tableObject.total,
      pageSize: tableObject.pageSize,
      currentPage: tableObject.currentPage
    }
  })

  const handleShowInfo = (row: any) => {
    infoRef.value?.show(row)
  }

  onMounted(() => {
    methods.getList()
  })

  return {
    schema,
    columns,
    register,
    handleSearch,
    methods,
    tableObject,
    pagination,
    handleShowInfo,
    infoRef
  }
}
