import React from 'react';
import { Switch } from 'antd';
import { useAjax } from '@/Hook/useAjax';
import { closeSwitchUpdateApi } from '@/services/adqV3/global';

interface CloseSwitchProps {
    id: number;
    value?: boolean;
    onChange?: () => void;
}

const CloseSwitch: React.FC<CloseSwitchProps> = ({ id, value, onChange }) => {

    /***************************************/
    const closeSwitchUpdate = useAjax((params) => closeSwitchUpdateApi(params))
    /***************************************/

    const handleSwitch = (checked: boolean) => {
        closeSwitchUpdate.run({ id, closeSwitch: !checked }).then(res => {
            if (res) {
                onChange?.()
            }
        })
    }

    return (
        <Switch
            size='small'
            checked={!value}
            onChange={(e) => handleSwitch(e)}
            loading={closeSwitchUpdate.loading}
        />
    );
};

export default CloseSwitch;