import { Card, Form, Input, Radio } from 'antd';
import React, { useEffect } from 'react';
import style from '../../index.less';
import SendTimeSet from '@/pages/weComTask/components/sendTimeSet';
import FilterUser from '@/pages/weComTask/components/filterUser';
import dayjs from 'dayjs';
import '../../global.less'

/**
 * 群发预览
 * @param param0 
 * @returns 
 */
const PreviewMassSendingStrategy: React.FC<{ massSendingStrategy: { [x: string]: any }, configType?: BUSINES_SPLAN_API.ConfigTypeProps }> = ({ massSendingStrategy, configType = 'USER_GROUP' }) => {

    /************************************************/
    const [form] = Form.useForm();
    const strategySettings = Form.useWatch('strategySettings', form);
    /************************************************/

    useEffect(() => {
        if (massSendingStrategy) {
            form.setFieldsValue({
                ...massSendingStrategy, strategySettings: massSendingStrategy?.strategySettings?.map(item => {
                    const { sendTime, startTime, endTime, sendDay, timeRepeatType } = item
                    if (timeRepeatType === 'TIME_TYPE_SINGLE_PLACE') {
                        const data = {
                            ...item,
                            timeRepeatType,
                            sendDay: sendDay ? dayjs(sendDay + ' ' + sendTime) : undefined
                        }
                        delete data?.sendTime
                        return data
                    }
                    return {
                        ...item,
                        timeRepeatType,
                        sendTime: sendTime ? dayjs('2025-04-25 ' + sendTime) : undefined,
                        startTime: startTime ? dayjs(startTime) : undefined,
                        endTime: endTime ? dayjs(endTime) : undefined,
                        sendDay: sendDay ? dayjs(sendDay) : undefined
                    }
                })
            })
        }
    }, [massSendingStrategy])

    return <Form
        form={form}
        name={'newMassSendingShowzs' + configType}
        labelAlign='left'
        labelCol={{ span: 5 }}
        colon={false}
        preserve={true}
    >
        <Card
            title={<strong>基础信息配置</strong>}
            style={{ background: '#fff', marginBottom: 10 }}
            id='basicInfo'
        >
            <div className='block_tm'>
                <Form.Item label={<strong>群发标题</strong>} name="groupSendName">
                    <Input placeholder="请输入标题" />
                </Form.Item>
            </div>
        </Card>

        <Form.List name="strategySettings">
            {(fields) => (
                <>
                    {fields.map(({ key, name, ...restField }, index) => {
                        const timeRepeatType = strategySettings?.[index]?.timeRepeatType
                        const sendData = strategySettings?.[index]?.sendData

                        return <Card
                            key={key}
                            title={<strong>策略{index + 1}配置</strong>}
                            style={{ background: '#fff', marginBottom: 10 }}
                        >
                            <div className='block_tm'>
                                <div className={style.strategy_item}>
                                    <Form.Item
                                        {...restField}
                                        label={<strong>策略名称</strong>}
                                        name={[name, 'strategyName']}
                                    >
                                        <Input placeholder="请输入标题" />
                                    </Form.Item>
                                    <SendTimeSet isShow active='all' form={form} restField={restField} name={name} timeRepeatType={timeRepeatType} />

                                    <Form.List name={[name, 'sendData']}>
                                        {(fields) => (
                                            <>
                                                {fields.map(({ key, name, ...restField }, i) => {
                                                    return <Card
                                                        key={i}
                                                        title={<strong>策略{index + 1} 发送对象{i + 1}</strong>}
                                                        style={{ background: '#fff', marginBottom: 10 }}
                                                    >
                                                        <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start', width: '100%' }}>
                                                            <Form.Item
                                                                {...restField}
                                                                label={<strong>名称</strong>}
                                                                name={[name, 'externalUserFilterName']}
                                                                rules={[{ required: true, message: '请输入发送对象名称!' }]}
                                                            >
                                                                <Input placeholder="请输入发送对象名称" allowClear />
                                                            </Form.Item>
                                                            <Form.Item
                                                                {...restField}
                                                                name={[name, 'externalUserType']}
                                                                rules={[{ required: true, message: '请选择转移对象!' }]}
                                                                noStyle
                                                            >
                                                                <Radio.Group options={[{ label: '全部', value: 'all' }, { label: '指定', value: 'specify' }]} />
                                                            </Form.Item>
                                                            {sendData?.[i]?.externalUserType === 'specify' && <div style={{ marginTop: 8, width: '100%' }}>
                                                                <Form.Item
                                                                    {...restField}
                                                                    name={[name, 'externalUserFilter']}
                                                                    rules={[{ required: true, message: '请选择人群包!' }]}
                                                                    noStyle
                                                                >
                                                                    <FilterUser isSHow configType={configType} />
                                                                </Form.Item>
                                                            </div>}
                                                        </div>
                                                    </Card>
                                                })}
                                            </>
                                        )}
                                    </Form.List>

                                </div>
                            </div>
                        </Card>
                    })}
                </>
            )}
        </Form.List>
    </Form>
};

export default React.memo(PreviewMassSendingStrategy);