/*
 * @Author: Kane
 * @Date: 2023-06-06 17:40:31
 * @LastEditors: Kane
 * @FilePath: /task_schedule/src/utils/ranking.ts
 * @Description: 排行榜相关的代码
 *
 * Copyright (c) ${2022} by Kane, All Rights Reserved.
 */
import { service as instance } from "./api/request.ts";
import { API_URL } from "./api/config.ts";
import { type RankingListItem } from "../types/cpicxim/RankingListItem.ts";

/**
 * 用于请求排行榜数据请求参数
 */
interface RankingListRequest
{
    departmentCode: string;
    year: string;
    month: string;
}

interface RankingListResponse
{
    success: boolean;
    message: string;
    departmentCode: string;
    year: string;
    month: string;
    attachingRateRankingList: RankingListItem[];
    renewalRateRankingList: RankingListItem[];
}

// 判断用的正则表达式
const regexMonth = "(0[1-9])|(1[0-2])"; // eslint-disable-line

/**
 * 请求坐席排行榜。
 * @param reqParam 请求参数
 * @param rander 保存请求结果的回调函数
 * @returns 返回RankingListResponse对象，里面包含请求状态和数据。
 */
function requestRankingList( reqParam: RankingListRequest, rander: any ): void // eslint-disable-line
{
    // let attachingRankingList: RankingListItem[];
    // let renewalRankintList: RankingListItem[];

    const rankingListResponse: RankingListResponse = {
        success: false,
        message: "",
        departmentCode: reqParam.departmentCode,
        year: reqParam.year,
        month: reqParam.month,
        attachingRateRankingList: [],
        renewalRateRankingList: [],
    };

    // 检查请求参数
    // TODO: 这里要加一个对month的正则表达式验证。

    instance.request(
        {
            method: "post",
            url: API_URL.URL_RANKINGLIST,
            data: reqParam,
        })
        .then(( response ) =>
        {
            const data: RankingListResponse = response.data as RankingListResponse;

            rankingListResponse.success = data.success ?? false;
            rankingListResponse.message = data.message ?? "";
            rankingListResponse.departmentCode = data.departmentCode ?? "";
            rankingListResponse.year = data.year ?? "";
            rankingListResponse.month = data.month ?? "";

            // 遍历排行榜元素
            for ( const item of ( data.attachingRateRankingList ?? [] )) // eslint-disable-line
            {
                const index: number = item.index ?? -1;

                if ( index === -1 )
                {
                    continue;
                }

                rankingListResponse.attachingRateRankingList.push( item );
            }

            for ( const item of ( data.renewalRateRankingList ?? [] )) // eslint-disable-line
            {
                const index: number = item.index ?? -1;

                if ( index === -1 )
                {
                    continue;
                }

                rankingListResponse.renewalRateRankingList.push( item );
            }

            // 调用回调函数保存数据
            rander( rankingListResponse );
        })
        .catch(( error ) =>
        {
            console.log( error );
        });
}

export {
    type RankingListRequest,
    type RankingListResponse,
    requestRankingList
};
