import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  CustomerMomentsChannelEnum,
  CustomerMomentsContentTypeEnum,
  CustomerMomentsPushStatusEnum,
  CustomerMomentsPushTypeEnum,
  CustomerMomentsTaskEndTypeEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { Card, Space } from 'antd';
import Create from './modals/Create';

export default function Index({ title = '创建内容' }) {
  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'moments-list',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="moments-list"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        expandable={{
          defaultExpandAllRows: true,
        }}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Customer.CustomerMoments.List,
          )
        }
        toolBarRender={(action) => [
          <Create key="Create" reload={action?.reload} title={title} />,
        ]}
        columns={[
          MyColumns.ID(),
          MyColumns.EnumTag({
            title: '任务推送',
            dataIndex: 'push_status',
            valueEnum: CustomerMomentsPushStatusEnum,
            search: false,
          }),
          {
            title: '标题',
            dataIndex: 'title',
            render: (_, i: any) => {
              return (
                <Card size="small">
                  <Space align="start">
                    <div>{i?.title}</div>
                    <img
                      src={
                        i?.cover_image?.length > 0
                          ? i?.cover_image[0]?.url
                          : i?.attachments[0]?.url
                      }
                      width={45}
                      height={45}
                    />
                  </Space>
                </Card>
              );
            },
          },
          MyColumns.EnumTag({
            title: '发送渠道',
            dataIndex: 'channel',
            valueEnum: CustomerMomentsChannelEnum,
          }),
          MyColumns.EnumTag({
            title: '内容类型',
            dataIndex: 'content_type',
            valueEnum: CustomerMomentsContentTypeEnum,
          }),
          MyColumns.EnumTag({
            title: '推送类型',
            dataIndex: 'push_type',
            valueEnum: CustomerMomentsPushTypeEnum,
          }),
          {
            title: '推送时间',
            dataIndex: 'scheduled_time',
            search: false,
          },

          MyColumns.EnumTag({
            title: '任务结束类型',
            dataIndex: 'task_end_type',
            valueEnum: CustomerMomentsTaskEndTypeEnum,
            search: false,
          }),
          {
            title: '任务结束时间',
            dataIndex: 'task_days',
            search: false,
            render: (_, record: any) => {
              return record?.task_end_type === 'AfterNDays'
                ? `任务发送后:${record?.task_days}天`
                : '${record?.task_end_time}';
            },
          },

          MyColumns.UpdatedAt(),
          MyColumns.CreatedAt(),
          MyColumns.Option({
            render: (_, item: any, index, action) => (
              <Space key={index}>
                {/* <Update item={item} reload={action?.reload} title={title} /> */}
                <MyButtons.Default
                  isConfirm
                  title="发送"
                  type="primary"
                  disabled={
                    item?.push_type === 'ScheduledPush' ||
                    item?.push_status === 'Pushed'
                  }
                  description="确定发送？"
                  onConfirm={() => {
                    Apis.Customer.CustomerMoments.Send({
                      id: item.id,
                    }).then(() => action?.reload());
                  }}
                />
                {/* <MyButtons.Delete
                  onConfirm={() =>
                    Apis.Customer.CustomerMoments.Delete({
                      id: item.id,
                    }).then(() => action?.reload())
                  }
                /> */}
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
