import {
  MyButtons,
  MyColumns,
  MyPageContainer,
  MyProTableProps,
  usePageTabs,
} from '@/common';
import { Apis } from '@/gen/Apis';
import {
  HouseCollectionTasksChannelEnum,
  HouseCollectionTasksStatusEnum,
} from '@/gen/Enums';
import { ProTable } from '@ant-design/pro-components';
import { useNavigate } from '@umijs/max';
import { Space, Tag } from 'antd';

export default function CollectionTaskList({ title = '通知任务' }) {
  const navigate = useNavigate();

  // 注册当前页面为标签页
  usePageTabs({
    tabKey: 'collection-tasks',
    tabLabel: title,
  });

  return (
    <MyPageContainer
      title={title}
      enableTabs={true}
      tabKey="collection-tasks"
      tabLabel={title}
    >
      <ProTable
        {...MyProTableProps.props}
        request={async (params, sort) =>
          MyProTableProps.request(
            params,
            sort,
            Apis.Collcetion.HouseCollectionTasks.List,
          )
        }
        columns={[
          MyColumns.ID(),
          {
            title: '任务名称',
            dataIndex: 'task_name',
            search: false,
          },
          {
            title: '催缴渠道',
            dataIndex: 'channel',
            search: false,
            render: (_, record: any) => {
              if (!record.channel || !Array.isArray(record.channel)) {
                return '-';
              }
              return record.channel.map((channelValue: string) => {
                const channelInfo =
                  HouseCollectionTasksChannelEnum[
                    channelValue as keyof typeof HouseCollectionTasksChannelEnum
                  ];
                return (
                  <Tag key={channelValue} color={channelInfo?.color}>
                    {channelInfo?.text || channelValue}
                  </Tag>
                );
              });
            },
          },
          MyColumns.EnumTag({
            title: '状态',
            dataIndex: 'status',
            valueEnum: HouseCollectionTasksStatusEnum,
          }),

          {
            title: '房屋数',
            dataIndex: 'total_houses',
            search: false,
          },
          {
            title: '住户数',
            dataIndex: 'total_notified',
            search: false,
          },
          {
            title: '发起人',
            dataIndex: 'CompanyEmployee',
            search: false,
            render: (_, record: any) =>
              `${record?.company_employee?.name}-${record?.company_employee?.phone}`,
          },
          {
            title: '完成时间',
            dataIndex: 'completed_time',
            valueType: 'dateTime',
            search: false,
          },
          MyColumns.CreatedAt(),

          MyColumns.Option({
            render: (_, item: any, index) => (
              <Space key={index}>
                <MyButtons.View
                  title="查看详情"
                  onClick={() => {
                    navigate(`/collection_task/${item.id}`);
                  }}
                />
              </Space>
            ),
          }),
        ]}
      />
    </MyPageContainer>
  );
}
