import { useAjax } from "@/Hook/useAjax";
import { addV3StrategyApi } from "@/services/adqV3";
import { Button, Dropdown, Form, Input, message, Modal } from "antd";
import React, { useState } from "react";



interface Props {
    addelivery: PULLIN.AddeliveryProps
    putInType?: 'NOVEL' | 'GAME'
}

/**
 * 素材存为预设
 * @param param0 
 * @returns 
 */
const Save: React.FC<Props> = ({ addelivery: { dynamic: { deliveryMode, creativeTemplateId }, dynamicMaterialDTos, mediaType, dynamicCreativesTextDTOS }, putInType }) => {

    /*********************************/
    const [form] = Form.useForm();
    const [visible, setVisible] = useState<boolean>(false)
    const [type, setType] = useState<1 | 2>(1)

    const addV3Strategy = useAjax((params) => addV3StrategyApi(params))
    /*********************************/

    const seve = () => {
        setVisible(true)
    }

    const handleOk = () => {
        form.validateFields().then(values => {
            let params: any = { ...values }
            if (type === 1) {
                if (deliveryMode === "DELIVERY_MODE_CUSTOMIZE") {
                    params.type = creativeTemplateId
                } else {
                    params.type = deliveryMode
                }
                params.strategyValue = JSON.stringify({ mediaType, dynamicMaterialDTos, deliveryMode, creativeTemplateId, dynamicCreativesTextDTOS })
            }
            addV3Strategy.run({ ...params, taskType: putInType }).then(res => {
                if (res) {
                    message.success('保存成功')
                    form.resetFields()
                    setVisible(false)
                }
            })
        })
    }

    return <>
        <Dropdown
            menu={{
                items: [
                    {
                        label: '根据创意形式保存素材',
                        key: '1',
                        onClick: () => {
                            setType(1)
                            seve()
                        },
                        disabled: !(dynamicMaterialDTos && Object.keys(dynamicMaterialDTos).length > 0)
                    }
                ]
            }}
        >
            <Button type='primary'>素材存为预设</Button>
        </Dropdown>

        {visible && <Modal
            title={<strong>{type === 1 ? '根据创意形式保存素材' : '保存文案'}</strong>}
            open={visible}
            onCancel={() => { form.resetFields(); setVisible(false) }}
            onOk={handleOk}
            confirmLoading={addV3Strategy.loading}
            className="modalResetCss"
        >
            <Form
                form={form}
                layout='vertical'
                name="saveUpdateAd"
                colon={false}
                initialValues={{}}
            >
                <Form.Item label={<strong>描述</strong>} name='strategyKey' rules={[{ required: true, message: '请输入描述' }]}>
                    <Input placeholder="请输入描述" showCount maxLength={30} />
                </Form.Item>
            </Form>
        </Modal>}
    </>
}


export default React.memo(Save)