import { useAjax } from "@/Hook/useAjax"
import { configEnableApi } from "@/services/gameData/adlist"
import { Switch, message } from "antd"
import React, { useEffect, useState } from "react"


interface Props {
    value: any
}
const EnableConfig: React.FC<Props> = ({ value }) => {

    const configEnable = useAjax((params) => configEnableApi(params))
    const [checked, setChecked] = useState<boolean>(value.isStart)

    useEffect(() => {
        setChecked(value.isStart)
    }, [value.isStart])

    const onChange = (c: boolean) => {
        const { id } = value
        configEnable.run({ isStart: c, ids: [id] }).then(res => {
            if (res) {
                setChecked(c)
                message.success(c ? '启用成功' : '暂停成功')
            }
        })
    }

    return <Switch size="small" checked={checked} onChange={onChange} loading={configEnable.loading} />
}

export default React.memo(EnableConfig)