import React, { useCallback, useState } from 'react'
import { Modal } from 'antd'
import FormItem from '@/components/Formitem'
import { FormConfig } from '@/components/Formitem/type'
import { useModel } from 'umi'
import WxSelect from '@/components/WxSelect'
/**新建非图文素材 */
let ImgModal = React.memo((props: { isAll?: boolean }) => {
    const { isAll } = props
    const [fromsubmit, setFromsubmit] = useState<() => Promise<any>>()//存放from提交事件
    const callback = useCallback((fnc: () => Promise<any>) => { setFromsubmit(fnc) }, [])//回调获取from提交事件
    const { state, typeEnum, fileOk, offEditFile } = useModel(isAll ? 'useOperating.useBdMedia' : 'useOperating.useBdMediaPup')
    const { imgVisrible, fileType, actionItem } = state
    const { state: { selectWx }, initSelectWx} = useModel('useOperating.useWxGroupList')//公众号筛选
    return <Modal
        title={(actionItem ? '编辑' : '新建') + typeEnum[fileType as string]}
        visible={imgVisrible}
        onOk={(e) => { fileOk(e, fromsubmit, selectWx);initSelectWx() }}
        onCancel={()=>{
            offEditFile();
            initSelectWx()
        }}
        destroyOnClose
        maskClosable={false}
    >
        <FormItem
            formConfig={config(typeEnum[fileType as string])}
            flow='column'
            getFormSubmit={callback}
            defaultProps={{ sort: 0 }}
            isAll={isAll}
        />
        <WxSelect />
    </Modal>
})
export default ImgModal
let config: (name: string,) => FormConfig[] = (name) => {
    let videoArr: FormConfig[] = [
        {
            label: name + '名称',
            tag: 'input',
            name: 'videoTitle',
            width: 200,
        },
        
        {
            label: name + '描述',
            tag: 'TextArea',
            name: 'videoDescription',
            width: 300,
        },
        {
            label: '排序',
            tag: 'inputNumber',
            name: 'sort',
            width: 200,
        },
        {
            label: '上传' + name,
            tag: 'inputFile',
            name: 'file',
            required: true,
            message: '请上传文件'
        }
    ]
    let imgArr: FormConfig[] = [
        {
            label: name + '名称',
            tag: 'input',
            name: 'fileName',
            width: 200,
        },
        {
            label: '排序',
            tag: 'inputNumber',
            name: 'sort',
            width: 200,
        },
        {
            label: '上传' + name,
            tag: 'imgCrop',
            name: 'file',
            required: true,
            message: '请上传文件'
        }
    ]
    let arr: FormConfig[] = [
        {
            label: name + '名称',
            tag: 'input',
            name: 'fileName',
            width: 200,
        },
        {
            label: '排序',
            tag: 'inputNumber',
            name: 'sort',
            width: 200,
        },
        {
            label: '上传' + name,
            tag: 'inputFile',
            name: 'file',
            required: true,
            message: '请上传文件'
        }
    ]
    if (name === '编辑') {
        return [
            {
                label: name + '名称',
                tag: 'input',
                name: 'fileName',
                width: 200
            },
        ]
    }
    return name === '视频' ? videoArr : name === '图片' ? imgArr : arr
}

