/*
 * @Author: Kane
 * @Date: 2023-10-17 11:18:19
 * @LastEditors: Kane
 * @FilePath: /task_schedule/src/utils/BIReport.ts
 * @Description: BI报表相关的代码
 * 
 * Copyright (c) ${2023} by Kane, All Rights Reserved. 
 */
import { service as instance } from "./api/request.js";
import { API_URL } from "./api/config.js";
import { type AxiosResponse } from "axios";
interface BIReportType
{
    reportTypeCode: number;
    reportTypeName: string;
}

interface ImportBIReportRequest
{
    filePath: string,
    reportType: number,
    firstRow: number,
    sheetIndex: number,
}

interface ImportBIReportResponse
{
    success: boolean,
    message: string,
    importedCount: number,
}

type ImportBIReportResponseHandler = ( response: ImportBIReportResponse, error?: any ) => void;

/**
 * 
 * @param request 请求参数
 * @param handler 调用者提供的请求结果处理函数
 */
function importBIReport( request: ImportBIReportRequest, handler: any ): void
{
    const importResponse: ImportBIReportResponse = {
        success: false,
        message: "",
        importedCount: 0,
    };

    instance.request({
        url: API_URL.URL_IMPORT_REPORT,
        method: "post",
        data: request,
    })
        .then(( response: AxiosResponse<any, any> ): any =>
        {
            const data: ImportBIReportResponse = response.data;

            importResponse.success = data.success ?? false;
            importResponse.message = data.message ?? "";
            importResponse.importedCount = data.importedCount ?? 0;

            handler( importResponse, null );
        })
        .catch(( error: any ): any =>
        {
            importResponse.message = "请求导入报表错误，请查看控制台！";
            console.log( error );

            handler( importResponse, error );
        });
}

export {
    type BIReportType,
    type ImportBIReportRequest,
    type ImportBIReportResponse,
    type ImportBIReportResponseHandler,
    importBIReport
};
