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

interface Props {
    strategyDTO: { [x: string]: any }
}

const PreviewStrategy: React.FC<Props> = ({ strategyDTO }) => {

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

    useEffect(() => {
        if (strategyDTO && Object.keys(strategyDTO).length) {
            const data = {
                ...strategyDTO, strategyList: strategyDTO?.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
                    }
                })
            }
            form.setFieldsValue(data)
        }
    }, [strategyDTO])

    return <Form
        form={form}
        name="newGroupChatStrategyShow"
        labelAlign='left'
        labelCol={{ span: 5 }}
        colon={false}
        preserve={true}
    >
        {/* <Card title={<strong>基础信息配置</strong>} style={{ background: '#fff', marginBottom: 10 }} id='basicInfo'>
            <Form.Item label={<strong>任务名称</strong>} name="taskName">
                <Input placeholder="请输入任务名称" />
            </Form.Item>
        </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 }}
                            id={`strategy_${index + 1}`}
                        >
                            <div className='block_tm'>
                                <Form.Item
                                    {...restField}
                                    name={[name, 'strategyName']}
                                    label={<strong>策略名称</strong>}
                                >
                                    <Input placeholder='请输入策略名称' allowClear />
                                </Form.Item>
                                <SendTimeSet isShow active='all' form={form} restField={restField} name={name} timeRepeatType={timeRepeatType} />
                            </div>
                        </Card>
                    })}
                </>
            )}
        </Form.List>
    </Form>
};

export default React.memo(PreviewStrategy);