import { useAjax } from "@/Hook/useAjax"
import { authSyncApi, openAllV3Api } from "@/services/launchAdq/adq"
import { LoadingOutlined, SyncOutlined } from "@ant-design/icons"
import { Button, Space, Tooltip, message } from "antd"
import React from "react"


interface Props {
    adUnitAccount: boolean,
    accountId: any
    onChange?: () => void
}
const OpenV3All: React.FC<Props> = ({ accountId, onChange, adUnitAccount }) => {

    /*************************************/
    const openAllV3a = useAjax((params) => openAllV3Api(params), { formatResult: true })
    const authSync = useAjax((params) => authSyncApi(params), { formatResult: true })
    /*************************************/

    const openAllV3 = () => {
        openAllV3a.run(accountId).then(res => {
            if (res?.data) {
                message.success('开启成功')
                onChange?.()
            }
        })
    }
    const asyncAllV3 = () => {
        authSync.run({ accountId }).then(res => {
            if (res?.data) {
                message.success('刷新成功')
                onChange?.()
            }
        })
    }

    return <Space>
        {adUnitAccount && <Tooltip title="广告业务单元广告主账号全量转3.0">
            <Button size="small" style={{ color: "#0eb83a" }} onClick={openAllV3}>{openAllV3a.loading ? <LoadingOutlined /> : <span>全3.0</span>}</Button>
        </Tooltip>}
        <Tooltip title="广告主账号同步">
            <Button size="small" style={{ color: "#ea5506" }} onClick={asyncAllV3} loading={authSync.loading} icon={<SyncOutlined />}></Button>
        </Tooltip>
    </Space>
}

export default React.memo(OpenV3All)