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 SettingsHighMassSending: React.FC<TASK_CREATE.HighMassSendingProps<any>> = ({ value: { highMassSendingContent, highMassSendingStrategy }, createType, onChange, visible, onClose }) => {

    /****************************************/
    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={highMassSendingStrategy}
            onChange={(value, type) => {
                console.log(value, type)
                if (type === 'CREATE_CONTENT') {
                    setType('CONTENT')
                }
                if (highMassSendingContent && (value?.strategySettings?.length !== highMassSendingContent?.massSendingContentDTO?.length || value?.strategySettings?.some((item, index) => highMassSendingContent?.massSendingContentDTO?.[index]?.sendContentDto?.length !== item?.sendData?.length))) {
                    message.error('策略配置和内容配置不一致，内容已重置，请重新配置！')
                    onChange({
                        highMassSendingContent: undefined,
                        highMassSendingStrategy: value
                    }, type)
                    return
                }

                onChange({
                    highMassSendingStrategy: value
                }, type)
            }}
        /> : <Content
            ref={ref1}
            massSendingStrategy={highMassSendingStrategy}
            value={highMassSendingContent}
            onChange={(value, type) => {
                onChange({
                    highMassSendingContent: value
                }, type)
            }}
        />}

    </Modal>;
};

export default SettingsHighMassSending;