import { Button, Card, Flex } from 'antd'
import { lazy, ReactNode } from 'react'
import AppList, { AppListProps } from '@/components/AppList'
import ViewDataButtonModal from '../data/ViewMyDataButtonModal'
import ViewChartButtonModal from './ViewChartButtonModal'
import { getChartComponentList } from '@/api/data'
import { useDeleteChart } from '@/hooks/useChartQuery'
import { CHART_TYPE } from '@/utils/constants'
const ButtonModal = lazy(() => import('@/components/chart/ChartButtonModal'))

export default function DataWrap({
  title,
  type,
}: {
  type: string
  title: string
  buttons?: ReactNode
  children?: ReactNode
}) {
  const { mutate } = useDeleteChart()
  const handleRemove = (record: any) => {
    mutate({ id: record.id })
  }

  const listOptions: AppListProps = {
    queryKey: ['chartComponentList', { type }],
    fetchApi: async (params) => {
      const { data } = await getChartComponentList({ ...params, type })
      return { ...data }
    },
    columns: [
      {
        title: '序号',
        key: 'index',
        render(_value, _record, index) {
          return index + 1
        },
        width: 62,
        align: 'center',
      },
      {
        title: '组件类型',
        dataIndex: 'type',
        align: 'center',
        render(value: keyof typeof CHART_TYPE) {
          return CHART_TYPE[value] || value
        },
      },
      { title: '组件名称', dataIndex: 'name', align: 'center' },
      { title: '创建人', dataIndex: 'created_operator_name', align: 'center' },
      { title: '创建时间', dataIndex: 'created_time', align: 'center' },
      { title: '更新时间', dataIndex: 'updated_time', align: 'center' },
      {
        title: '操作',
        key: 'x',
        width: 220,
        align: 'center',
        render(_value, record) {
          return (
            <>
              <ViewChartButtonModal id={record.id}></ViewChartButtonModal>
              <ButtonModal type={type} id={record.id}></ButtonModal>
              <Button color="danger" variant="text" onClick={() => handleRemove(record)}>
                删除
              </Button>
            </>
          )
        },
      },
    ],
  }
  return (
    <Flex gap={20} style={{ height: '100%' }}>
      <Card className="app-card" title={title} style={{ flex: 1, overflowX: 'hidden' }}>
        <Flex justify="space-between" style={{ marginBottom: '20px' }}>
          <Flex wrap gap={10}>
            <ButtonModal type={type}></ButtonModal>
          </Flex>
          <ViewDataButtonModal></ViewDataButtonModal>
        </Flex>
        <AppList {...listOptions} />
      </Card>
    </Flex>
  )
}
