import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
} 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();

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="ad-spaces"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        headerTitle="广告位列表"
        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({ search: false }),
          {
            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}>
                <BannerSpaceUpdate
                  item={item}
                  reload={action?.reload}
                  title={title}
                />
                <MyButtons.View
                  title="配置"
                  onClick={() => {
                    navigate(`/marketing/ad_location/${item.id}`);
                  }}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Banner.BannerSpaces.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
