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 PreviewFriendsStrategy: React.FC<{ friendsStrategy: { [x: string]: any }, configType?: BUSINES_SPLAN_API.ConfigTypeProps }> = ({ friendsStrategy, configType = 'USER_GROUP' }) => {

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

    useEffect(() => {
        if (friendsStrategy) {
            form.setFieldsValue({
                ...friendsStrategy, strategySettings: friendsStrategy?.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
                    }
                })
            })
        }
    }, [friendsStrategy])

    return <Form
        form={form}
        name={'friendsStrategyShowzs' + 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="momentSendName">
                    <Input placeholder="请输入标题" />
                </Form.Item>
            </div>
        </Card>

        <Form.List name="strategySettings">
            {(fields) => (
                <div style={{ padding: '4px 2px 0', border: '1px dashed #d9d9d9', borderRadius: 8, background: '#f5f5f5' }}>
                    {fields.map(({ key, name, ...restField }, index) => {
                        const timeRepeatType = strategySettings?.[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>
                    })}
                </div>
            )}
        </Form.List>
    </Form>
};

export default React.memo(PreviewFriendsStrategy);