import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  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 ActivityCreate from './modals/ActivityCreate';
import ActivityShow from './modals/ActivityShow';
import ActivityUpdate from './modals/ActivityUpdate';
import EnrollsList from './modals/EnrollsList';

export default function Index({ title = '项目活动' }) {
  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="activity"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(params, sort, Apis.Activity.Activities.List)
        }
        toolBarRender={(action) => [
          <ActivityCreate key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          {
            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,
          },
          {
            title: '活动报名',
            dataIndex: 'is_enroll',
            search: false,
            render(_, record) {
              return `${record?.is_enroll ? '开启' : '关闭'} `;
            },
          },
          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} />
                {/* {(!item.end_time || new Date(item.end_time) > new Date()) && (
                )} */}
                <ActivityUpdate
                  item={item}
                  reload={action?.reload}
                  // title={title}
                />
                {/* {(!item.end_time || new Date(item.end_time) > new Date()) && (
                  <MyButtons.Default
                    title={
                      item.publish_status ===
                      ActivitiesPublishStatusEnum.Unpublished.value
                        ? '发布'
                        : item.publish_status ===
                          ActivitiesPublishStatusEnum.Published.value
                        ? '下架'
                        : '重新发布'
                    }
                    type="primary"
                    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());
                    }}
                  />
                )} */}
                <MyButtons.Default
                  title={
                    item.publish_status ===
                    ActivitiesPublishStatusEnum.Unpublished.value
                      ? '发布'
                      : item.publish_status ===
                        ActivitiesPublishStatusEnum.Published.value
                      ? '下架'
                      : '重新发布'
                  }
                  type="primary"
                  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());
                  }}
                />
                {item.is_enroll === 1 && (
                  <EnrollsList
                    item={item}
                    title="报名"
                    reload={action?.reload}
                  />
                )}
                <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Activity.Activities.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
