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';


interface Props {
    data: any,
    bookPlatForm: TASK_CREATE.BookPlatFormProps[]
    bookList: TASK_CREATE.BookListProps[]
    corpList: TASK_CREATE.CorpListProps[]
    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, corpList, 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={1200}
        styles={{ body: { paddingTop: 5 } }}
    >

        <Spin spinning={getProjectLogList.loading}>
            <DispatchTaskDetails.Provider
                value={{ bookPlatForm, bookList, corpList }}
            >
                <Tabs
                    items={Object.keys(previewData).filter(key => key === 'welcomeMsgTemplateVO' ? previewData[key] : previewData[key]?.length).map(key => ({
                        key: key,
                        label: { 'externalUserTransferTasksVOList': '客户继承', 'groupSendTaskVOList': '客户群发', 'welcomeMsgTemplateVO': '欢迎语', 'messageSendTaskVOS': '高级群发' }[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]} />
                        </> : undefined
                    }))}
                />
            </DispatchTaskDetails.Provider>
        </Spin>
    </Drawer>
};

export default Log;