/*
 * @Author: Kane
 * @Date: 2023-03-17 15:17:44
 * @LastEditors: Kane
 * @FilePath: /task_schedule/src/utils/archievement.ts
 * @Description: 请求业绩数据
 *
 * Copyright (c) ${2022} 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 Archievement
{
    success: boolean;
    message: string;
    total_archievement: number;
    mensual_archievement_list: number[];
    insurance_renewal_rate: string;
    attaching_rate: string;
    leading_reward_gainers: string[];
    advance_reward_gainers: string[];
    backward_list: string[];
}

/**
 * 获取部门业绩数据，并调用回调函数进行渲染。
 * @param render 用于给组件更新数据的回调函数
 */
function queryDepartmentArchievement( render: any ): void
{
    instance.request({
        method: "post",
        url: API_URL.URL_DEPARTMENT_ARCHIEVEMENT,
    })
        .then(( response ) =>
        {
            const archievement: Archievement = {
                success: false,
                message: "",
                total_archievement: 0,
                mensual_archievement_list: [],
                insurance_renewal_rate: "",
                attaching_rate: "",
                leading_reward_gainers: [],
                advance_reward_gainers: [],
                backward_list: [],
            };

            const data = response.data ?? {};

            archievement.success = data.success ?? false;
            archievement.message = data.message ?? "";
            archievement.mensual_archievement_list = data.mensual_archievement_list ?? [];
            archievement.insurance_renewal_rate = data.insurance_renewal_rate ?? "0.0";
            archievement.attaching_rate = data.attaching_rate ?? "0.0";
            archievement.leading_reward_gainers = data.leading_reward_gainers ?? [];
            archievement.advance_reward_gainers = data.advance_reward_gainers ?? [];
            archievement.backward_list = data.backward_list ?? [];

            render( data );
        })
        .catch(( error ) =>
        {
            console.log( error );
        });
}

export { queryDepartmentArchievement, type Archievement };
