/** @format */

import * as React from 'react';
import Axios from '../utils/xhr';
import {PROVIDE_SERVER} from '../const/path';

interface ServerParams {
    today_end_timestamp: number;
    provider_api_id: number | '';
}
/**
 * 仪表盘 供应商 服务概况
 */
export const useServer = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
        deepData: {},
        dataSource: {},
    });
    const requestData = (params?: ServerParams) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParams}));
        Axios({url: PROVIDE_SERVER, params: combineParams})
            .then(({code, msg, info}) => {
                if (code === 0) {
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        dataSource: [
                            {
                                key: '1',
                                name: '有效查询',
                                today: info.today.valid,
                                yesterday: info.yesterday.valid,
                                week: info.week.valid,
                                month: info.month.valid,
                            },
                            {
                                key: '2',
                                name: '计费查次',
                                today: info.today.charge,
                                yesterday: info.yesterday.charge,
                                week: info.week.charge,
                                month: info.month.charge,
                            },
                            {
                                key: '4',
                                name: '异常调用',
                                today: info.today.fail,
                                yesterday: info.yesterday.fail,
                                week: info.week.fail,
                                month: info.month.fail,
                            },
                        ],
                    }));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };
    return [state, requestData] as const;
};
