import React, { useEffect, useCallback } from 'react'
import { Col, Input, message, Row } from 'antd'
import { useAjax } from '@/Hook/useAjax'
import { getAdqTargetingList, putAdqTargetingSyncAll } from '@/services/launchAdq/adq'
import TableData from '../../components/TableData'
import tableConfig from './tableConfig'

function Targeting(props: { accountId: string, adAccountId: string }) {
    let { accountId, adAccountId } = props
    const listAjax = useAjax((params) => getAdqTargetingList(params), { formatResult: true })
    const syncAjax = useAjax((adAccountId) => putAdqTargetingSyncAll(adAccountId))
    useEffect(() => {
        getList({ pageNum: 1, pageSize: 20 })
    }, [accountId])
    // 获取列表
    const getList = useCallback((params: {
        pageNum: number;
        pageSize: number;
        accountId?: string;
        pageName?: string;
        pageType?: string;
        pageTemplateId?: string;
        pageStatus?: string;
    }) => {
        accountId && listAjax.run({ ...params, accountId })
    }, [accountId])
    // 同步 
    const sync = useCallback(() => {
        syncAjax.run(adAccountId).then(res => {
            console.log(res)
            res && listAjax.refresh()
            res? message.success('同步成功！') : message.error('同步失败！')

        })
    }, [adAccountId, listAjax])
    return <div>
        <TableData
            columns={tableConfig}
            ajax={listAjax}
            syncAjax={sync}
            dataSource={listAjax?.data?.data?.records}
            loading={listAjax?.loading || syncAjax?.loading}
            scroll={{ x: 2000 }}
            total={listAjax?.data?.data?.total}
            page={listAjax?.data?.data?.current}
            pageSize={listAjax?.data?.data?.size}
            myKey={'targetingId'}
            leftChild={<>
                <Row gutter={[10, 10]}>
                    <Col>
                        <Input
                            placeholder='定向名称'
                            allowClear
                            onBlur={(e) => {
                                let value = e.target.value
                                getList({ pageNum: 1, pageSize: 20 })
                            }}
                            onKeyDownCapture={(e: any) => {
                                let key = e.key
                                if (key === 'Enter') {
                                    let value = e.target.value
                                    getList({ pageNum: 1, pageSize: 20 })
                                }
                            }}
                            onChange={(e) => {
                                let value = e.target.value
                                if (!value) {
                                    getList({ pageNum: 1, pageSize: 20 })
                                }
                            }}
                        />
                    </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, accountId})
                        }}>
                            {
                                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 })
            }}
        />
    </div>
}
export default Targeting