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

/**
 * 朋友圈预览
 * @param param0 
 * @returns 
 */
const PreviewMomentsStrategy: React.FC<{ massSendingStrategy: { [x: string]: any } }> = ({ massSendingStrategy }) => {

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

    useEffect(() => {
        if (massSendingStrategy) {
            form.setFieldsValue({
                ...massSendingStrategy, strategyList: massSendingStrategy?.strategyList?.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={'newMomentsShowzs'}
        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="momentSendName">
                    <Input placeholder="请输入标题" />
                </Form.Item>
            </div>
        </Card>

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

                        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} />
                                </div>
                            </div>
                        </Card>
                    })}
                </>
            )}
        </Form.List>
    </Form>
};

export default React.memo(PreviewMomentsStrategy);