import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import { BannersRedirectTypeEnum, BannersTypeEnum } from '@/gen/Enums';
import BannerCreate from '@/pages/banner/modals/BannerCreate';
import BannerShow from '@/pages/banner/modals/BannerShow';
import BannerUpdate from '@/pages/banner/modals/BannerUpdate';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useEffect, useRef } from 'react';

export default function Index({ ...rest }) {
  const actionLooks = useRef<any>();
  useEffect(() => {
    actionLooks?.current.reloadAndRest();
  }, [rest.loadmore]);

  return (
    <>
      <ProTable<Record<any, any>>
        {...MyProTableProps.props}
        actionRef={actionLooks}
        request={async (params, sort) =>
          MyProTableProps.request(
            {
              ...params,
              banner_spaces_id: rest.item?.banner_spaces_id,
            },
            sort,
            Apis.Banner.Banners.List,
          )
        }
        toolBarRender={(action) => [
          <BannerCreate
            key="AddOccupant"
            item={{
              ...rest.item,
              banner_spaces_id: rest.item?.banner_spaces_id,
            }}
            reload={action?.reload}
            title="添加住户"
          />,
        ]}
        search={false}
        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,
          },
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <BannerShow item={item} reload={action?.reload} />
                <BannerUpdate item={item} reload={action?.reload} />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Banner.Banners.Delete({ id: item.id }).then(() =>
                      action?.reload(),
                    )
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
