import { App, Button, Card, Form, Input, Modal, Select } from "antd";
import React from "react";
import { FANQIELINKTYPE, HUASHENGLINKTYPE, LINKTYPE, WENDINGLINKTYPE } from "./const";
import { PlusOutlined, MinusCircleOutlined } from "@ant-design/icons";
import { useAjax } from "@/Hook/useAjax";
import { addCorpAutoLinkApi } from "../../API/bookLink";
import PagePromoLinkCreateDTO from "./components/yuewen/pagePromoLinkCreateDTO";
import BookPromoLinkCreateDTO from "./components/yuewen/bookPromoLinkCreateDTO";
import RechargeActivityLinkDTO from "./components/yuewen/rechargeActivityLinkDTO";
import GiftActivityLinkDTO from "./components/yuewen/giftActivityLinkDTO";
import ConsumeActivityLinkDTO from "./components/yuewen/consumeActivityLinkDTO";
import PagePromoLinkCreateDTOFanQie from "./components/fanqie/pagePromoLinkCreateDTO";
import BookPromoLinkCreateDTOFanqie from "./components/fanqie/bookPromoLinkCreateDTO";
import RechargeActivityLinkDTOFanqie from "./components/fanqie/rechargeActivityLinkDTO";
import GiftActivityLinkDTOFanQie from "./components/fanqie/giftActivityLinkDTO";
import ConsumeActivityLinkDTOFanQie from "./components/fanqie/consumeActivityLinkDTO";
import BookPromoLinkCreateDTOHuaSheng from './components/huasheng/bookPromoLinkCreateDTO'
import RechargeActivityLinkDTOHuaSheng from './components/huasheng/rechargeActivityLinkDTO'
import GiftActivityLinkDTOHuaSheng from './components/huasheng/giftActivityLinkDTO'
import BookPromoLinkCreateDTOWenDing from './components/wending/bookPromoLinkCreateDTO'
import RechargeActivityLinkDTOWenDing from './components/wending/rechargeActivityLinkDTO'
import dayJs from "dayjs";

interface Props {
    mpList: { label: string, value: number }[]
    bookPlatForm: TASK_CREATE.BookPlatFormProps[]
    visible?: boolean;
    onChange?: () => void
    onClose?: () => void
    initialValues?: any
}

export const DispatchBookLink = React.createContext<BOOKLINK.DispatchBookLink | null>(null);

/**
 * 书城链接生成
 * @param param0 
 * @returns 
 */
const ModalBookLink: React.FC<Props> = ({ mpList, bookPlatForm, visible, onChange, onClose, initialValues = { yueWenCreateLinkDTOList: [{}] } }) => {

    /********************************************/
    const { message } = App.useApp()
    const [form] = Form.useForm();
    const yueWenCreateLinkDTOList = Form.useWatch('yueWenCreateLinkDTOList', form)
    const fanQieCreateLinkDTOList = Form.useWatch('fanQieCreateLinkDTOList', form)
    const huaShengCreateLinkDTOList = Form.useWatch('huaShengCreateLinkDTOList', form)
    const wenDingCreateLinkDTOList = Form.useWatch('wenDingCreateLinkDTOList', form)
    const platform = Form.useWatch('platform', form)
    const mpAccountIds = Form.useWatch('mpAccountIds', form)

    const addCorpAutoLink = useAjax((params) => addCorpAutoLinkApi(params))
    /********************************************/

    const handleOk = () => {
        form.validateFields().then((values) => {
            console.log(values)
            let params: { [x: string]: any } = {}
            if (values?.platform === 'YUE_WEN') {
                params = {
                    mpAccountIds: values.mpAccountIds,
                    platform: values.platform,
                    yueWenCreateLinkDTOList: values.yueWenCreateLinkDTOList.map(item => {
                        const { linkType, pagePromoLinkCreateDTO, bookPromoLinkCreateDTO, rechargeActivityLinkDTO, consumeActivityLinkDTO, giftActivityLinkDTO, ...rest } = item
                        if (linkType === '1') {
                            return { ...rest, pagePromoLinkCreateDTO, linkType }
                        } else if (linkType === '2') {
                            return { ...rest, bookPromoLinkCreateDTO, linkType }
                        } else if (linkType === '3') {
                            const { activityTime, display, isDayRepeat, rechargeCount, ...ralDto } = rechargeActivityLinkDTO
                            return { ...rest, rechargeActivityLinkDTO: { ...ralDto, rechargeCount, isDayRepeat: rechargeCount == 1 ? isDayRepeat : false, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD'), display: display?.join(',') }, linkType }
                        } else if (linkType === '4') {
                            const { activityTime, display, ...calDto } = giftActivityLinkDTO
                            return { ...rest, giftActivityLinkDTO: { ...calDto, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD'), display: display?.join(',') }, linkType }
                        } else if (linkType === '5') {
                            const { activityTime, display, ...galDto } = consumeActivityLinkDTO
                            return { ...rest, consumeActivityLinkDTO: { ...galDto, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD'), display: display?.join(',') }, linkType }
                        }
                    })
                }
            } else if (values?.platform === 'FAN_QIE') {
                params = {
                    mpAccountIds: values.mpAccountIds,
                    platform: values.platform,
                    fanQieCreateLinkDTOList: values.fanQieCreateLinkDTOList.map(item => {
                        const { linkType, pagePromoLinkCreateDTO, bookPromoLinkCreateDTO, rechargeActivityLinkDTO, consumeActivityLinkDTO, giftActivityLinkDTO, ...rest } = item
                        if (linkType === '1') {
                            return { ...rest, pagePromoLinkCreateDTO, linkType }
                        } else if (linkType === '2') {
                            return { ...rest, bookPromoLinkCreateDTO, linkType }
                        } else if (linkType === '3') {
                            const { activityTime, ...ralDto } = rechargeActivityLinkDTO
                            return { ...rest, rechargeActivityLinkDTO: { ...ralDto, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD') }, linkType }
                        } else if (linkType === '4') {
                            const { activityTime, ...calDto } = giftActivityLinkDTO
                            return { ...rest, giftActivityLinkDTO: { ...calDto, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD') }, linkType }
                        } else if (linkType === '5') {
                            const { activityTime, ...galDto } = consumeActivityLinkDTO
                            return { ...rest, consumeActivityLinkDTO: { ...galDto, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD') }, linkType }
                        }
                    })
                }
            } else if (values?.platform === 'HUA_SHENG') {
                params = {
                    mpAccountIds: values.mpAccountIds,
                    platform: values.platform,
                    huaShengCreateLinkDTOList: values.huaShengCreateLinkDTOList.map(item => {
                        const { linkType, bookPromoLinkCreateDTO, rechargeActivityLinkDTO, giftActivityLinkDTO, ...rest } = item
                        if (linkType === '1') {
                            return { ...rest, bookPromoLinkCreateDTO, linkType }
                        } else if (linkType === '2') {
                            const { activityTime, display, rechargeAmount, giftAmount, ...ralDto } = rechargeActivityLinkDTO
                            return { ...rest, rechargeActivityLinkDTO: { ...ralDto, productName: rechargeAmount + '-' + giftAmount, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD'), display: display?.join(',') }, linkType }
                        } else if (linkType === '3') {
                            const { activityTime, display, rechargeAmount, giftAmount, ...ralDto } = giftActivityLinkDTO
                            return { ...rest, giftActivityLinkDTO: { ...ralDto, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD'), display: display?.join(',') }, linkType }
                        }
                    })
                }
            } else if (values?.platform === 'WEN_DING') {
                params = {
                    mpAccountIds: values.mpAccountIds,
                    platform: values.platform,
                    wenDingCreateLinkDTOList: values.wenDingCreateLinkDTOList.map(item => {
                        const { linkType, bookPromoLinkCreateDTO, rechargeActivityLinkDTO, giftActivityLinkDTO, ...rest } = item
                        if (linkType === '1') {
                            return { ...rest, bookPromoLinkCreateDTO, linkType }
                        } else if (linkType === '2') {
                            const { activityTime, ...ralDto } = rechargeActivityLinkDTO
                            return { ...rest, rechargeActivityLinkDTO: { ...ralDto, startTime: dayJs(activityTime[0]).format('YYYY-MM-DD'), endTime: dayJs(activityTime[1]).format('YYYY-MM-DD') }, linkType }
                        }
                    })
                }
            } else {
                message.error('当前书城占不支持')
                return
            }
            addCorpAutoLink.run(params).then(res => {
                if (res?.data) {
                    message.success('添加成功')
                    onChange?.()
                }
            })
        }).catch(() => {
            form.submit()
        });
    }

    return <Modal
        title={<strong>书城链接生成</strong>}
        open={visible}
        onCancel={onClose}
        onOk={handleOk}
        width={650}
        confirmLoading={addCorpAutoLink.loading}
    >
        <Form
            form={form}
            name="newBookLink"
            labelAlign='left'
            labelCol={{ span: 5 }}
            colon={false}
            scrollToFirstError={{
                behavior: 'smooth',
                block: 'center'
            }}
            onFinishFailed={({ errorFields }) => {
                message.error(errorFields?.[0]?.errors?.[0])
            }}
            onFinish={handleOk}
            initialValues={initialValues}
        >
            <Card title={<strong>基础信息配置</strong>} style={{ background: '#fff', marginBottom: 10 }}>
                <Form.Item label={<strong>书城</strong>} name="platform" rules={[{ required: true, message: '请选择书城!' }]}>
                    <Select
                        showSearch
                        placeholder="书城"
                        filterOption={(input, option) =>
                            ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                        }
                        allowClear
                        options={bookPlatForm.map(item => ({ value: item.platformKey, label: item.platformName }))}
                        onChange={(e) => {
                            if (e === 'YUE_WEN') {
                                form.setFieldsValue({ yueWenCreateLinkDTOList: [{}] })
                            } else if (e === 'FAN_QIE') {
                                form.setFieldsValue({ fanQieCreateLinkDTOList: [{}] })
                            } else if (e === 'HUA_SHENG') {
                                form.setFieldsValue({ huaShengCreateLinkDTOList: [{}] })
                            } else if (e === 'WEN_DING') {
                                form.setFieldsValue({ wenDingCreateLinkDTOList: [{}] })
                            }
                        }}
                    />
                </Form.Item>
                <Form.Item label={<strong>公众号</strong>} name="mpAccountIds" rules={[{ required: true, message: '请选择公众号!' }]}>
                    <Select
                        showSearch
                        mode="multiple"
                        allowClear
                        placeholder="公众号"
                        filterOption={(input, option) =>
                            ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                        }
                        options={mpList}
                    />
                </Form.Item>
            </Card>
            <DispatchBookLink.Provider value={{ form }}>
                {platform === 'YUE_WEN' ? <Form.List name="yueWenCreateLinkDTOList">
                    {(fields, { add, remove }) => (
                        <>
                            {fields.map(({ key, name, ...restField }, index) => {

                                const yueWenCreateLinkDTO = yueWenCreateLinkDTOList?.[index]

                                return <Card
                                    title={<strong>链接信息配置{index + 1}</strong>}
                                    style={{ background: '#fff', marginBottom: 10 }}
                                    key={key}
                                    extra={yueWenCreateLinkDTOList?.length > 1 ? <div style={{ color: 'red' }} onClick={() => remove(name)}>
                                        <MinusCircleOutlined />
                                    </div> : undefined}
                                >
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'linkType']}
                                        label={<strong>链接类型</strong>}
                                        rules={[{ required: true, message: '请选择链接类型!' }]}
                                    >
                                        <Select
                                            showSearch
                                            placeholder="链接类型"
                                            filterOption={(input, option) =>
                                                ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                                            }
                                            allowClear
                                            options={Object.keys(LINKTYPE).map(key => ({ label: LINKTYPE[key], value: key }))}
                                            onChange={(e) => {
                                                if (e === '1') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            yueWenCreateLinkDTOList: yueWenCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '1', pagePromoLinkCreateDTO: {} }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '2') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            yueWenCreateLinkDTOList: yueWenCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '2', bookPromoLinkCreateDTO: { forceStyle: 1, isOpenBottom: false, forceChapter: 0 } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '3') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            yueWenCreateLinkDTOList: yueWenCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '3', rechargeActivityLinkDTO: { rechargeCount: 1, timeIsShow: 1, isDayRepeat: 0 } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '4') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            yueWenCreateLinkDTOList: yueWenCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '4', giftActivityLinkDTO: { expire: 7, resourceType: '1' } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '5') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            yueWenCreateLinkDTOList: yueWenCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '5', consumeActivityLinkDTO: {} }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                }
                                            }}
                                        />
                                    </Form.Item>
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'channelName']}
                                        label={<strong>渠道名称</strong>}
                                        rules={[{ required: true, message: '请输入渠道名称!' }]}
                                    >
                                        <Input placeholder="请输入渠道名称" allowClear />
                                    </Form.Item>
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'channelType']}
                                        label={<strong>推广类型</strong>}
                                        rules={[{ required: true, message: '请选择推广类型!' }]}
                                    >
                                        <Select
                                            showSearch
                                            placeholder="推广类型"
                                            filterOption={(input, option) =>
                                                ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                                            }
                                            allowClear
                                            options={[{ label: '外部', value: '1' }, { label: '内部', value: '2' }]}
                                        />
                                    </Form.Item>
                                    {yueWenCreateLinkDTO?.linkType === '1' ? <PagePromoLinkCreateDTO restField={restField} name={name} /> :
                                        yueWenCreateLinkDTO?.linkType === '2' ? <BookPromoLinkCreateDTO restField={restField} name={name} platform={platform} mpAccountIds={mpAccountIds} yueWenCreateLinkDTO={yueWenCreateLinkDTO} /> :
                                            yueWenCreateLinkDTO?.linkType === '3' ? <RechargeActivityLinkDTO restField={restField} name={name} itemData={yueWenCreateLinkDTO} /> :
                                                yueWenCreateLinkDTO?.linkType === '4' ? <GiftActivityLinkDTO restField={restField} name={name} /> :
                                                    yueWenCreateLinkDTO?.linkType === '5' ? <ConsumeActivityLinkDTO restField={restField} name={name} /> :
                                                        undefined}
                                </Card>
                            })}
                            <Form.Item>
                                <Button type="dashed" onClick={() => add({})} block icon={<PlusOutlined />}>
                                    新增链接信息配置
                                </Button>
                            </Form.Item>
                        </>
                    )}
                </Form.List> : platform === "FAN_QIE" ? <Form.List name="fanQieCreateLinkDTOList">
                    {(fields, { add, remove }) => (
                        <>
                            {fields.map(({ key, name, ...restField }, index) => {
                                const fanQieCreateLinkDTO = fanQieCreateLinkDTOList?.[index]
                                return <Card
                                    title={<strong>链接信息配置{index + 1}</strong>}
                                    style={{ background: '#fff', marginBottom: 10 }}
                                    key={key}
                                    extra={fanQieCreateLinkDTOList?.length > 1 ? <div style={{ color: 'red' }} onClick={() => remove(name)}>
                                        <MinusCircleOutlined />
                                    </div> : undefined}
                                >
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'linkType']}
                                        label={<strong>链接类型</strong>}
                                        rules={[{ required: true, message: '请选择链接类型!' }]}
                                    >
                                        <Select
                                            showSearch
                                            placeholder="链接类型"
                                            filterOption={(input, option) =>
                                                ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                                            }
                                            allowClear
                                            options={Object.keys(FANQIELINKTYPE).map(key => ({ label: FANQIELINKTYPE[key], value: key }))}
                                            onChange={(e) => {
                                                if (e === '1') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            fanQieCreateLinkDTOList: fanQieCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName } = item
                                                                    return { channelName, linkType: '1', pagePromoLinkCreateDTO: {} }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '2') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            fanQieCreateLinkDTOList: fanQieCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName } = item
                                                                    return { channelName, linkType: '2', bookPromoLinkCreateDTO: {} }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '3') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            fanQieCreateLinkDTOList: fanQieCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName } = item
                                                                    return { channelName, linkType: '3', rechargeActivityLinkDTO: { rechargeCount: -1 } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '4') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            fanQieCreateLinkDTOList: fanQieCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName } = item
                                                                    return { channelName, linkType: '4', giftActivityLinkDTO: { rechargeCount: 1 } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '5') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            fanQieCreateLinkDTOList: fanQieCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName } = item
                                                                    return { channelName, linkType: '5', consumeActivityLinkDTO: { rechargeCount: 1, activityFrequencies: 2 } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                }
                                            }}
                                        />
                                    </Form.Item>
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'channelName']}
                                        label={<strong>渠道名称</strong>}
                                        rules={[{ required: true, message: '请输入渠道名称!' }]}
                                    >
                                        <Input placeholder="请输入渠道名称" allowClear />
                                    </Form.Item>
                                    {fanQieCreateLinkDTO?.linkType === '1' ? <PagePromoLinkCreateDTOFanQie restField={restField} name={name} /> :
                                        fanQieCreateLinkDTO?.linkType === '2' ? <BookPromoLinkCreateDTOFanqie restField={restField} name={name} platform={platform} /> :
                                            fanQieCreateLinkDTO?.linkType === '3' ? <RechargeActivityLinkDTOFanqie restField={restField} name={name} /> :
                                                fanQieCreateLinkDTO?.linkType === '4' ? <GiftActivityLinkDTOFanQie restField={restField} name={name} vipDuration={fanQieCreateLinkDTO?.giftActivityLinkDTO?.vipDuration} /> :
                                                    fanQieCreateLinkDTO?.linkType === '5' ? <ConsumeActivityLinkDTOFanQie restField={restField} name={name} /> :
                                                        undefined}
                                </Card>
                            })}
                            <Form.Item>
                                <Button type="dashed" onClick={() => add({})} block icon={<PlusOutlined />}>
                                    新增链接信息配置
                                </Button>
                            </Form.Item>
                        </>
                    )}
                </Form.List> : platform === 'HUA_SHENG' ? <Form.List name="huaShengCreateLinkDTOList">
                    {(fields, { add, remove }) => (
                        <>
                            {fields.map(({ key, name, ...restField }, index) => {
                                const huaShengCreateLinkDTO = huaShengCreateLinkDTOList?.[index]
                                return <Card
                                    title={<strong>链接信息配置{index + 1}</strong>}
                                    style={{ background: '#fff', marginBottom: 10 }}
                                    key={key}
                                    extra={huaShengCreateLinkDTOList?.length > 1 ? <div style={{ color: 'red' }} onClick={() => remove(name)}>
                                        <MinusCircleOutlined />
                                    </div> : undefined}
                                >
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'linkType']}
                                        label={<strong>链接类型</strong>}
                                        rules={[{ required: true, message: '请选择链接类型!' }]}
                                    >
                                        <Select
                                            showSearch
                                            placeholder="链接类型"
                                            filterOption={(input, option) =>
                                                ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                                            }
                                            allowClear
                                            options={Object.keys(HUASHENGLINKTYPE).map(key => ({ label: HUASHENGLINKTYPE[key], value: key }))}
                                            onChange={(e) => {
                                                if (e === '1') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            huaShengCreateLinkDTOList: huaShengCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '1', bookPromoLinkCreateDTO: { cost: 0 } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '2') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            huaShengCreateLinkDTOList: huaShengCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '2', rechargeActivityLinkDTO: { status: 'normal', chargeType: 0, userType: 1, charge: 0, commonType: 0, display: ['stack', 'read_footer_text'] } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '3') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            huaShengCreateLinkDTOList: huaShengCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName } = item
                                                                    return { channelName, linkType: '3', giftActivityLinkDTO: { status: 'normal', freeAmount: 50, userType: 1, charge: 0, commonType: 0, display: ['stack', 'read_footer_text'] } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                }
                                            }}
                                        />
                                    </Form.Item>
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'channelName']}
                                        label={<strong>渠道名称</strong>}
                                        rules={[{ required: true, message: '请输入渠道名称!' }]}
                                    >
                                        <Input placeholder="请输入渠道名称" allowClear />
                                    </Form.Item>
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'channelType']}
                                        label={<strong>推广类型</strong>}
                                        rules={[{ required: huaShengCreateLinkDTO?.linkType == 1 ? true : false, message: '请选择推广类型!' }]}
                                    >
                                        <Select
                                            showSearch
                                            placeholder="推广类型"
                                            filterOption={(input, option) =>
                                                ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                                            }
                                            allowClear
                                            options={[{ label: '外部', value: '1' }, { label: '内部', value: '2' }]}
                                        />
                                    </Form.Item>
                                    {huaShengCreateLinkDTO?.linkType === '1' ? <BookPromoLinkCreateDTOHuaSheng restField={restField} name={name} platform={platform} mpAccountIds={mpAccountIds} huaShengCreateLinkDTO={huaShengCreateLinkDTO} /> :
                                        huaShengCreateLinkDTO?.linkType === '2' ? <RechargeActivityLinkDTOHuaSheng restField={restField} name={name} itemData={huaShengCreateLinkDTO} /> :
                                            huaShengCreateLinkDTO?.linkType === '3' ? <GiftActivityLinkDTOHuaSheng restField={restField} name={name} itemData={huaShengCreateLinkDTO} /> :
                                                undefined}
                                </Card>
                            })}
                            <Form.Item>
                                <Button type="dashed" onClick={() => add({})} block icon={<PlusOutlined />}>
                                    新增链接信息配置
                                </Button>
                            </Form.Item>
                        </>
                    )}
                </Form.List> : platform === 'WEN_DING' ? <Form.List name="wenDingCreateLinkDTOList">
                    {(fields, { add, remove }) => (
                        <>
                            {fields.map(({ key, name, ...restField }, index) => {
                                const wenDingCreateLinkDTO = wenDingCreateLinkDTOList?.[index]
                                return <Card
                                    title={<strong>链接信息配置{index + 1}</strong>}
                                    style={{ background: '#fff', marginBottom: 10 }}
                                    key={key}
                                    extra={wenDingCreateLinkDTOList?.length > 1 ? <div style={{ color: 'red' }} onClick={() => remove(name)}>
                                        <MinusCircleOutlined />
                                    </div> : undefined}
                                >
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'linkType']}
                                        label={<strong>链接类型</strong>}
                                        rules={[{ required: true, message: '请选择链接类型!' }]}
                                    >
                                        <Select
                                            showSearch
                                            placeholder="链接类型"
                                            filterOption={(input, option) =>
                                                ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                                            }
                                            allowClear
                                            options={Object.keys(WENDINGLINKTYPE).map(key => ({ label: WENDINGLINKTYPE[key], value: key }))}
                                            onChange={(e) => {
                                                if (e === '1') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            wenDingCreateLinkDTOList: wenDingCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '1', bookPromoLinkCreateDTO: { qrcodeOpenStatus: 0, linkClassify: '3' } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                } else if (e === '2') {
                                                    setTimeout(() => {
                                                        form.setFieldsValue({
                                                            wenDingCreateLinkDTOList: wenDingCreateLinkDTOList.map((item, i) => {
                                                                if (index === i) {
                                                                    const { channelName, channelType } = item
                                                                    return { channelName, channelType, linkType: '2', rechargeActivityLinkDTO: { userGroup: 0, hideCountDown: 0 } }
                                                                }
                                                                return item
                                                            })
                                                        })
                                                    }, 0)
                                                }
                                            }}
                                        />
                                    </Form.Item>
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'channelName']}
                                        label={<strong>渠道名称</strong>}
                                        rules={[{ required: true, message: '请输入渠道名称!' }]}
                                    >
                                        <Input placeholder="请输入渠道名称" allowClear />
                                    </Form.Item>
                                    <Form.Item
                                        {...restField}
                                        name={[name, 'channelType']}
                                        label={<strong>推广类型</strong>}
                                        rules={[{ required: true, message: '请选择推广类型!' }]}
                                    >
                                        <Select
                                            showSearch
                                            placeholder="推广类型"
                                            filterOption={(input, option) =>
                                                ((option?.label ?? '') as string).toLowerCase().includes(input.toLowerCase())
                                            }
                                            allowClear
                                            options={[{ label: '内推', value: 0 }, { label: '派单', value: 1 }]}
                                        />
                                    </Form.Item>
                                    {wenDingCreateLinkDTO?.linkType === '1' ? <BookPromoLinkCreateDTOWenDing restField={restField} name={name} platform={platform} mpAccountIds={mpAccountIds} wenDingCreateLinkDTO={wenDingCreateLinkDTO} /> :
                                        wenDingCreateLinkDTO?.linkType === '2' ? <RechargeActivityLinkDTOWenDing restField={restField} name={name} /> :
                                            undefined}
                                </Card>
                            })}
                            <Form.Item>
                                <Button type="dashed" onClick={() => add({})} block icon={<PlusOutlined />}>
                                    新增链接信息配置
                                </Button>
                            </Form.Item>
                        </>
                    )}
                </Form.List> : undefined}

            </DispatchBookLink.Provider>
        </Form>
    </Modal>
};

export default React.memo(ModalBookLink);