import { App, Button, Modal, Space } from 'antd';
import React, { useRef, useState } from 'react';
import '../../global.less';
import Strategy from './strategy';
import Content from './content';

/**
 * 群发配置
 * @param param0 
 * @returns 
 */
const SettingsMassSending: React.FC<TASK_CREATE.MassSendingProps<any>> = ({ bookCityList, value: { massSendingContent, massSendingStrategy }, createType, onChange, visible, onClose, isEditSc }) => {

    /****************************************/
    const { message } = App.useApp()
    const ref = useRef(null)
    const ref1 = useRef(null)
    const [type, setType] = useState<'STRATEGY' | 'CONTENT'>(createType)
    /****************************************/

    const handleOk = (p: string) => {
        if (type === 'STRATEGY') {
            ref?.current?.handleOk(p)
        } else {
            ref1?.current?.handleOk(p)
        }
    };

    return <Modal
        title={<strong>{type === 'STRATEGY' ? '群发模板配置' : '群发内容配置'}</strong>}
        open={visible}
        onCancel={onClose}
        width={850}
        className={`settingsModal`}
        footer={<Space>
            <Button onClick={onClose}>取消</Button>
            {type === 'STRATEGY' && <>
                <Button type="primary" onClick={() => handleOk('CREATE_CONTENT')}>编辑群发内容</Button>
            </>}
            <Button type="primary" onClick={() => handleOk('OK')}>确定</Button>
        </Space>}
    >
        {/* 策略 */}
        {type === 'STRATEGY' ? <Strategy
            ref={ref}
            value={massSendingStrategy}
            onChange={(value, type) => {
                console.log(value, type)
                if (type === 'CREATE_CONTENT') {
                    setType('CONTENT')
                }
                if (massSendingContent && (value?.strategySettings?.length !== massSendingContent?.massSendingContentDTO?.length || value?.strategySettings?.some((item, index) => massSendingContent?.massSendingContentDTO?.[index]?.sendContentDto?.length !== item?.sendData?.length))) {
                    message.warning('策略配置和内容配置不一致，内容已更新，请查看！')
                    onChange({
                        massSendingContent: {
                            massSendingContentDTO: value?.strategySettings?.map(item => {
                                const mc = massSendingContent.massSendingContentDTO.find(mc => mc.id === item.id)
                                if (mc) {
                                    return {
                                        sendContentDto: item.sendData.map((i) => {
                                            const sendContent = mc.sendContentDto.find(s => s.id === i.id)
                                            if (sendContent) {
                                                return sendContent
                                            }
                                            return { contentDTO: [undefined], sendMode: undefined, id: i?.id }
                                        }),
                                        id: item?.id
                                    }
                                }
                                return {
                                    sendContentDto: item.sendData.map((i) => ({ contentDTO: [undefined], sendMode: undefined, id: i?.id })),
                                    id: item?.id
                                }
                            })
                        },
                        massSendingStrategy: value
                    }, type)
                    return
                }

                onChange({
                    massSendingStrategy: value
                }, type)
            }}
        /> : <Content
            ref={ref1}
            isEditSc={isEditSc}
            bookCityList={bookCityList}
            massSendingStrategy={massSendingStrategy}
            value={massSendingContent}
            onChange={(value, type) => {
                onChange({
                    massSendingContent: value
                }, type)
            }}
        />}

    </Modal>;
};

export default SettingsMassSending;