import { useAjax } from '@/Hook/useAjax';
import { getProjectLogListApi } from '@/pages/weComTask/API/businessPlan/create';
import { Drawer, Input, Spin, Tabs } from 'antd';
import React, { useEffect, useState } from 'react';
import WelcomeTask from './components/welcomeTask';
import ExternalUserTransferTask from './components/externalUserTransferTask';
import GroupTask from './components/groupTask';


interface Props {
    data: any,
    bookPlatForm: TASK_CREATE.BookPlatFormProps[]
    bookList: TASK_CREATE.BookListProps[]
    visible?: boolean,
    onClose?: () => void,
}

export const DispatchTaskDetails = React.createContext<TASK_CREATE.DispatchTaskDetails | null>(null);
/**
 * 任务日志
 * @param param0 
 * @returns 
 */
const Log: React.FC<Props> = ({ data, bookPlatForm, bookList, visible, onClose }) => {

    /******************************************************/
    const [previewData, setPreviewData] = useState<{ welcomeMsgTemplateVO?: any, groupSendTaskVOList?: any[], externalUserTransferTasksVOList?: any[] }>({})
    const [taskId, setTaskId] = useState<string>()
    const [accessKey, setActiveKey] = useState<string>()

    const getProjectLogList = useAjax((params) => getProjectLogListApi(params))
    /******************************************************/

    useEffect(() => {
        console.log(data.id)
        if (data?.msg === '跳转日志详情') {
            setTaskId(data.taskId)
        }
        getProjectLogList.run(data.id).then(res => {
            if (res?.data) {
                setPreviewData(res.data)
                setActiveKey(Object.keys(previewData)?.[0])
                if (data?.msg === '跳转日志详情') {
                    switch (data.taskType) {
                        case 'TASK_STAT_TRANSFER': // 继承
                            setActiveKey('externalUserTransferTasksVOList')
                            break
                        case 'TASK_STAT_GROUP_SEND': // 官方群发
                            setActiveKey('groupSendTaskVOList')
                            break
                    }
                }
            }
        })
    }, [])

    return <Drawer
        title={<strong>{data.projectName} 任务详情</strong>}
        onClose={onClose}
        open={visible}
        width={1400}
        styles={{ body: { paddingTop: 5 } }}
    >
        {accessKey !== 'welcomeMsgTemplateVO' && <Input.Search placeholder="请输入任务ID" allowClear defaultValue={data?.taskId} onSearch={(value) => { setTaskId(value); }} style={{ marginBottom: 10, width: 250 }} />}
        <Spin spinning={getProjectLogList.loading}>
            <DispatchTaskDetails.Provider
                value={{ bookPlatForm, bookList }}
            >
                <Tabs
                    items={Object.keys(previewData).filter(key => key === 'welcomeMsgTemplateVO' ? previewData[key] : previewData[key]?.length).map((key, index) => ({
                        key: key,
                        label: { 'externalUserTransferTasksVOList': '客户继承', 'groupSendTaskVOList': '客户群发', 'welcomeMsgTemplateVO': '欢迎语', 'messageSendTaskVOS': '高级群发', 'momentsTaskVOList': '朋友圈' }[key],
                        children: key === 'externalUserTransferTasksVOList' ? <>
                            <ExternalUserTransferTask externalUserTransferTasksVOList={taskId ? previewData?.[key]?.filter(item => item.id == taskId) : previewData[key]} />
                        </> : key === 'groupSendTaskVOList' ? <>
                            <GroupTask groupSendTaskVOList={taskId ? previewData?.[key]?.filter(item => item.id == taskId) : previewData[key]} />
                        </> : key === 'welcomeMsgTemplateVO' ? <>
                            <WelcomeTask welcomeMsgTemplateVO={previewData[key]} />
                        </> : undefined
                    }))}
                    accessKey={accessKey}
                    onChange={(key) => setActiveKey(key)}
                />
            </DispatchTaskDetails.Provider>
        </Spin>
    </Drawer>
};

export default Log;