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'

/**
 * 客户继承预览
 * @returns 
 */
const PreviewUserInherit: React.FC<{ userInherit: any }> = ({ userInherit }) => {

    const [form] = Form.useForm();
    const schedulingStrategyDTO = Form.useWatch('schedulingStrategyDTO', form)


    // 回填
    useEffect(() => {
        if (userInherit && Object.keys(userInherit).length) {
            const data = {
                ...userInherit, schedulingStrategyDTO: userInherit?.schedulingStrategyDTO?.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)
        }
    }, [userInherit])

    return <Form
        form={form}
        name="newUserInheritShow"
        labelAlign='left'
        labelCol={{ span: 5 }}
        colon={false}
        scrollToFirstError={{
            behavior: 'smooth',
            block: 'center'
        }}
        preserve={true}
    >
        <Card title={<strong>基础信息配置</strong>} style={{ background: '#fff', marginBottom: 10 }} id='basicInfo'>
            <div className='block_tm'>
                <Form.Item label={<strong>继承标题</strong>} name="taskName">
                    <Input placeholder="请输入标题" allowClear />
                </Form.Item>
            </div>
        </Card>

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

                        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="请输入标题" allowClear />
                                    </Form.Item>
                                    <div id={`strategy_${index}_sendTime`}>
                                        <SendTimeSet isShow active='all' form={form} restField={restField} name={name} timeRepeatType={timeRepeatType} />
                                    </div>

                                    <Form.List name={[name, 'inheritDto']}>
                                        {(fields) => (
                                            <>
                                                {fields.map(({ key, name, ...restField }, i) => {
                                                    return <Card
                                                        key={i}
                                                        title={<strong>策略{index + 1} 继承转移{i + 1}</strong>}
                                                        id={`strategy_${index}_${i}_inheritDto`}
                                                        style={{ background: '#fff', marginBottom: 10 }}
                                                    >
                                                        <Form.Item
                                                            label={<strong>转移对象</strong>}
                                                        >
                                                            <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'flex-start' }}>
                                                                <Form.Item
                                                                    {...restField}
                                                                    name={[name, 'transferType']}
                                                                    noStyle
                                                                >
                                                                    <Radio.Group options={[{ label: '全部', value: 'all' }, { label: '指定', value: 'specify' }]} />
                                                                </Form.Item>
                                                                {inheritDto?.[i]?.transferType === 'specify' && <div style={{ marginTop: 8, width: '100%' }}>
                                                                    <Form.Item
                                                                        {...restField}
                                                                        name={[name, 'transferUserDto']}
                                                                        noStyle
                                                                    >
                                                                        <FilterUser isSHow configType='USER_GROUP' />
                                                                    </Form.Item>
                                                                </div>}
                                                            </div>
                                                        </Form.Item>
                                                        <Form.Item {...restField} name={[name, 'transferSuccessMsg']} label={<strong>文本消息</strong>} >
                                                            <Input.TextArea placeholder={`移成功后发给客户的消息，最多200个字符，不填则使用默认文案`} rows={2} />
                                                        </Form.Item>
                                                    </Card>
                                                })}
                                            </>
                                        )}
                                    </Form.List>
                                </div>
                            </div>
                        </Card>
                    })}
                </>
            )}
        </Form.List>
    </Form>
};

export default React.memo(PreviewUserInherit);