import { MyButtons, MyColumns, MyProTableProps } from '@/common';
import { Apis } from '@/gen/Apis';
import {
  ActivitiesPublishStatusEnum,
  ActivitiesPublishTypeEnum,
  ActivitiesStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';
import { useEffect, useRef } from 'react';
import ActivityCreate from '../../activities/modals/ActivityCreate';
import ActivityShow from '../../activities/modals/ActivityShow';
import ActivityUpdate from '../../activities/modals/ActivityUpdate';
import EnrollsList from '../../activities/modals/EnrollsList';

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, asset_projects_id: rest.item?.id },
            sort,
            Apis.Activity.Activities.List,
          )
        }
        search={false}
        toolBarRender={(action) => [
          <ActivityCreate
            key="Create"
            item={rest.item}
            reload={action?.reload}
          />,
        ]}
        columns={[
          MyColumns.ID({
            search: false,
          }),
          {
            title: '活动标题',
            dataIndex: 'title',
          },
          MyColumns.EnumTag({
            title: '发布方式',
            dataIndex: 'publish_type',
            valueEnum: ActivitiesPublishTypeEnum,
            search: false,
          }),
          MyColumns.EnumTag({
            title: '发布状态',
            dataIndex: 'publish_status',
            valueEnum: ActivitiesPublishStatusEnum,
            search: false,
          }),
          MyColumns.EnumTag({
            title: '活动状态',
            dataIndex: 'status',
            valueEnum: ActivitiesStatusEnum,
            search: false,
          }),
          {
            title: '开始时间',
            dataIndex: 'start_time',
            search: false,
          },
          {
            title: '结束时间',
            dataIndex: 'end_time',
            search: false,
          },
          {
            title: '发布时间',
            dataIndex: 'publish_time',
            search: false,
          },
          MyColumns.SoftDelete({
            onRestore: Apis.Activity.Activities.Restore,
            onSoftDelete: Apis.Activity.Activities.SoftDelete,
            search: false,
          }),
          MyColumns.UpdatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                <ActivityShow item={item} />
                <ActivityUpdate
                  item={item}
                  reload={action?.reload}
                  // title={title}
                />
                <MyButtons.Default
                  title={
                    item.publish_status ===
                    ActivitiesPublishStatusEnum.Unpublished.value
                      ? '发布'
                      : item.publish_status ===
                        ActivitiesPublishStatusEnum.Published.value
                      ? '下架'
                      : '重新发布'
                  }
                  isConfirm
                  description={
                    item.publish_status ===
                    ActivitiesPublishStatusEnum.Unpublished.value
                      ? '确认发布此活动吗？'
                      : item.publish_status ===
                        ActivitiesPublishStatusEnum.Published.value
                      ? '确认下架此活动吗？'
                      : '确认重新发布此活动吗？'
                  }
                  onConfirm={() => {
                    let newStatus;
                    if (
                      item.publish_status ===
                      ActivitiesPublishStatusEnum.Unpublished.value
                    ) {
                      newStatus = ActivitiesPublishStatusEnum.Published.value;
                    } else if (
                      item.publish_status ===
                      ActivitiesPublishStatusEnum.Published.value
                    ) {
                      newStatus = ActivitiesPublishStatusEnum.Unlisted.value;
                    } else {
                      newStatus = ActivitiesPublishStatusEnum.Published.value;
                    }
                    Apis.Activity.Activities.ChangePublishStatus({
                      id: item.id,
                      publish_status: newStatus,
                    }).then(() => action?.reload());
                  }}
                />
                <EnrollsList
                  item={item}
                  title="报名信息"
                  reload={action?.reload}
                />
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Activity.Activities.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </>
  );
}
