import Tables from "@/components/Tables"
import { useAjax } from "@/Hook/useAjax"
import { createAdBatchApi, CreateAdProps } from "@/services/launchAdq/createAd"
import { getSysAdcreativeInfo } from "@/services/launchAdq/creative"
import { PromotedObjectType } from "@/services/launchAdq/enum"
import { getTagsList } from "@/services/launchAdq/global"
import { getSysAdgroupsInfo } from "@/services/launchAdq/localAd"
import { getsysTargetingInfo } from "@/services/launchAdq/targeting"
import { CheckOutlined, CloseOutlined, SearchOutlined } from "@ant-design/icons"
import { Button, Card, Col, Empty, Row, Select, Space, Spin, Tooltip, Image, message, Tabs, Popconfirm, notification } from "antd"
import React, { useCallback, useEffect, useState } from "react"
import { useModel } from "umi"
import Ad from "./ad"
import DataSourceModal from "../../components/dataSourceModal"
import GoodsModal from "../../components/goodsModal"
import IdModal from "../../components/idModal"
import LookLanding from "../../components/lookLanding"
import PageModal from "../../components/pageModal"
import SelectCloud from "../../components/selectCloud"
import style from './index.less'
import Selector from "./selector"
import SubmitModal from "./submitModal"
import columns from "./tableConfig"
import TargetIng from './targeting'
import Creative from './creative'
import AddGroup from '../../components/addGroup'
import CustomerServiceModal from "../../components/customerServiceModal"
import { getTaskDetailsApi } from "@/services/launchAdq/taskList"
import CreativeCL from "./creativeCL"
import { groupBy } from "@/utils/utils"

const CreateAd: React.FC = () => {

    /*************************/
    const { getAdAccount } = useModel('useLaunchAdq.useAdAuthorize')
    const [queryForm, setQueryForm] = useState<Partial<CreateAdProps>>({
        campaignName: '',  // 计划名称
        campaignType: 'CAMPAIGN_TYPE_NORMAL', // 计划类型 CAMPAIGN_TYPE_NORMAL CAMPAIGN_TYPE_SEARCH 
        promotedObjectType: 'PROMOTED_OBJECT_TYPE_WECHAT_OFFICIAL_ACCOUNT', // 推广目标类型
        speedMode: 'SPEED_MODE_STANDARD', // 投放速度模式
        sysAdgroupId: undefined,  // 广告组ID
        sysAdgroup: undefined,//广告组内容
        sysTargetingId: undefined,  // 定向包 id
        sysTargeting: undefined,  // 定向包 内容
        adgroupName: undefined,  // 广告名称
        configuredStatus: 'AD_STATUS_SUSPEND',  // 广告状态
        sysAdcreativeId: undefined, // 创意ID
        taskMediaMaps: [], // 创意内容
        pageList: [],//本地落地页详情入口
        adqPageList: [],//云落地页
        expandEnabled: false,
        expandTargeting: []
    })
    const [launchMode, setLaunchMode] = useState<number>(Number(localStorage.getItem('LAUNCHMODE')) || 1) // 投放模式 1 现在投放模式  2 创量模式
    const [accountCreateLogs, setAccountCreateLogs] = useState<{ adAccountId: number, id: number, userActionSetsList?: any[], productList?: any, conversionList?: any, customAudienceList?: any, excludedCustomAudienceList?: any, pageList?: any, coldStartAudienceList?: any[] }[]>([])  // 账户
    const { currentUser: { userId } }: any = useModel('@@initialState', model => ({ currentUser: model.initialState?.currentUser }))

    const [goodsVisible, setGoodsVisible] = useState<boolean>(false) // 选择商品弹窗控制
    const [sourceVisible, setSourceVisible] = useState<boolean>(false) // 选择数据源弹窗控制
    const [idVisible, setIdVisible] = useState<boolean>(false) // 选择转化ID弹窗控制
    const [selectImgVisible, setSelectImgVisible] = useState<boolean>(false) // 选择转化ID弹窗控制
    const [lookVisible, setLookVisible] = useState<boolean>(false) // 选择转化ID弹窗控制
    const [subVisible, setSubVisible] = useState<boolean>(false) // 选择设置名称弹窗控制
    const [pageVisible, setPageVisible] = useState<boolean>(false) // 选择云端落地页控制
    const [tableData, setTableData] = useState<any[]>([])   // 预览表格
    const [tableSelect, setTableSelect] = useState<any[]>([])
    const [geoLocationList, setGeoLocationList] = useState<any>({}) // 所有地域列表
    const [modelList, setModelList] = useState<any>({})  // 所有品牌手机
    const [targetKey, set_targetKey] = useState('0')//创意key
    const [page_checked, set_page_checked] = useState(false)//创意key
    const [usesArr, setUsersArr] = useState<any>(localStorage.getItem('ADQUSERS' + userId) ? JSON.parse(localStorage.getItem('ADQUSERS' + userId) as any) : [])
    const { init, get } = useModel('useLaunchAdq.useBdMediaPup')
    const [cloudParams, setCloudParams] = useState<{ adcreativeTemplateId?: number }>({})


    const tagsList_REGION = useAjax((params) => getTagsList(params))
    const tagsList_MODEL = useAjax((params) => getTagsList(params))
    const getSysAdgroups = useAjax((params) => getSysAdgroupsInfo(params))
    const getsysTargeting = useAjax((params) => getsysTargetingInfo(params))
    const getSysAdcreative = useAjax((params) => getSysAdcreativeInfo(params))
    const createAdBatch = useAjax((params) => createAdBatchApi(params))
    const getTaskDetails = useAjax((params) => getTaskDetailsApi(params))
    /*************************/

    /**数据回填 */
    useEffect(() => {
        let taskId = sessionStorage.getItem('TASKID')
        if (taskId) {
            getTaskDetails.run(taskId).then(res => {
                console.log('res----->', res)
                setLaunchMode(1)
                const { adCreateLogs, campaignType, promotedObjectType, speedMode, sysAdgroup, sysAdgroupId, sysTargeting, sysTargetingId } = res
                let adCreateLogsData = adCreateLogs?.map((item: any) => {
                    return {
                        adAccountId: item?.accountId,
                        id: item?.adAccountId,
                        userActionSetsList: item?.userActionSetList?.map((item: any) => ({ ...item, id: item?.userActionSetId })),
                        productList: item?.product ? [{ ...item?.product, productCatalog: item?.productCatalog, id: Number(item?.product?.productOuterId?.replace(/\D/ig, '')) }] : undefined,
                        coldStartAudienceList: item?.coldStartAudienceList?.map((item: any) => ({ ...item, id: item.audienceId }))
                        // pageList: [item.page]
                    }
                }).filter((item: any, index: number, self: any) => self.findIndex((i: any) => i.id == item.id) === index)

                setAccountCreateLogs(adCreateLogsData)
                const sorted = groupBy(adCreateLogs, (item) => [item.sysAdcreativeId])
                let taskMediaMaps = sorted[0]?.map((item: any) => {
                    let pageElementsSpecList = item?.sysPage?.pageSpecsList[0]?.pageElementsSpecList // 内容区
                    let globalSpec = item?.sysPage?.globalSpec  // 悬浮组件
                    /** 处理客服 */
                    let cropUserGroupMap: any[] = []
                    if ((pageElementsSpecList as any[])?.some((item: { elementType: string }) => item?.elementType === 'ENTERPRISE_WX') || (globalSpec?.globalElementsSpecList?.length > 0 && globalSpec?.globalElementsSpecList?.some((item: { floatButtonSpec: { elementType: string } }) => item?.floatButtonSpec?.elementType === 'ENTERPRISE_WX'))) {
                        let groupList: { type: number, name: string, cropList: any[], cropId?: number, groupId?: number }[] = [];
                        (pageElementsSpecList as any[])?.forEach((item: { elementType: string, enterpriseWxSpec: { btnTitle: string } }) => {
                            if (item?.elementType === 'ENTERPRISE_WX') {
                                groupList.push({ type: 1, name: '联系商家', cropList: [] }) // item.enterpriseWxSpec.btnTitle
                            }
                        })
                        if ((globalSpec?.globalElementsSpecList?.length > 0 && globalSpec?.globalElementsSpecList)) {
                            groupList.push({ type: 2, name: '悬浮组件', cropList: [] })
                        }
                        cropUserGroupMap = sorted[0]?.map((item: any) => {
                            let corpUserGroup1s = item?.corpUserGroup1s
                            let corpUserGroup2s = item?.corpUserGroup2s
                            return {
                                adAccountId: item.accountId, id: item.adAccountId, data: groupList.map((crop: any, index: number) => {
                                    return { ...crop, cropList: crop.type === 1 ? corpUserGroup1s[index] ? [{ ...corpUserGroup1s[index], id: corpUserGroup1s[index].groupId }] : [] : corpUserGroup2s[0] ? [{ ...corpUserGroup2s[0], id: corpUserGroup2s[0].groupId }] : []}
                                })
                            }

                        })
                    }
                    let accountPageIdMap: any = {}
                    adCreateLogs?.forEach((item: any) => {
                        if (item?.pageId) {
                            accountPageIdMap[item.accountId] = item?.pageId
                        }
                    })
                    return { sysAdcreative: item?.sysAdcreative, sysPageId: item?.sysPageId, cropUserGroupMap, accountPageIdMap }
                })

                /** 云端落地页处理 */
                let pageList = sorted?.map((item: any[]) => {
                    if (item.some((item1: { sysPageId: number }) => item1.sysPageId)) {
                        return item[0].sysPage
                    } else {
                        return null
                    }
                })
                let adqPageList = sorted?.map((item: any[]) => {
                    if (item.some((item1: { pageId: number }) => item1.pageId)) {
                        return item.map((item1: any) => ({
                            pageList: [{ ...item1.page, id: item1.page.pageId }],
                            adAccountId: item1?.accountId,
                            id: item1?.adAccountId,
                        }))
                    } else {
                        return null
                    }
                })

                setQueryForm({
                    ...queryForm,
                    campaignType,
                    promotedObjectType,
                    speedMode,
                    sysAdgroup,
                    sysAdgroupId,
                    sysTargeting,
                    sysTargetingId,
                    adgroupName: sysAdgroup?.adgroupName,
                    configuredStatus: sysAdgroup?.configuredStatus,
                    expandEnabled: sysAdgroup?.expandEnabled || false,
                    expandTargeting: sysAdgroup?.expandTargeting || [],
                    taskMediaMaps: taskMediaMaps || [],
                    pageList,
                    adqPageList
                })
            })
            sessionStorage.removeItem('TASKID')
        } else {
            let adqAdData = localStorage.getItem('ADQAD')
            if (adqAdData) {
                const { queryForm, accountCreateLogs } = JSON.parse(adqAdData)
                setQueryForm({ ...queryForm })
                setAccountCreateLogs(accountCreateLogs)
            }
        }
    }, [])
    // 设置地域
    useEffect(() => {
        tagsList_REGION.run({ type: 'REGION' }).then(res => {
            if (res && Array.isArray(res)) {
                setGeoLocationList(() => (res as any[])?.reduce((prev: any, cur: { id: number }) => {
                    prev[cur.id] = cur
                    return prev
                }, {}))
            }
        })
        tagsList_MODEL.run({ type: 'DEVICE_BRAND_MODEL' }).then(res => {
            if (res && Array.isArray(res)) {
                setModelList(() => (res as any[])?.reduce((prev: any, cur: { id: number }) => {
                    prev[cur.id] = cur
                    return prev
                }, {}))
            }
        })
    }, [])

    // 获取账户列表
    useEffect(() => {
        getAdAccount.run()
    }, [])

    // 账号对比
    useEffect(() => {
        if (getAdAccount?.data?.data && accountCreateLogs) {
            if (accountCreateLogs.some(item => !getAdAccount?.data?.data?.find((item1: { id: number, accountId: number }) => item.id === item1.id && item.adAccountId == item1.accountId))) {
                let errorData: any[] = []
                let newAccountCreateLogs = accountCreateLogs.filter(item => {
                    let data = getAdAccount?.data?.data?.find((item1: { id: number, accountId: number }) => item.id === item1.id && item.adAccountId == item1.accountId)
                    if (data) {
                        return true
                    } else {
                        errorData.push(item.adAccountId)
                        return false
                    }
                })
                notification.error({
                    duration: 60 * 5,
                    message: '重要提示',
                    description: `本地媒体账户与你所拥有账户对不上，当前创建账号不符合账号及部分相关配置已清空。请把保存在本地的媒体账户或者媒体账户组清空，重新选择保存。问题账户：(${errorData.toString()})`
                })
                setAccountCreateLogs(newAccountCreateLogs)
                setQueryForm({ ...queryForm, adqPageList: [], taskMediaMaps: queryForm?.taskMediaMaps?.map(item => ({ ...item, accountPageIdMap: {} })) })
            }
        }
    }, [getAdAccount?.data, accountCreateLogs, queryForm])

    /** 获取广告详情 */
    useEffect(() => {
        if (getSysAdgroups?.data?.bidMode !== 'BID_MODE_CPM' && accountCreateLogs?.length > 0) {
            let newAccountCreateLogs = accountCreateLogs?.map((item: any) => {
                if (item?.customAudienceList) {
                    delete item?.customAudienceList
                }
                return { ...item }
            })
            setAccountCreateLogs([...newAccountCreateLogs])
        }
    }, [getSysAdgroups?.data?.bidMode])


    /** 删除商品内容 */
    const goodsDel = (index: number) => {
        let newArr = JSON.parse(JSON.stringify(accountCreateLogs))
        delete newArr[index].productList
        setAccountCreateLogs(newArr)
    }

    /** 删除数据源 */
    const sourceDel = (index: number, num: number) => {
        let newArr = JSON.parse(JSON.stringify(accountCreateLogs))
        newArr[index].userActionSetsList?.splice(num, 1)
        setAccountCreateLogs(newArr)
    }

    /** 删除人群包 */
    const cpDel = (index: number, num: number, key: string) => {
        let newArr = JSON.parse(JSON.stringify(accountCreateLogs))
        newArr[index][key]?.splice(num, 1)
        setAccountCreateLogs(newArr)
    }

    // 创意素材与文案叉乘处理
    const whatever = (...arrs: any[]) => {
        console.log('arrs---->', arrs);
        if (arrs[0]?.length && arrs[1]?.length) {
            return arrs.reduce((total, curr) => total.flatMap((e: any) => curr.map((e2: any) => ({ ...e2, ...e }))))
        } else if (arrs[0]?.length) {
            return arrs[0]
        } else if (arrs[1]?.length) {
            return arrs[1]
        } else {
            return ['']
        }
    }


    /** 预览 */
    const preview = () => {
        let newQueryForm: Partial<CreateAdProps> = JSON.parse(JSON.stringify(queryForm))
        if (accountCreateLogs?.length === 0) {
            message.error('请选择媒体账户')
            return
        }
        if (!newQueryForm.promotedObjectType) {
            message.error('请选择推广目标')
            return
        }
        if (!newQueryForm.sysAdgroup) {
            message.error('请先设置广告基本信息')
            return
        }
        if (!newQueryForm.sysTargeting) {
            message.error('请选择定向')
            return
        }
        if (!newQueryForm.taskMediaMaps?.every(item => item.sysAdcreative)) {
            message.error('请设置创意的基本信息')
            return
        }
        if (!newQueryForm.taskMediaMaps?.every(item => item.sysPageId || item.accountPageIdMap)) {
            message.error('请选择落地页')
            return
        }
        if (launchMode === 2) {
            if ((queryForm?.materialData && queryForm?.materialData?.length > 0) && !(newQueryForm?.materials && newQueryForm?.materials?.length > 0)) {
                message.error('请选择创意素材')
                return
            }
            if ((queryForm?.textData && queryForm.textData?.length > 0) && !(newQueryForm?.texts && newQueryForm?.texts?.length > 0)) {
                message.error('请选择创意文案')
                return
            }
        }
        if (newQueryForm?.taskMediaMaps && newQueryForm?.taskMediaMaps?.some((item: { cropUserGroupMap: any[] }) => item?.cropUserGroupMap?.length > 0)) {
            let cropData = newQueryForm?.taskMediaMaps?.filter((item: { cropUserGroupMap: any[] }) => item?.cropUserGroupMap?.length > 0)
            if (cropData?.some((item: { cropUserGroupMap: { data: { cropList: any[] }[] }[] }) => {
                return item?.cropUserGroupMap?.some((item1: { data: { cropList: any[] }[] }) => item1?.data?.some((item2: { cropList: any[] }) => item2?.cropList?.length === 0))
            })) {
                message.error('请完善落地页企微客服组')
                return
            }
        }
        let data: any[] = []
        if (launchMode === 2) {
            if (Array.isArray(newQueryForm.materials) && Array.isArray(newQueryForm?.texts)) {
                let taskMediaMap = JSON.parse(JSON.stringify(newQueryForm.taskMediaMaps[0]))
                let adcreativeElements = taskMediaMap.sysAdcreative?.adcreativeElements || {}
                let newTaskMediaMaps = whatever(newQueryForm.materials, newQueryForm.texts).map((item: any) => {
                    taskMediaMap.sysAdcreative.adcreativeElements = { ...adcreativeElements, ...item }
                    return JSON.parse(JSON.stringify(taskMediaMap))
                })
                newQueryForm.taskMediaMaps = newTaskMediaMaps
            }
        }
        accountCreateLogs.forEach((item: any) => {
            newQueryForm.taskMediaMaps?.forEach((task, index) => {
                let obj = {
                    ...item,
                    ...newQueryForm,
                    sysAdGroupData: newQueryForm.sysAdgroup,
                    targetingData: newQueryForm.sysTargeting,
                    sysAdcreativeData: task.sysAdcreative,
                    pageData: launchMode === 2 ? (newQueryForm.pageList as any)[0] || (newQueryForm.adqPageList as any)[0]?.find((adq: { adAccountId: any }) => adq.adAccountId === item.adAccountId)?.pageList[0] : (newQueryForm.pageList as any)[index] || (newQueryForm.adqPageList as any)[index]?.find((adq: { adAccountId: any }) => adq.adAccountId === item.adAccountId)?.pageList[0],
                    myId: Number(item.id + '' + index)
                }
                data.push(obj)
            })
        })
        setTableData(data)
    }

    const submit = (props: { campaignName: string, count?: number }) => {
        let newQueryForm = JSON.parse(JSON.stringify(queryForm))
        if (launchMode === 2) {
            if (Array.isArray(newQueryForm.materials) && Array.isArray(newQueryForm?.texts)) {
                let taskMediaMap = JSON.parse(JSON.stringify(newQueryForm.taskMediaMaps[0]))
                let adcreativeElements = taskMediaMap.sysAdcreative?.adcreativeElements || {}
                let newTaskMediaMaps = whatever(newQueryForm.materials, newQueryForm.texts).map((item: any) => {
                    if (item) {
                        taskMediaMap.sysAdcreative.adcreativeElements = { ...adcreativeElements, ...item }
                    } else {
                        taskMediaMap.sysAdcreative.adcreativeElements = { ...adcreativeElements }
                    }
                    return JSON.parse(JSON.stringify(taskMediaMap))
                })
                newQueryForm.taskMediaMaps = newTaskMediaMaps
            }
        }
        let newtaskMediaMaps = newQueryForm.taskMediaMaps.map((item1: { cropUserGroupMap?: any[] }) => {
            let { cropUserGroupMap, ...data } = item1
            if (cropUserGroupMap && cropUserGroupMap?.length > 0) {
                let corpUserGroup1Map: any = {}
                let corpUserGroup2Map: any = {}
                cropUserGroupMap.forEach((cropData: { id: number, data: any[] }) => {
                    let cropData1: { corpId: string, groupId: number }[] = []
                    let cropData2: { corpId: string, groupId: number }[] = []
                    cropData?.data.forEach((crop: { type: 1 | 2, cropList: { corpId: string, groupId: number }[] }) => {
                        let cropList = crop.cropList
                        if (crop.type === 1) {
                            cropData1.push({ corpId: cropList[0].corpId, groupId: cropList[0].groupId })
                        } else {
                            cropData2.push({ corpId: cropList[0].corpId, groupId: cropList[0].groupId })
                        }
                    })
                    if (cropData1.length > 0) {
                        corpUserGroup1Map[cropData.id.toString()] = cropData1
                    }
                    if (cropData2.length > 0) {
                        corpUserGroup2Map[cropData.id.toString()] = cropData2
                    }
                })
                return { ...data, corpUserGroup1Map: Object.keys(corpUserGroup1Map)?.length > 0 ? corpUserGroup1Map : null, corpUserGroup2Map: Object.keys(corpUserGroup2Map)?.length > 0 ? corpUserGroup2Map : null }
            }
            return data
        })
        newQueryForm.taskMediaMaps = newtaskMediaMaps
        let params = { ...newQueryForm, ...props }
        console.log(accountCreateLogs)
        let accountLogs = accountCreateLogs.map((item: any, index) => {
            // userActionSetsList 数据源  productList 商品
            let data: any = { adAccountId: item.id, count: props.count || 1 }
            if (item?.userActionSetsList?.length > 0) { // 数据源
                data.userActionSets = item?.userActionSetsList?.map((item: any) => ({ id: item?.id, type: item?.type }))
            }
            if (item?.productList?.length > 0) { // 商品
                data.productId = item?.productList[0].productOuterId
                data.productCatalogId = item?.productList[0].productCatalogId
            }
            if (item?.customAudienceList?.length > 0) {
                data.customAudience = item?.customAudienceList?.map((item: any) => item.id)
            }
            if (item?.excludedCustomAudienceList?.length > 0) {
                data.excludedCustomAudience = item?.excludedCustomAudienceList?.map((item: any) => item.id)
            }
            if (item?.pageList) {
                data.pageId = item?.pageData?.id
            }
            if (item?.coldStartAudienceList?.length > 0) {
                data.coldStartAudience = item?.coldStartAudienceList?.map((item: any) => item.id)
            }
            return data
        })
        if (params?.expandEnabled) {
            params.sysAdgroup.expandEnabled = params?.expandEnabled
            params.sysAdgroup.expandTargeting = []
        }
        if (params?.expandTargeting?.length > 0) {
            params.sysAdgroup.expandTargeting = params?.expandTargeting
        }
        params.accountCreateLogs = accountLogs
        delete params.sysAdgroupId
        delete params.sysAdcreativeId
        delete params.sysTargetingId
        delete params.pageList
        delete params.adqPageList
        delete params.count
        delete params?.expandEnabled
        delete params?.expandTargeting
        delete params?.texts
        delete params?.textData
        delete params?.materialData
        delete params?.materials
        console.log('paramsSubmit====>', params)
        createAdBatch.run(params).then(res => {
            if (res) {
                sessionStorage.setItem('CAMP', props?.campaignName)
                message.success('创建成功')
                window.location.href = '/#/launchSystemNew/launchManage/taskList'
            }
        })
    }

    /** 清除数据 */
    const clearData = () => {
        setTableData([])
        setTableSelect([])
    }

    /** 存为预设 */
    const severBd = () => {
        // queryForm accountCreateLogs
        localStorage.setItem('ADQAD', JSON.stringify({
            queryForm,
            accountCreateLogs
        }))
        message.success('存储成功')
    }

    /** 清除 */
    const delBdPlan = () => {
        localStorage.removeItem('ADQAD')
        setAccountCreateLogs([])
        setQueryForm({
            campaignName: '',  // 计划名称
            campaignType: 'CAMPAIGN_TYPE_NORMAL', // 计划类型 CAMPAIGN_TYPE_NORMAL CAMPAIGN_TYPE_SEARCH 
            promotedObjectType: 'PROMOTED_OBJECT_TYPE_WECHAT_OFFICIAL_ACCOUNT', // 推广目标类型
            speedMode: 'SPEED_MODE_STANDARD', // 投放速度模式
            sysAdgroupId: undefined,  // 广告组内容
            sysTargetingId: undefined,  // 定向包 id
            adgroupName: undefined,  // 广告名称
            configuredStatus: 'AD_STATUS_SUSPEND',  // 广告状态
            sysAdcreativeId: undefined, // 创意ID
            expandEnabled: false,
            expandTargeting: []
        })
    }
    /** 设置落地页 */
    const setPage = (e: any) => {
        let arr: any = queryForm.taskMediaMaps || []
        function setUrl(item: { sysAdcreative: { overrideCanvasHeadOption: string, adcreativeElements: any } }) {
            if (item?.sysAdcreative?.overrideCanvasHeadOption && item?.sysAdcreative?.overrideCanvasHeadOption === "OPTION_CANVAS_OVERRIDE_CREATIVE") {
                let adcreativeElementsNew = { ...item.sysAdcreative.adcreativeElements }
                let obj = e[0].pageSpecsList[0].pageElementsSpecList[0]
                let { topImageSpec, topVideoSpec, topSliderSpec } = obj
                Object.keys(adcreativeElementsNew).forEach(key => {
                    switch (key) {
                        case 'imageUrl'://图素材
                            adcreativeElementsNew[key] = topImageSpec?.imageUrl
                            break;
                        case 'videoUrl'://视频素材
                            adcreativeElementsNew[key] = topVideoSpec?.videoUrl
                            break;
                        case 'imageUrlList'://图素材
                            adcreativeElementsNew[key] = topSliderSpec?.imageUrlList || [topImageSpec?.imageUrl]
                            break;
                        case 'shortVideoStruct'://视频素材
                            adcreativeElementsNew[key] = { shortVideo1Url: topVideoSpec?.videoUrl }
                            break;
                    }
                })
                return { ...item, sysPageId: e[0]?.id, accountPageIdMap: null, sysAdcreative: { ...item.sysAdcreative, adcreativeElements: adcreativeElementsNew } }
            }
            return { ...item, sysPageId: e[0]?.id, accountPageIdMap: null, }
        }
        if (page_checked) {
            arr = queryForm.taskMediaMaps?.map(item => {
                return setUrl(item)
            })
        } else {
            arr[targetKey as string] = setUrl(arr[targetKey as string])
        }
        getPageInfo(arr)
        setSelectImgVisible(false)
    }

    /** 获取落地页详情 */
    const getPageInfo = useCallback((arrList) => {
        console.log('arrList====>', arrList)
        if (arrList && arrList[targetKey]?.sysPageId) {
            get.run({ mediaType: 'PAGE', sysMediaId: arrList[targetKey]?.sysPageId }).then(res => {
                if (!Object.keys(res)?.includes('fail')) {
                    let data = res
                    let pageElementsSpecList = data?.pageSpecsList[0]?.pageElementsSpecList // 内容区
                    let globalSpec = data?.globalSpec  // 悬浮组件
                    let arr: any = queryForm.pageList || []
                    let adqPageArr: any = queryForm.adqPageList || []
                    adqPageArr[targetKey] = null
                    arr[targetKey] = data
                    /** 处理客服 */
                    let cropUserGroupMap: any[] = []
                    if ((pageElementsSpecList as any[])?.some((item: { elementType: string }) => item?.elementType === 'ENTERPRISE_WX') || (globalSpec?.globalElementsSpecList?.length > 0 && globalSpec?.globalElementsSpecList?.some((item: { floatButtonSpec: { elementType: string } }) => item?.floatButtonSpec?.elementType === 'ENTERPRISE_WX'))) {
                        let groupList: { type: number, name: string, cropList: any[], cropId?: number, groupId?: number }[] = [];
                        (pageElementsSpecList as any[])?.forEach((item: { elementType: string, enterpriseWxSpec: { btnTitle: string } }) => {
                            if (item?.elementType === 'ENTERPRISE_WX') {
                                groupList.push({ type: 1, name: '联系商家', cropList: [] }) // item.enterpriseWxSpec.btnTitle
                            }
                        })
                        if ((globalSpec?.globalElementsSpecList?.length > 0 && globalSpec?.globalElementsSpecList)) {
                            groupList.push({ type: 2, name: '悬浮组件', cropList: [] })
                        }
                        cropUserGroupMap = accountCreateLogs?.map((item: any) => ({ adAccountId: item.adAccountId, id: item.id, data: groupList }))
                    }
                    arrList[targetKey].cropUserGroupMap = cropUserGroupMap
                    setQueryForm({ ...queryForm, pageList: arr, taskMediaMaps: arrList, adqPageList: adqPageArr })//设置落地页详情数组
                } else {
                    //清空对应创意中的落地页ID
                    let arr = queryForm.taskMediaMaps || []
                    arr[targetKey].sysPageId = ''
                    setQueryForm({ ...queryForm, taskMediaMaps: arr })
                }
            })
        }
    }, [queryForm, targetKey, accountCreateLogs])
    // 设置云端落地页
    const setAdqPage = useCallback((data) => {
        if (Array.isArray(data) && data.length > 0) {
            let objMap = {}
            data?.forEach(item => {
                objMap[item.adAccountId] = item.pageList[0].pageId
            })
            let arr: any = queryForm.taskMediaMaps || []
            let adqPageArr: any = queryForm.adqPageList || []
            let pageArr: any = queryForm.pageList || []
            adqPageArr[targetKey as string] = data
            pageArr[targetKey as string] = null
            delete arr[targetKey as string]?.cropUserGroupMap
            arr[targetKey as string] = { ...arr[targetKey as string], sysPageId: '', accountPageIdMap: objMap }
            // 重新设置云端数据并清空本地数据
            setQueryForm({ ...queryForm, taskMediaMaps: arr, adqPageList: adqPageArr, pageList: pageArr })
        }
    }, [queryForm, targetKey])
    // tabs新增和删除
    const onEdit = useCallback((targetKey: string | React.MouseEvent<Element, MouseEvent> | React.KeyboardEvent<Element>, action: 'add' | 'remove') => {
        if (queryForm.taskMediaMaps) {
            if (action === 'remove') {
                let arr = queryForm.taskMediaMaps
                let adqPageArr: any = queryForm.adqPageList || []
                let pageArr: any = queryForm.pageList || []
                adqPageArr[targetKey as string] = null
                pageArr[targetKey as string] = null
                arr[targetKey as string] = { ...arr[targetKey as string], sysPageId: '', accountPageIdMap: null }
                setQueryForm({ ...queryForm, taskMediaMaps: arr, pageList: pageArr, adqPageList: adqPageArr })
            }
        }
    }, [queryForm, targetKey])
    // 媒体组更新通知
    const usersChange = useCallback(() => {
        let data = JSON.parse(localStorage.getItem('ADQUSERS' + userId) as any)
        setUsersArr(data)
    }, [])

    // 切换投放模式
    const switchLaunchMode = () => {
        if (launchMode === 1) {
            setLaunchMode(2)
            localStorage.setItem('LAUNCHMODE', '2')
        } else {
            setLaunchMode(1)
            localStorage.setItem('LAUNCHMODE', '1')
        }
        delBdPlan()
        set_targetKey('0')
    }

    console.log('queryForm111111---->', queryForm, accountCreateLogs);

    return <Space direction="vertical" style={{ width: '100%' }}>
        <Card
            title={<Space>
                <div className={style.cardTitle}>配置区</div>
                <Popconfirm
                    title="数据部分不会保存，是否切换?"
                    onConfirm={switchLaunchMode}
                    okText="是"
                    cancelText="否"
                >
                    <Button type="link" style={{ padding: 0 }}>切换投放模式</Button>
                </Popconfirm>
            </Space>}
            className={style.createAd}
            hoverable
            extra={<AddGroup onChange={usersChange} pitcherData={getAdAccount?.data?.data} />}
        >
            <Space>
                <Selector label="媒体账户组">
                    <Select
                        mode="multiple"
                        style={{ minWidth: 200 }}
                        placeholder="快捷选择媒体账户组"
                        maxTagCount={1}
                        allowClear
                        bordered={false}
                        filterOption={(input: any, option: any) => {
                            return option!.children?.toString().toLowerCase().includes(input.toLowerCase())
                        }}
                        onChange={(e, option) => {
                            console.log(e, option)
                            let userArr: any[] = []
                            e.forEach((key: any) => {
                                let obj = usesArr.find((item: { id: any }) => item.id === key)
                                if (obj) {
                                    userArr.push(obj['pitcher'])
                                }
                            })
                            userArr = [...new Set(userArr.flat())]
                            setAccountCreateLogs(userArr?.map((item: any) => ({ adAccountId: item?.split('_')[1], id: Number(item?.split('_')[0]) })))
                            clearData()
                        }}
                    >
                        {usesArr?.map((item: any) => <Select.Option value={item.id} key={item.id}>{item.name}</Select.Option>)}
                    </Select>
                </Selector>
                <Selector label="媒体账户">
                    <Select
                        mode="multiple"
                        style={{ minWidth: 200 }}
                        placeholder="请选择媒体账户"
                        maxTagCount={1}
                        allowClear
                        bordered={false}
                        filterOption={(input: any, option: any) => {
                            return option!.children?.toString().toLowerCase().includes(input.toLowerCase())
                        }}
                        value={accountCreateLogs?.map((item: { id: number }) => item?.id)}
                        onChange={(e, option) => {
                            console.log(option)
                            setQueryForm({ ...queryForm, adqPageList: [], pageList: [], taskMediaMaps: queryForm?.taskMediaMaps?.map((item: { sysPageId: number }) => ({ ...item, sysPageId: '', accountPageIdMap: {}, cropUserGroupMap: [] })) })
                            setAccountCreateLogs(option?.map((item: any) => ({ adAccountId: item?.children?.toString()?.split('——')[0], id: item?.value })))
                            clearData()
                        }}
                    >
                        {getAdAccount?.data?.data?.map((item: any) => <Select.Option value={item.id} key={item.id}>{item.remark ? item.accountId + '——' + item.remark : item.accountId}</Select.Option>)}
                    </Select>
                </Selector>
                <Selector label="推广目标">
                    <Select style={{ width: 200 }} value={queryForm?.promotedObjectType} placeholder="请选择推广目标" bordered={false} showSearch filterOption={(input: any, option: any) =>
                        (option!.children as unknown as string).toLowerCase().includes(input.toLowerCase())
                    } onChange={(e) => { setQueryForm({ ...queryForm, promotedObjectType: e, sysAdgroup: null, sysAdgroupId: undefined, taskMediaMaps: [], sysAdcreativeId: undefined, materials: [], textData: [], texts: [] }); clearData() }}>
                        {Object.keys(PromotedObjectType).map(key => {
                            return <Select.Option value={key} key={key}>{PromotedObjectType[key]}</Select.Option>
                        })}
                    </Select>
                </Selector>
                {launchMode === 2 && accountCreateLogs?.length > 0 && <>
                    <Button onClick={() => { setGoodsVisible(true) }}>商品广告（选填）{accountCreateLogs?.some(item => item?.productList?.length) && <CheckOutlined style={{ color: '#1890ff' }} />}</Button>
                    <Button onClick={() => { setSourceVisible(true) }}>精准匹配归因（选填）{accountCreateLogs?.some(item => item?.userActionSetsList?.length) && <CheckOutlined style={{ color: '#1890ff' }} />}</Button>
                </>}
            </Space>

            <div className={style.cardBody}>
                <Row className={style.content}>
                    <Col span={launchMode === 1 ? 12 : 8} className={style.conLeft}>
                        <Row className={`${style.conTitle} ${style.conRightBorder}`}><Col span={24}>广告</Col></Row>
                        <Row className={style.items}>
                            {/* =============广告基本信息=========== */}
                            <Ad queryForm={queryForm} setQueryForm={setQueryForm} getSysAdgroups={getSysAdgroups} clearData={clearData} />
                            {/* =============定向包=========== */}
                            <TargetIng
                                queryForm={queryForm}
                                setQueryForm={setQueryForm}
                                getSysAdgroups={getSysAdgroups}
                                clearData={clearData}
                                setAccountCreateLogs={setAccountCreateLogs}
                                getsysTargeting={getsysTargeting}
                                geoLocationList={geoLocationList}
                                modelList={modelList}
                                cpDel={cpDel}
                                accountCreateLogs={accountCreateLogs}
                            />

                            {launchMode === 1 && <>
                                {/* =============商品=========== */}
                                <Col className={style.conRightBorder} span={5}>
                                    <div className={style.top}>
                                        商品
                                    </div>
                                    <div className={style.center}>
                                        <div className={style.centerContent}>
                                            {accountCreateLogs?.map((item: any, index: number) => {
                                                if (item?.productList) {
                                                    return <div className={style.acc} key={index}>
                                                        <div className={style.accName} style={{ fontWeight: 800 }}>{item.adAccountId}</div>
                                                        {
                                                            item?.productList?.map((pack: { productName: string, author: string, id: number }, index: number) => {
                                                                return <div className={style.accCon} key={pack.id}>{pack.productName}<CloseOutlined className={style.close} onClick={() => {
                                                                    goodsDel(index)
                                                                }} /></div>
                                                            })
                                                        }
                                                    </div>
                                                } else {
                                                    return null
                                                }
                                            })}
                                        </div>
                                    </div>
                                    <div className={style.bottom}>
                                        {accountCreateLogs?.length > 0 ? <span onClick={() => { setGoodsVisible(true) }}>编辑</span> : <Tooltip title="请先选择媒体账户">
                                            <span>编辑</span>
                                        </Tooltip>}
                                    </div>
                                </Col>

                                {/* 数据源 */}
                                <Col className={style.conRightBorder} span={5}>
                                    <div className={style.top}>
                                        数据源
                                    </div>
                                    <div className={style.center}>
                                        <div className={style.centerContent}>
                                            {accountCreateLogs?.map((item: any, index: number) => {
                                                if (item?.userActionSetsList && item?.userActionSetsList?.length > 0) {
                                                    return <div className={style.acc} key={index}>
                                                        <div className={style.accName} style={{ fontWeight: 800 }}>{item.adAccountId}</div>
                                                        {
                                                            item?.userActionSetsList?.map((pack: { name: string, type: string, id: number }, index1: number) => {
                                                                return <div className={style.accCon} key={pack.id}> <span className={style.title}>{pack.name}{' > '}{pack.type?.replace('USER_ACTION_SET_TYPE_', '')}</span> <CloseOutlined className={style.close} onClick={() => {
                                                                    sourceDel(index, index1)
                                                                }} /></div>
                                                            })
                                                        }
                                                    </div>
                                                } else {
                                                    return null
                                                }
                                            })}
                                        </div>
                                    </div>
                                    <div className={style.bottom}>
                                        {accountCreateLogs?.length > 0 ? <span onClick={() => { setSourceVisible(true) }}>编辑</span> : <Tooltip title="请先选择媒体账户">
                                            <span>编辑</span>
                                        </Tooltip>}
                                    </div>
                                </Col>
                            </>}
                        </Row>
                    </Col>
                    {/* =============广告创意=========== */}
                    {launchMode === 1 ? <Col span={12} className={style.conRight}>
                        <Row className={style.conTitle}><Col span={24}>广告创意</Col></Row>
                        <Row className={style.items}>
                            {/* 创意 */}
                            <Creative queryForm={queryForm} setQueryForm={setQueryForm} getSysAdgroups={getSysAdgroups} clearData={clearData} getSysAdcreative={getSysAdcreative} set_targetKey={set_targetKey} targetKey={targetKey} page_checked={page_checked} />
                            {/* 落地页 */}
                            <Col span={12} >
                                <div className={style.top}>
                                    落地页
                                    {(queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.cropUserGroupMap?.length > 0) && <CustomerServiceModal data={queryForm?.taskMediaMaps[targetKey]?.cropUserGroupMap} onChange={(data) => {
                                        let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                                        newQueryForm.taskMediaMaps[targetKey].cropUserGroupMap = data
                                        setQueryForm(newQueryForm)
                                    }} />}
                                </div>
                                <div className={style.center}>
                                    <Tabs size={'small'} onEdit={onEdit} type="editable-card" activeKey={targetKey} onChange={(key) => { set_targetKey(key) }} hideAdd >
                                        {
                                            queryForm?.taskMediaMaps?.map((item, index) => {
                                                return <Tabs.TabPane tab={'创意' + (index + 1)} key={index} >
                                                    <Spin spinning={get.loading}>
                                                        <div className={style.centerContent}>
                                                            {
                                                                item?.sysPageId || item?.accountPageIdMap ? <>
                                                                    {
                                                                        (item?.sysPageId && queryForm?.pageList) && <>
                                                                            <div>落地页名称：{queryForm?.pageList[targetKey]?.pageName || ''}</div>
                                                                            <div>分享名称：{queryForm?.pageList[targetKey]?.shareContentSpec?.shareTitle || ''}</div>
                                                                            <div>分享描述：{queryForm?.pageList[targetKey]?.shareContentSpec?.shareDescription || ''}</div>
                                                                            <div style={{ marginBottom: 10 }}>原生推广页顶部素材预览：
                                                                                <div>{queryForm?.pageList[targetKey]?.pageSpecsList && queryForm?.pageList[targetKey]?.pageSpecsList[0]?.pageElementsSpecList?.filter((item: any, index: number) => index === 0)?.map((item: { elementType: 'TOP_IMAGE' | 'TOP_VIDEO' | 'TOP_SLIDER', topImageSpec: any, topSliderSpec: any, topVideoSpec: any }, index: number) => {
                                                                                    switch (item?.elementType) {
                                                                                        case 'TOP_IMAGE':
                                                                                            return <Image width={80} src={item?.topImageSpec?.imageUrl} style={{ borderRadius: 8, overflow: 'hidden' }} key={index} />
                                                                                        case 'TOP_SLIDER':
                                                                                            return <Space wrap key={index}>
                                                                                                {item?.topSliderSpec?.imageUrlList?.map((url: string, index: number) => <Image width={70} src={url} style={{ borderRadius: 8 }} key={'TOP_SLIDER' + index} />)}
                                                                                            </Space>
                                                                                        case 'TOP_VIDEO':
                                                                                            return <video src={item?.topVideoSpec?.videoUrl} width={150} controls key={index}></video>
                                                                                    }
                                                                                })}</div>
                                                                            </div>
                                                                        </>
                                                                    }
                                                                    {
                                                                        queryForm?.adqPageList && queryForm?.adqPageList[targetKey]?.map((adq: any) => {
                                                                            return <div className={style.acc} key={adq.adAccountId}>
                                                                                <div className={style.accName} style={{ fontWeight: 800 }}>{adq.adAccountId}</div>
                                                                                <div className={style.accCon}>
                                                                                    <span className={style.title}>{adq.pageList[0].pageName}</span>
                                                                                </div>
                                                                            </div>
                                                                        })
                                                                    }
                                                                </> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />}
                                                        </div>
                                                    </Spin>
                                                </Tabs.TabPane>
                                            })
                                        }
                                    </Tabs>
                                </div>
                                <div className={style.bottom}>{
                                    (queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysAdcreative) ? <>
                                        {queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysPageId && <Button type="link" onClick={() => { setLookVisible(true) }}>查看</Button>}
                                        <Button type="link" onClick={() => {
                                            setSelectImgVisible(true)
                                            // 判定是否用原生页顶部替换外部素材
                                            if (queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysAdcreative?.overrideCanvasHeadOption === 'OPTION_CANVAS_OVERRIDE_CREATIVE') {
                                                init({ mediaType: 'PAGE', cloudSize: undefined, adcreativeTemplateId: queryForm?.taskMediaMaps[targetKey]?.sysAdcreative?.adcreativeTemplateId })
                                            } else {
                                                init({ mediaType: 'PAGE', cloudSize: undefined })
                                            }
                                        }}>{queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysPageId ? '修改' : '选择落地页'}</Button>

                                        {accountCreateLogs?.length > 0 ? <Button type="link" onClick={() => {
                                            setPageVisible(true)
                                            if (queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysAdcreative?.overrideCanvasHeadOption === 'OPTION_CANVAS_OVERRIDE_CREATIVE') {
                                                setCloudParams({ adcreativeTemplateId: queryForm?.taskMediaMaps[targetKey]?.sysAdcreative?.adcreativeTemplateId })
                                            } else {
                                                setCloudParams({})
                                            }
                                        }}>云端落地页</Button> : <Tooltip title="请先选择媒体账户">
                                            <Button type="link">云端落地页</Button>
                                        </Tooltip>}
                                    </> : <Tooltip title="请先设置创意">
                                        <Button type="link"><span>选择落地页</span></Button>
                                    </Tooltip>}
                                </div>
                            </Col>
                        </Row>
                    </Col> : <Col span={16} className={style.conRight}>
                        <Row className={style.conTitle}><Col span={24}>广告创意</Col></Row>
                        <Row className={style.items}>
                            {/* 创意 */}
                            <CreativeCL queryForm={queryForm} setQueryForm={setQueryForm} clearData={clearData} getSysAdcreative={getSysAdcreative} targetKey={targetKey} />
                            {/* 落地页 */}
                            <Col className={style.conRightBorder} style={{ maxWidth: '25%', border: 'none' }}>
                                <div className={style.top}>
                                    落地页
                                    {(queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.cropUserGroupMap?.length > 0) && <CustomerServiceModal data={queryForm?.taskMediaMaps[targetKey]?.cropUserGroupMap} onChange={(data) => {
                                        let newQueryForm = JSON.parse(JSON.stringify(queryForm))
                                        newQueryForm.taskMediaMaps[targetKey].cropUserGroupMap = data
                                        setQueryForm(newQueryForm)
                                    }} />}
                                </div>
                                <div className={style.center}>
                                    {queryForm?.taskMediaMaps?.filter((item, index) => index === 0)?.map((item, index) => {
                                        return <Spin spinning={get.loading} key={index}>
                                            <div className={style.centerContent}>
                                                {item?.sysPageId || item?.accountPageIdMap ? <>
                                                    {(item?.sysPageId && queryForm?.pageList) && <>
                                                        <div>落地页名称：{queryForm?.pageList[targetKey]?.pageName || ''}</div>
                                                        <div>分享名称：{queryForm?.pageList[targetKey]?.shareContentSpec?.shareTitle || ''}</div>
                                                        <div>分享描述：{queryForm?.pageList[targetKey]?.shareContentSpec?.shareDescription || ''}</div>
                                                        <div style={{ marginBottom: 10 }}>原生推广页顶部素材预览：
                                                            <div>{queryForm?.pageList[targetKey]?.pageSpecsList && queryForm?.pageList[targetKey]?.pageSpecsList[0]?.pageElementsSpecList?.filter((item: any, index: number) => index === 0)?.map((item: { elementType: 'TOP_IMAGE' | 'TOP_VIDEO' | 'TOP_SLIDER', topImageSpec: any, topSliderSpec: any, topVideoSpec: any }, index: number) => {
                                                                switch (item?.elementType) {
                                                                    case 'TOP_IMAGE':
                                                                        return <Image width={80} src={item?.topImageSpec?.imageUrl} style={{ borderRadius: 8, overflow: 'hidden' }} key={index} />
                                                                    case 'TOP_SLIDER':
                                                                        return <Space wrap key={index}>
                                                                            {item?.topSliderSpec?.imageUrlList?.map((url: string, index: number) => <Image width={70} src={url} style={{ borderRadius: 8 }} key={'TOP_SLIDER' + index} />)}
                                                                        </Space>
                                                                    case 'TOP_VIDEO':
                                                                        return <video src={item?.topVideoSpec?.videoUrl} width={150} controls key={index}></video>
                                                                }
                                                            })}</div>
                                                        </div>
                                                    </>}
                                                    {queryForm?.adqPageList && queryForm?.adqPageList[targetKey]?.map((adq: any) => {
                                                        return <div className={style.acc} key={adq.adAccountId}>
                                                            <div className={style.accName} style={{ fontWeight: 800 }}>{adq.adAccountId}</div>
                                                            <div className={style.accCon}>
                                                                <span className={style.title}>{adq.pageList[0].pageName}</span>
                                                            </div>
                                                        </div>
                                                    })}
                                                </> : <Empty image={Empty.PRESENTED_IMAGE_SIMPLE} />}
                                            </div>
                                        </Spin>
                                    })}
                                </div>
                                <div className={style.bottom}>{
                                    (queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysAdcreative) ? <>
                                        {queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysPageId && <Button type="link" onClick={() => { setLookVisible(true) }}>查看</Button>}
                                        <Button type="link" onClick={() => {
                                            setSelectImgVisible(true)
                                            // 判定是否用原生页顶部替换外部素材
                                            if (queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysAdcreative?.overrideCanvasHeadOption === 'OPTION_CANVAS_OVERRIDE_CREATIVE') {
                                                init({ mediaType: 'PAGE', cloudSize: undefined, adcreativeTemplateId: queryForm?.taskMediaMaps[targetKey]?.sysAdcreative?.adcreativeTemplateId })
                                            } else {
                                                init({ mediaType: 'PAGE', cloudSize: undefined })
                                            }
                                        }}>{queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysPageId ? '修改' : '选择落地页'}</Button>

                                        {accountCreateLogs?.length > 0 ? <Button type="link" onClick={() => {
                                            setPageVisible(true)
                                            if (queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey]?.sysAdcreative?.overrideCanvasHeadOption === 'OPTION_CANVAS_OVERRIDE_CREATIVE') {
                                                setCloudParams({ adcreativeTemplateId: queryForm?.taskMediaMaps[targetKey]?.sysAdcreative?.adcreativeTemplateId })
                                            } else {
                                                setCloudParams({})
                                            }
                                        }}>云端落地页</Button> : <Tooltip title="请先选择媒体账户">
                                            <Button type="link">云端落地页</Button>
                                        </Tooltip>}
                                    </> : <Tooltip title="请先设置创意">
                                        <Button type="link"><span>选择落地页</span></Button>
                                    </Tooltip>}
                                </div>
                            </Col>
                        </Row>
                    </Col>}

                </Row>
                {/* =============广告底部按钮=========== */}
                <Space className={style.bts}>
                    <Button type='primary' onClick={severBd}>存为预设</Button>
                    <Button type='primary' onClick={preview}><SearchOutlined /> 批量预览广告</Button>
                    <Button onClick={delBdPlan}>清空配置/预设</Button>
                </Space>
            </div>
        </Card>

        <Card
            className={style.createAd}
            hoverable
            extra={tableData?.length > 0 ? <Space>
                <span>推广计划总数：{tableData?.length}</span>
                <span>广告总数：{tableData?.length}</span>
                {/* {tableSelect?.length > 0 && <span> 已选：<span style={{ color: '#1890FF' }}>{tableSelect?.length}</span> 条</span>} */}
                {
                    <Button type='primary' onClick={() => {
                        // if (tableSelect.length === 0) {
                        //     message.error('请选择要提交的计划！')
                        //     return
                        // };
                        setSubVisible(true)
                    }}>批量提交审核</Button>
                }
            </Space> : false
            }
        >
            {tableData?.length > 0 ? <div className={style.cardBody}>
                <div className={style.content} style={{ marginTop: 20 }}>
                    <Tables
                        columns={columns()}
                        dataSource={tableData}
                        total={0}
                        size="small"
                        bordered
                        scroll={{ x: 2000 }}
                        myKey={'myId'}
                    // rowSelection={{
                    //     selectedRowKeys: tableSelect?.map((item: any) => item?.myId.toString()),
                    //     onChange: (selectedRowKeys: React.Key[], selectedRows: any) => {
                    //         setTableSelect(selectedRows)
                    //     }
                    // }}
                    />
                </div>
            </div> : <div style={{ minHeight: 400, display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
                <Empty description="请先完成模块配置后，再预览广告计划" />
            </div>}
        </Card>


        {/* 选择商品 */}
        {goodsVisible && <GoodsModal visible={goodsVisible} data={accountCreateLogs} onClose={() => setGoodsVisible(false)} onChange={(e) => { setAccountCreateLogs(e); setGoodsVisible(false); clearData() }} />}
        {/* 选择数据源 */}
        {sourceVisible && <DataSourceModal visible={sourceVisible} promotedObjectType={queryForm.promotedObjectType} data={accountCreateLogs} onClose={() => setSourceVisible(false)} onChange={(e) => { setAccountCreateLogs(e); setSourceVisible(false); clearData() }} />}
        {/* 选择转化ID */}
        {idVisible && <IdModal visible={idVisible} data={accountCreateLogs} onClose={() => setIdVisible(false)} onChange={(e) => { setAccountCreateLogs(e); setSourceVisible(false); clearData() }} />}
        {/* 选择ADQ落地页 */}
        {pageVisible && <PageModal cloudParams={cloudParams} visible={pageVisible} data={accountCreateLogs} onClose={() => setPageVisible(false)} onChange={(e) => { setAdqPage(e); setPageVisible(false); clearData() }} />}
        {/* 选择素材 */}
        {selectImgVisible && <SelectCloud visible={selectImgVisible} onClose={() => setSelectImgVisible(false)} onChange={setPage} isBack={false} />}
        {/* 查看落地页 */}
        {lookVisible && <LookLanding visible={lookVisible} onClose={() => setLookVisible(false)} id={queryForm?.taskMediaMaps && queryForm?.taskMediaMaps[targetKey].sysPageId} />}
        {/* 设置名称 */}
        {subVisible && <SubmitModal data={getSysAdgroups?.data} visible={subVisible} onClose={() => setSubVisible(false)} onChange={submit} ajax={createAdBatch} />}
    </Space>
}


export default CreateAd