import {
  MyBetaModalFormProps,
  MyButtons,
  MyColumns,
  MyProTableProps,
} from '@/common';
import { MyModal } from '@/components/MyModal';
import { Apis } from '@/gen/Apis';
import { ActivityEnrollsStatusEnum } from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Space } from 'antd';

export default function EnrollsList(props: MyBetaModalFormProps) {
  return (
    <MyModal
      title={props.title || '报名'}
      type={props.item?.type || 'primary'}
      width="800px"
      node={
        <ProTable
          {...MyProTableProps.props}
          request={async (params, sort) =>
            MyProTableProps.request(
              { ...params, activities_id: props?.item?.id },
              sort,
              Apis.Activity.ActivityEnrolls.List,
            )
          }
          options={false}
          columns={[
            MyColumns.ID(),
            {
              title: '客户姓名',
              dataIndex: 'customer_name',
              search: false,
            },
            {
              title: '客户电话',
              dataIndex: 'customer_phone',
              search: false,
            },
            MyColumns.EnumTag({
              title: '状态',
              dataIndex: 'status',
              valueEnum: ActivityEnrollsStatusEnum,
            }),
            MyColumns.Option({
              render: (_, item: any, index, action) => (
                <Space key={index}>
                  <MyButtons.Default
                    title={
                      item.status === ActivityEnrollsStatusEnum.Success.value
                        ? '取消报名'
                        : item.status ===
                          ActivityEnrollsStatusEnum.Cancelled.value
                        ? '重新报名'
                        : '报名'
                    }
                    isConfirm
                    description={
                      item.status === ActivityEnrollsStatusEnum.Success.value
                        ? '确认取消报名吗？'
                        : item.status ===
                          ActivityEnrollsStatusEnum.Cancelled.value
                        ? '确认重新报名吗？'
                        : '确认报名吗？'
                    }
                    onConfirm={() => {
                      let newStatus;
                      if (
                        item.status === ActivityEnrollsStatusEnum.Success.value
                      ) {
                        newStatus = ActivityEnrollsStatusEnum.Cancelled.value;
                      } else if (
                        item.status ===
                        ActivityEnrollsStatusEnum.Cancelled.value
                      ) {
                        newStatus = ActivityEnrollsStatusEnum.Success.value;
                      } else {
                        newStatus = ActivityEnrollsStatusEnum.Success.value;
                      }
                      Apis.Activity.ActivityEnrolls.ChangeStatus({
                        id: item.id,
                        status: newStatus,
                      }).then(() => action?.reload());
                    }}
                  />
                  <EnrollsList
                    item={item}
                    title="报名信息"
                    reload={action?.reload}
                  />
                </Space>
              ),
            }),
          ]}
        />
      }
    />
  );
}
