import { useAjax } from "@/Hook/useAjax"
import { newEditAdqAdgroupsDataApi } from "@/services/launchAdq/adq"
import { message, notification, Switch } from "antd"
import React from "react"



/**
 * 修改启停
 */
interface Props {
    configuredStatus: string,
    isDeleted?: boolean,
    adgroupId: number,
    onChange?: () => void
}
const SwitchStatus: React.FC<Props> = (prosp) => {

    const { configuredStatus, isDeleted, adgroupId, onChange } = prosp
    const editAdqAdgroupsData = useAjax((params) => newEditAdqAdgroupsDataApi(params))
    
    const switchHandle = (adgroupIds: number[], configuredStatus: string) => {
        editAdqAdgroupsData.run({ adgroupIds, configuredStatus }).then(res => {
            message.success(`${configuredStatus === 'AD_STATUS_NORMAL' ? '启动' : '暂停'}成功`)
            if (res?.fail) {
                notification.error({
                    message: `${configuredStatus === 'AD_STATUS_NORMAL' ? '启动' : '暂停'}失败`,
                    description: `修改失败${res.fail}条,失败的请到任务列表查看`,
                    duration: 0
                });
            }
            onChange?.()
        })
    }

    return <Switch checked={configuredStatus === 'AD_STATUS_NORMAL'} loading={editAdqAdgroupsData.loading} disabled={isDeleted} onChange={(checked) => switchHandle([adgroupId] ,checked ? 'AD_STATUS_NORMAL' : 'AD_STATUS_SUSPEND')}/>
}


export default React.memo(SwitchStatus)