import request from "@/utils/requestOld"

export async function getServerpartTree(ProvinceCode?: number | string, ServerpartCodes?: string,
    ShowWholePower?: boolean, ShowSPRegion?: boolean, ShowRoyalty?: boolean | false, ShowCompactCount?: boolean | false, StatisticsType?: any, ServerpartSiteType?: any): Promise<ServerpartTree[]> {
    // ShowRoyalty 无论传入什么都是false
    const data = await request(`/BaseInfo/GetServerpartTree?
		ProvinceCode=${ProvinceCode || ''}&ServerpartCodes=${ServerpartCodes || ''}&
		ShowWholePower=${ShowWholePower || false}&ShowWholePower=${ShowSPRegion || true}&
		ShowRoyalty=${false}&ShowCompactCount=${ShowCompactCount || false}&StatisticsType=${StatisticsType || ''}&ServerpartSiteType=${ServerpartSiteType || ''}`, {
        method: 'GET',
    });
    if (data.Result_Code !== 100) {
        return [];
    }


    return data.Result_Data.List;
}


export async function getMerchantShopTree(params: { BusinessManId?: any, provinceCode?: number }) {
    const data = await request(`/FrameWork/GetMerchantShopTree`, {
        method: 'GET',
        params
    })

    if (data.Result_Code !== 100) {
        return []
    }

    const treeTable = wrapTreeNode(data.Result_Data.List);
    return [...treeTable];
}

export function wrapTreeNode(data: any[]) {

    const wrapData: any = data.map((item: any) => {
        const node = { ...item.node };

        if (item.children && item.children.length > 0) {
            node.children = wrapTreeNode(item.children);
        }
        return node
    });
    return wrapData;
}


export async function handleGetServerpartDDL(params: any) {
    const data = await request(`/BaseInfo/GetServerpartDDL`, {
        method: 'GET',
        params
    })

    if (data.Result_Code !== 100) {
        return []
    }

    return data.Result_Data.List
}

