import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import { BannerSpacesTypeEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space } from 'antd';
import BannerSpaceCreate from './modals/BannerSpaceCreate';
import BannerSpaceUpdate from './modals/BannerSpaceUpdate';

export default function Index({ title = '广告位' }) {
  const navigate = useNavigate();

  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'banner-spaces',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="banner-spaces"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Banner.BannerSpaces.List)
        }
        toolBarRender={(action) => [
          <BannerSpaceCreate
            key="Create"
            reload={action?.reload}
            title={title}
          />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            title: '广告位名称',
            dataIndex: 'name',
          },
          MyColumns.EnumTag({
            title: '类型',
            dataIndex: 'type',
            valueEnum: BannerSpacesTypeEnum,
            search: false,
          }),
          {
            title: '备注',
            dataIndex: 'remark',
            search: false,
            ellipsis: true,
          },
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <MyButtons.View
                  title="查看"
                  onClick={() => {
                    navigate(`/banner_space/${item.id}`);
                  }}
                />
                <BannerSpaceUpdate
                  item={item}
                  reload={action?.reload}
                  title={title}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Banner.BannerSpaces.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
