import { useAjax } from '@/Hook/useAjax';
import { getProjectLogListApi } from '@/pages/weComTask/API/businessPlan/create';
import { Drawer, 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';
import HighGroupTask from './components/highGroupTask';
import MomentTask from './components/momentTask';


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 getProjectLogList = useAjax((params) => getProjectLogListApi(params))
    /******************************************************/

    useEffect(() => {
        console.log(data.id)
        getProjectLogList.run(data.id).then(res => {
            if (res?.data) {
                setPreviewData(res.data)
            }
        })
    }, [])

    return <Drawer
        title={<strong>{data.projectName} 任务详情</strong>}
        onClose={onClose}
        open={visible}
        width={1400}
        styles={{ body: { paddingTop: 5 } }}
    >

        <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 => ({
                        key: key,
                        label: { 'externalUserTransferTasksVOList': '客户继承', 'groupSendTaskVOList': '客户群发', 'welcomeMsgTemplateVO': '欢迎语', 'messageSendTaskVOS': '高级群发', 'momentsTaskVOList': '朋友圈' }[key],
                        children: key === 'externalUserTransferTasksVOList' ? <>
                            <ExternalUserTransferTask externalUserTransferTasksVOList={previewData[key]} />
                        </> : key === 'groupSendTaskVOList' ? <>
                            <GroupTask groupSendTaskVOList={previewData[key]} />
                        </> : key === 'welcomeMsgTemplateVO' ? <>
                            <WelcomeTask welcomeMsgTemplateVO={previewData[key]} />
                        </> : key === 'messageSendTaskVOS' ? <>
                            <HighGroupTask groupSendTaskVOList={previewData[key]} />
                        </> : key === 'momentsTaskVOList' ? <>
                            <MomentTask momentTaskVOList={previewData[key]} />
                        </> : undefined
                    }))}
                />
            </DispatchTaskDetails.Provider>
        </Spin>
    </Drawer>
};

export default Log;