import {getHeaders} from './index';
import {ContractClosePosition, onContractRevoke} from './contract';
import {show_message, open_loading} from "../tdesign";
import {loading} from 'tdesign-react';
import {SpotCancel} from './spot';

/**
 * 复用的接口
 */

// 合约平仓
export const ClosePosition = async (data: any, needLoading: boolean = true) => {
    try {
        let loadingInstance: any = null;
        if (needLoading) {
            loadingInstance = open_loading();
        }
        const res: any = await ContractClosePosition(data);
        if (needLoading && loadingInstance) {
            loadingInstance.hide()
        }
        if (res.code == 0) {
            show_message('平仓成功', 'success');
            // 刷新页面
            window.location.reload();
        }
    } catch (e) {
        console.log(e)
    }
}

// 合约撤销
export const ContractRevoke = async (data: any, needLoading: boolean = true) => {
    let loadingInstance: any = null;
    try {
        if (needLoading) {
            loadingInstance = open_loading();
        }
        const res: any = await onContractRevoke(data);
        if (loadingInstance) {
            loadingInstance.hide()
        }
        if (res.code == 0) {
            show_message('撤销成功', 'success');
            return true;
        }
        return false;
    } catch (e) {
        console.log(e);
        loadingInstance && loadingInstance.hide();
        return false;
    }
}

// 现货撤销
export const onSpotRevoke = async (data: any, needLoading: boolean = true) => {
    let loadingInstance: any = null;
    try {
        if (needLoading) {
            loadingInstance = open_loading();
        }
        const res: any = await SpotCancel(data);
        if (loadingInstance) {
            loadingInstance.hide()
        }
        if (res.code == 0) {
            show_message('撤销成功', 'success');
            return true;
        }
        return false;
    } catch (e) {
        console.log(e);
        loadingInstance && loadingInstance.hide();
        return false;
    }
}
