/** @format */

import * as React from 'react';
import Axios from '../utils/xhr';
import {GET_CONTROL_MERCAHNT_LOG, GET_SOURCE_CONTETN_LOG, GET_SOURCE_LOG} from '../const/path';
interface Iparams {
    page_size?: number;
    state: number;
    request_param: string;
    merchant_id: number | string;
    api_id: number | string;
    start_timestamp: number;
    end_timestamp: number;
    search: string;
    code: number | string;
    page_number: number;
    is_all?: boolean;
    min: number | '';
    max: number | '';
}
/**
 * 控制面板-调用日志-商户调用日志
 */
export const useControlMerchantLog = (params: Iparams) => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
        dataSource: [],
        total: 0,
        deepData: {
            page_size: 10,
            is_all: false,

            ...params,
        },
    });
    const requestData = (params?: Partial<Iparams>) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParams}));
        Axios({
            url: GET_CONTROL_MERCAHNT_LOG,
            params: combineParams,
        })
            .then(({code, data, msg}) => {
                if (code === 0) {
                    if (!data.access_logs) {
                        data.access_logs = [];
                    }
                    const basePage = (data.page_number - 1) * data.page_size + 1;
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        total: data.total,
                        dataSource: data.access_logs.map((element: any, index: number) => {
                            return {
                                ...element,
                                index: basePage + index,
                            };
                        }),
                    }));
                } else {
                    setState(preState => ({...preState, error: msg, loading: false}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, error: error.message, loading: false}));
            });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};
/**
 * 获取商户调用日志-数据源内容
 *
 */
export const useSourceContent = () => {
    const [state, setState] = React.useState({
        loading: false,
        dataSource: [],
    });
    const requestData = (id: number, create_time: string) => {
        setState(preState => ({...preState, loading: true}));
        Axios({
            url: GET_SOURCE_CONTETN_LOG,
            params: {
                access_id: id,
                create_time: create_time,
            },
        })
            .then(({code, data}) => {
                if (code === 0) {
                    if (!data.thirdpart_logs) {
                        data.thirdpart_logs = [];
                    }

                    setState(preState => ({
                        ...preState,
                        loading: false,
                        dataSource: data.thirdpart_logs.map((element: any, index: number) => {
                            element.request_params = element.request_params || null;
                            element.response_params = element.response_params || null;
                            try {
                                if (element.request_params) {
                                    element.request_params = JSON.parse(element.request_params);
                                }
                            } catch (e) {}
                            try {
                                if (element.response_params) {
                                    element.response_params = JSON.parse(element.response_params);
                                }
                            } catch (e) {}
                            return {
                                ...element,
                                index: index + 1,
                            };
                        }),
                    }));
                }
            })
            .catch(error => {});
    };
    return [state, requestData] as const;
};

interface SourceLogParams {
    page_size?: number;
    raw_code?: any;
    is_all?: boolean;
    page_number?: number;
    search: string;
    state: 0 | 1 | 2;
    request_param: string;
    provider_id: number | '';
    provider_api_id: '' | number;
    start_timestamp: number;
    end_timestamp: number;
    min: number | '';
    max: number | '';
}

export interface ReciveSourceLogProps {
    api_name: string;
    code: number;
    create_time: string;
    elapsed: number;
    id: number;
    merchant_name: string;
    msg: string;
    provider_api_name: string;
    provider_name: string;
    raw_code: string;
    remote_addr: string;
    request_params: string;
    response_params: string;
    search: string;
    state: boolean;
}

/**
 * 控制面板-调用日志-数据源调用日志
 */
export const useSourceCallLog = (params: SourceLogParams) => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
        total: 0,
        dataSource: [],
        deepData: {
            is_all: false,
            page_size: 10,
            ...params,
        },
    });
    const requestData = (params?: Partial<SourceLogParams>) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParams}));
        Axios({url: GET_SOURCE_LOG, params: combineParams})
            .then(({code, data, msg}) => {
                if (code === 0) {
                    if (!data.thirdpart_logs) {
                        data.thirdpart_logs = [];
                    }
                    const basePage = (data.page_number - 1) * data.page_size + 1;
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        total: data.total,
                        dataSource: data.thirdpart_logs.map((element: ReciveSourceLogProps, index: number) => {
                            return {
                                ...element,
                                index: basePage + index,
                            };
                        }),
                    }));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
            });
    };
    React.useEffect(() => {
        requestData(params);
    }, []);
    return [state, requestData] as const;
};
interface ExportLogParams {
    api_id: number;
    end_timestamp: number;
    export_name: string;
    is_parse_param: boolean;
    merchant_id: number;
    start_timestamp: number;
}
/**
 * 日志分析-调用日志-商户-日志导出
 */
export const useMerchantExportLog = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: ExportLogParams) => {
        setState(preState => ({...preState, loading: true}));
        return Axios({url: '/access_log/access_log_export', method: 'POST', data: params})
            .then(({code, msg, data}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false}));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                    return msg;
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
                return error.message;
            });
    };
    return [state, requestData] as const;
};
interface ExportProvideParams {
    provider_id: number;
    end_timestamp: number;
    export_name: string;
    is_parse_param: boolean;
    provider_api_id: number;
    start_timestamp: number;
}
/**
 * 日志分析-调用日志-供应商-日志导出
 */
export const useProvideExportLog = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: ExportProvideParams) => {
        setState(preState => ({...preState, loading: true}));
        return Axios({url: '/access_log/thirdpart_log_export', method: 'POST', data: params})
            .then(({code, msg, data}) => {
                if (code === 0) {
                    setState(preState => ({...preState, loading: false}));
                } else {
                    setState(preState => ({...preState, loading: false, error: msg}));
                    return msg;
                }
            })
            .catch(error => {
                setState(preState => ({...preState, loading: false, error: error.message}));
                return error.message;
            });
    };
    return [state, requestData] as const;
};
