/** @format */

import * as React from 'react';
import {MERCHANT_EXPORT_TASK, GET_TASKS, DELELE_TASK} from '../const/path';
import Axios from '../utils/xhr';
interface requestCreateTaskPrams {
    api_id_list: number[];
    export_name: string;
    merchant_id: number;
    start_timestamp: number;
    end_timestamp: number;
    time_list: string;
}
/**
 *   控制面板-统计报表-商户统计报表-导出任务
 */
export const useCreateReportTask = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (params: requestCreateTaskPrams) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: MERCHANT_EXPORT_TASK,
            data: params,
            method: 'POST',
        })
            .then(({code, msg}) => {
                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 BaseViewTaskParms {
    page_number: number;
    is_all?: boolean;
    export_type?: 0 | 1;
    page_size?: number;
}
export interface TaskItemProps {
    create_time: string;
    file_name: string;
    id: number;
    path: string;
    status: string;
    status_code: number;
}

/**
 * 控制面板-统计报表-商户统计报表-查看任务
 *
 */
export const useViewTask = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
        deepData: {
            page_number: 1,
            is_all: false,
            export_type: 0,
            page_size: 10,
        },
        total: 0,
        dataSource: [],
    });
    const requestData = (
        params?: Partial<{page_number: number; is_all: boolean; export_type: 0 | 1 | 2 | 3; page_size: number}>,
    ) => {
        const combineParams = {...state.deepData, ...params};
        setState(preState => ({...preState, loading: true, error: null, deepData: combineParams}));

        return Axios({
            url: GET_TASKS,
            params: combineParams,
        })
            .then(({code, msg, data}) => {
                if (code === 0) {
                    if (!data.export_list) {
                        data.export_list = [];
                    }
                    const basePage = (data.page_number - 1) * data.page_size + 1;
                    setState(preState => ({
                        ...preState,
                        loading: false,
                        total: data.total,
                        dataSource: data.export_list.map((element: TaskItemProps, 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}));
            });
    };

    return [state, requestData] as const;
};
/**
 * 删除任务(导出任务)
 */
export const useDeleteTask = () => {
    const [state, setState] = React.useState({
        loading: false,
        error: null,
    });
    const requestData = (id: number) => {
        setState(preState => ({...preState, loading: true, error: null}));
        return Axios({
            url: DELELE_TASK,
            params: {id},
            method: 'DELETE',
        })
            .then(({code, msg}) => {
                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;
};
