import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { BannersRedirectTypeEnum, BannersTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import BannerCreate from './modals/BannerCreate';
import BannerShow from './modals/BannerShow';
import BannerUpdate from './modals/BannerUpdate';

export default function Index({ title = '内容管理' }) {
  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'banners',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="banners"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Banner.Banners.List)
        }
        toolBarRender={(action) => [
          <BannerCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),

          {
            title: '广告名称',
            dataIndex: 'name',
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'type',
            valueEnum: BannersTypeEnum,
            search: false,
          }),
          MyColumns.EnumTag({
            title: '跳转类型',
            dataIndex: 'redirect_type',
            valueEnum: BannersRedirectTypeEnum,
            search: false,
          }),
          {
            title: '开始时间',
            dataIndex: 'start_time',
            valueType: 'dateTime',
            search: false,
          },
          {
            title: '结束时间',
            dataIndex: 'end_time',
            valueType: 'dateTime',
            search: false,
          },
          {
            title: '广告位',
            dataIndex: ['banner_space', 'name'],
            search: false,
            render: (_, record) => {
              return record?.banner_space?.name;
            },
          },
          {
            //创建日期
            title: '创建日期',
            dataIndex: 'created_at',
            valueType: 'dateTime',
            search: false,
            render: (_, record) => {
              return record?.created_at?.substring(0, 10);
            },
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <BannerShow item={item} />
                <BannerUpdate
                  item={item}
                  reload={action?.reload}
                  title={title}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Banner.Banners.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
