
import { FnAjax, useAjax } from '@/Hook/useAjax'
import { ListData, } from '@/services/launchAdq'
import { PromotedObjectType } from '@/services/launchAdq/enum'
import { createSysAdcreative, getSysAdcreativeInfo, getSysAdcreativeList } from '@/services/launchAdq/creative'
import { Col, Row, Input, Select, Button } from 'antd'
import React, { useEffect, useState, useCallback } from 'react'
import TableData from '../../../components/TableData'
import AdModal from './modal'
import tableConfig from './tableConfig'

export interface ModalConfig {
    visible: boolean;
    type?:'add'|'look'|'edit',
    dataInfo?:null
}

function Creative() {
    // 变量
    const [modalConfig, setModalConfig] = useState<ModalConfig>({
        visible: false,
        type:'add'
    })
    const [oldsearchData, setOldsearchData] = useState<any>(null)
    // api方法
    const sysAdcreativeList: FnAjax<ListData<any> | any> = useAjax((params) => getSysAdcreativeList(params), { formatResult: true })
    const createSysAdgroup = useAjax((params) => createSysAdcreative(params))
    const targetingInfo = useAjax((params) => getSysAdcreativeInfo(params),{formatResult:true})
    // 初始获取列表
    useEffect(() => {
        getList({ pageSize: 20, pageNum: 1 })
    }, [])
    // 获取列表
    const getList = useCallback((arg: { pageSize: number, pageNum: number, adcreativeName?: string, promotedObjectType?: string }) => {
        Object.keys(arg).forEach(key => {
            !arg[key] && delete arg[key]
        })
        if (JSON.stringify(arg) !== JSON.stringify(oldsearchData)) {
            setOldsearchData(arg)
            sysAdcreativeList.run(arg)
        }
    }, [oldsearchData])

    // 设置变量
    const handleModalConfig = useCallback((arg: ModalConfig) => {
        setModalConfig({ ...modalConfig, ...arg })
    }, [modalConfig])
    // submit
    const submit = useCallback((arg: any) => {
        console.log(arg)
        createSysAdgroup.run(arg).then(res => {
            if (res) {
                sysAdcreativeList.refresh()
                handleModalConfig({ visible: false,dataInfo:null,type:'add' })
            }
        })
    }, [sysAdcreativeList])
    // 获取详情
    const getInfo = useCallback((adgroupsId, type) => {
        targetingInfo.run(adgroupsId).then((res: any) => {
            setModalConfig({ ...modalConfig, visible: true, type, dataInfo: res.data })
        })
    }, [modalConfig])
    // 回调
    const callback = useCallback((data, type) => {
        getInfo(data.id, type)

    }, [])
    return <div>
        <TableData
            columns={()=>tableConfig(callback)}
            ajax={sysAdcreativeList}
            dataSource={sysAdcreativeList?.data?.data?.records}
            loading={sysAdcreativeList?.loading}
            // scroll={{ y: 600 }}
            total={sysAdcreativeList?.data?.data?.total}
            page={sysAdcreativeList?.data?.data?.current}
            pageSize={sysAdcreativeList?.data?.data?.size}
            leftChild={<>
                <Row gutter={[10, 10]}>
                    {/* <Col span={24}><Button type='primary' onClick={() => {
                        handleModalConfig({ visible: true })
                    }}>新建创意模板</Button></Col> */}
                    <Col>
                        <Input
                            placeholder='创意名称'
                            allowClear
                            onBlur={(e) => {
                                let value = e.target.value
                                getList({ pageNum: 1, pageSize: 20, adcreativeName: value })
                            }}
                            onKeyDownCapture={(e: any) => {
                                let key = e.key
                                if (key === 'Enter') {
                                    let value = e.target.value
                                    getList({ pageNum: 1, pageSize: 20, adcreativeName: value })
                                }
                            }}
                            onChange={(e) => {
                                let value = e.target.value
                                if (!value) {
                                    getList({ pageNum: 1, pageSize: 20, adcreativeName: value })
                                }
                            }}
                        />
                    </Col>
                    <Col>
                        <Select placeholder='推广目标选择' style={{ minWidth: 200 }} showSearch filterOption={(input: any, option: any) =>
                            (option!.children as unknown as string).toLowerCase().includes(input.toLowerCase())
                        } allowClear onChange={(value: any) => {
                            getList({ pageNum: 1, pageSize: 20, promotedObjectType: value as any })
                        }}>
                            {
                                Object.keys(PromotedObjectType).map(key => {
                                    // let obj = JSON.parse(PromotedObjectType[key])
                                    return <Select.Option value={key} key={key}>{PromotedObjectType[key]}</Select.Option>
                                })
                            }
                        </Select>
                    </Col>
                </Row>
            </>}
            onChange={(props: any) => {
                let { sortData, pagination } = props
                let { current, pageSize } = pagination
                getList({ pageNum: current, pageSize })
            }}
        />
        {modalConfig.visible && <AdModal visible={modalConfig.visible} type={modalConfig.type} PupFn={handleModalConfig} callback={submit} confirmLoading={createSysAdgroup.loading} dataInfo={modalConfig.dataInfo}/>}
    </div>
}
export default Creative