import { Badge, Popover, Space, Typography } from "antd";
import { AnyObject } from "antd/es/_util/type"
import { ColumnsType } from "antd/es/table"
import { businessPlanData, TIME_TYPE } from "../../businessPlan/create/const";
import FilterUserTooltip from "@/pages/weComTask/components/filterUser/filterUserTooltip";
import { QuestionCircleFilled, UserOutlined } from "@ant-design/icons";
const { Paragraph, Title, Text } = Typography;


export const PreviewColumns = (bookPlatForm: TASK_CREATE.BookPlatFormProps[], bookList: TASK_CREATE.BookListProps[], estimateNumDto: Record<string, number>, getNum: (params: Record<string, any>) => void): ColumnsType<AnyObject> => {

    return [
        {
            title: '企微号信息',
            dataIndex: 'corpUser',
            key: 'corpName',
            width: 150,
            fixed: 'left',
            render(value) {
                return <>
                    <Title level={5} style={{ margin: 0 }}>{value?.corpName || '--'}</Title>
                    <Paragraph style={{ margin: 0 }}>{value?.name || '--'}</Paragraph>
                </>
            }
        },
        {
            title: '预计人数',
            dataIndex: 'estimateNum',
            key: 'estimateNum',
            width: 80,
            fixed: 'left',
            align: 'center',
            render(_, record) {
                const { strategyId, taskId, corpUser: { corpUserId } } = record
                return <Space size={20}>
                    <span>{estimateNumDto?.[`${strategyId}_${taskId}_${corpUserId}`] || 0}</span>
                    <a title="获取预计人数"
                        onClick={() => {
                            const { bizType, platform, templateProductId, corpUser, corpUserChat, robotCorpUsers, strategyData, groupObjectData } = record
                            const { externalUserType, externalUserFilter, groupObjectName, tagDTO, weChatAppid, ...itgo } = groupObjectData
                            const detail = {
                                ...itgo,
                                groupName: groupObjectName,
                                msgTagDTO: tagDTO
                            }
                            if (externalUserType === 'specify') {
                                detail.externalUserFilter = {
                                    configName: externalUserFilter.configName,
                                    ...externalUserFilter.configContent
                                }
                            }
                            if (weChatAppid) {
                                detail.mpAccountId = weChatAppid.split('_')[1]
                            }
                            const params = {
                                bizType,
                                platform,
                                templateProductId,
                                corpChatUserIds: corpUserChat.map(item => item.value),
                                corpRobots: robotCorpUsers?.map(item => ({
                                    corpId: item.corpId,
                                    corpUserId: item.corpUserId,
                                    corpUserName: item.name,
                                    corpName: item.corpName
                                })),
                                corpUsers: [{
                                    corpId: corpUser.corpId,
                                    corpUserId: corpUser.corpUserId,
                                    corpUserName: corpUser.name,
                                    corpName: corpUser.corpName
                                }],
                                strategyList: [{
                                    ...strategyData,
                                    taskDetail: [detail]
                                }]
                            }
                            getNum(params)
                        }}
                    ><UserOutlined /></a>
                </Space>
            }
        },
        {
            title: '基础信息',
            dataIndex: 'taskName',
            key: 'taskName',
            width: 130,
            render(value, record) {
                return <>
                    <Title level={5} style={{ margin: 0 }}>标题：{value}</Title>
                    {record?.bizType && <Paragraph style={{ margin: 0 }}>业务类型：{record?.bizType === 'novel' ? '小说' : record?.bizType === 'game' ? '游戏' : '<空>'}</Paragraph>}
                    {record?.platform && <Paragraph style={{ margin: 0 }}>书城：{record?.platform ? bookPlatForm?.find(item => item.id === record?.platform)?.platformName : '<空>'}</Paragraph>}
                    {record?.templateProductId && <Paragraph style={{ margin: 0 }}>适用产品：{record?.templateProductId ? bookList?.find(item => item.id === record?.templateProductId)?.bookName : '<空>'}</Paragraph>}
                </>
            }
        },
        {
            title: '群主号',
            dataIndex: 'corpUserChat',
            key: 'corpUserChat',
            width: 130,
            render(value) {
                return value.map((item, index) => <Paragraph style={{ margin: 0 }} key={index}>{item.label}</Paragraph>)
            }
        },
        {
            title: '机器人客服号',
            dataIndex: 'robotCorpUsers',
            key: 'robotCorpUsers',
            width: 130,
            render(value) {
                return value?.length > 0 ? value?.map((item, index) => <Paragraph style={{ margin: 0 }} key={index}>{item.name}（{item.corpName}）</Paragraph>) : '--'
            }
        },
        {
            title: '策略信息',
            dataIndex: 'strategyData',
            key: 'strategyData',
            width: 200,
            render(_, record) {
                return <>
                    <Title level={5} style={{ margin: 0 }}>策略{record.strategyIndex + 1}</Title>
                    <Paragraph style={{ margin: 0 }}>名称：{record?.strategyName || '<空>'}</Paragraph>
                    <Paragraph style={{ margin: 0 }}>执行类型：{TIME_TYPE[record?.timeRepeatType]}</Paragraph>
                    {record?.sendDay && <Paragraph style={{ margin: 0 }}>执行时间：{record?.sendDay}</Paragraph>}
                    {record?.startTime && <Paragraph style={{ margin: 0 }}>执行日期：{record?.startTime}~{record?.endTime ? record?.endTime : '长期执行'}</Paragraph>}
                    {record?.sendTime && <Paragraph style={{ margin: 0 }}>执行时间：{record?.sendTime}</Paragraph>}
                    {record?.repeatArray && <Paragraph style={{ margin: 0 }}>执行天数：{record?.repeatArray.join('、')}</Paragraph>}
                </>
            }
        },
        {
            title: '群名称',
            dataIndex: 'groupObjectName',
            key: 'groupObjectName',
            width: 120,
            ellipsis: true,
            fixed: 'left'
        },
        {
            title: '是否开启已有旧群聊补缺',
            dataIndex: 'isRepair',
            key: 'isRepair',
            width: 150,
            ellipsis: true,
            render(value, record, index) {
                return value ? <Space>
                    <Badge status="success" text="开启" />
                    <span>近{record.repairTimes}天</span>
                </Space> : <Badge status="error" text="关闭" />
            },
        },
        {
            title: '进群对象',
            dataIndex: 'externalUserFilter',
            key: 'externalUserFilter',
            width: 90,
            align: 'center',
            render(value) {
                return value ? <div style={{ display: 'flex', alignItems: 'center' }}>
                    <div style={{ flex: '1 0', overflow: 'hidden' }}>
                        <Text ellipsis>指定</Text>
                    </div>
                    <Popover
                        placement="right"
                        styles={{ body: { maxWidth: 350, maxHeight: 350, overflow: 'hidden', overflowY: 'auto' } }}
                        mouseEnterDelay={0.5}
                        content={<FilterUserTooltip
                            bookCityList={bookPlatForm?.map(item => ({ label: item.platformName, value: item.platformKey }))}
                            configName={value?.configName}
                            data={value?.configContent}
                        />}
                    >
                        <a style={{ color: '#000' }}><QuestionCircleFilled /></a>
                    </Popover>
                </div> : '全部'
            },
        },
        {
            title: '群递增起始编号',
            dataIndex: 'groupIndex',
            key: 'groupIndex',
            width: 60,
            align: 'center'
        },
        {
            title: '群固定人数',
            dataIndex: 'groupUserCount',
            key: 'groupUserCount',
            width: 50,
            align: 'center'
        },
        {
            title: '邀请客户进群完毕后客服号是否退群',
            dataIndex: 'autoOutGroup',
            key: 'autoOutGroup',
            width: 85,
            align: 'center',
            render(value) {
                return value ? <span style={{ color: '#1890ff' }}>是</span> : <span style={{ color: 'red' }}>否</span>
            },
        },
        {
            title: '是否排除已在群的客户',
            dataIndex: 'excludeInGroup',
            key: 'excludeInGroup',
            width: 60,
            align: 'center',
            render(value) {
                return value ? <span style={{ color: '#1890ff' }}>是</span> : <span style={{ color: 'red' }}>否</span>
            },
        },
        {
            title: '拉群完成后自动删除拉群标签',
            dataIndex: 'excludeInGroup',
            key: 'excludeInGroup',
            width: 70,
            align: 'center',
            render(value) {
                return value ? <span style={{ color: '#1890ff' }}>是</span> : <span style={{ color: 'red' }}>否</span>
            },
        },
        {
            title: '是否开启失败人群重试',
            dataIndex: 'isRetry',
            key: 'isRetry',
            width: 70,
            align: 'center',
            render(value) {
                return value ? <span style={{ color: '#1890ff' }}>是</span> : <span style={{ color: 'red' }}>否</span>
            },
        },
        {
            title: '群聊关联公众号',
            dataIndex: 'weChatAppid',
            key: 'weChatAppid',
            width: 100,
            align: 'center',
            render(value) {
                return value || '--'
            },
        },
        {
            title: '建群模板名称',
            dataIndex: 'templateName',
            key: 'templateName',
            width: 100,
            ellipsis: true,
            render(value) {
                return value || '--'
            },
        },
        {
            title: '拉群完成后群聊备注',
            dataIndex: 'remark',
            key: 'remark',
            width: 140,
            render(value) {
                return value || '--'
            },
        },
        {
            title: '拉群完成后群聊智能标签',
            dataIndex: 'tagDTO',
            key: 'tagDTO',
            width: 200,
            render(value) {
                return value && Object.keys(value)?.length > 0 ?
                    <Paragraph ellipsis style={{ margin: 0 }}>{Object.keys(value).map(key => {
                        if (key === 'business' && value[key]) {
                            return `业务（来源渠道）：${businessPlanData.find(i => i.value === value.business)?.label || '<空>'}`
                        } else if (key === 'bookCity' && value[key]) {
                            return `书城（来源渠道）：${bookPlatForm.find(i => i.id === value.bookCity)?.platformName || '<空>'}`
                        } else if (key === 'product' && value[key]) {
                            return `产品（来源渠道）：${bookList.find(i => i.id === value.product)?.bookName || '<空>'}`
                        }
                        return ''
                    }).join('、')}</Paragraph> : '--'
            },
        },
        {
            title: '客服消息',
            dataIndex: 'groupSendMsg',
            key: 'groupSendMsg',
            width: 140,
            render(value) {
                return value ? value : '--'
            },
        },
    ]

}