import { wrapTreeNode } from "@/components/leftSelectTree/service";
import requestAHYD from "@/utils/requestAHYD";
import requestCode from "@/utils/requestCode";
import requestSamember from "@/utils/requestOld";
import requestSamemberCode from "@/utils/requestOldCode";
import request from "@/utils/requestMain";
import session from "@/utils/session";
import numeral from 'numeral'
/* 获取枚举列表 */
export async function getFieldEnumTree(params: any) {
    const sessionName = `${params.sessionName ? params.sessionName : params.FieldExplainField}Tree`;
    const FieldExplain = session.get(sessionName);

    const data = await requestSamember(`/FrameWork/GetFieldEnumTree`, {
        method: 'GET',
        params,
    });
    if (data.Result_Code !== 100) {
        return [];
    }

    const options = params.notformate ? formateTreeField(data.Result_Data.List) : wrapTreeNode(data.Result_Data.List);
    session.set(sessionName, options);
    return [...options];
}

// 转换树节点的value类型为string
export function formateTreeField(list: any[]) {
    const valueNumber: any[] = list.map((item: any) => {
        const node = { label: item.node.label, value: item.node.value.toString(), type: item.node.type, ico: item.node.ico };
        if (item.children && item.children.length > 0) {
            node.children = formateTreeField(item.children);
        }
        return node
    });
    return valueNumber.length > 0 ? valueNumber : list;
}

// 删除服务区资产表
export async function handleDeletePROPERTYASSETS(params?: any) {
    const data = await requestSamember(`/BaseInfo/DeletePROPERTYASSETS`, {
        method: 'POST',
        data: params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return data
}

// 将门店批量与资产关联
export async function handleGetBatchPROPERTYSHOP(params?: any) {
    const data = await requestSamember(`/BaseInfo/BatchPROPERTYSHOP`, {
        method: 'POST',
        data: params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return data
}

// 获取服务区资产表树形列表
export async function handleGetPROPERTYASSETSTreeList(params?: any) {
    const data = await requestSamember(`/BaseInfo/GetPROPERTYASSETSTreeList`, {
        method: 'POST',
        data: params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return wrapTreeNode(data.Result_Data.List);
}

// 获取服务区资产表列表
export async function handleGetProvinceVehicleDetail(params?: any) {
    const data = await requestSamember(`/BaseInfo/GetPROPERTYASSETSList`, {
        method: 'POST',
        data: params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return data.Result_Data.List;
}
// 同步服务区资产表
export async function handleSynchroPROPERTYASSETS(params?: any) {
    const data = await requestSamember(`/BaseInfo/SynchroPROPERTYASSETS`, {
        method: 'POST',
        data: params
    });

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

    return data
}

// 获取服务区资产表明细
export async function handleGetPROPERTYASSETSDetail(params?: any) {
    const data = await requestSamember(`/BaseInfo/GetPROPERTYASSETSDetail`, {
        method: 'GET',
        params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return data.Result_Data
}
// 拿到经营项目列表
export async function handleGetBusinessProjectList(params?: any) {
    const data = await requestSamember(`/BusinessProject/GetBusinessProjectList`, {
        method: 'POST',
        data: params
    });

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

    return data.Result_Data.List
}
// 将资产和门店相关联
export async function handleGetSynchroPROPERTYSHOP(params?: any) {
    const data = await requestSamember(`/BaseInfo/SynchroPROPERTYSHOP`, {
        method: 'POST',
        data: params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return data
}

// 拿到图片列表
export async function handleGetPictureList(params: any) {
    const data = await requestSamember(`/Picture/GetPictureList`, {
        method: 'GET',
        params
    })
    if (data.Result_Code === 500) {
        return []
    }

    return data.Result_Data
}

// 拿到服务区的方位详情
export async function handleGetServerpartInfo(params: any) {

    const data = await requestSamemberCode(`/BaseInfo/GetServerpartInfo`, {
        method: 'GET',
        params
    })

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

    return data.Result_Data
}

// 拿到服务区列表
export async function handleGetServerpartTree(params?: any) {
    const data = await requestSamember(`/BaseInfo/GetServerpartTree`, {
        method: 'GET',
        params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return data.Result_Data.List;
}

// 拿到服务区的非树形列表
export async function handleGetServerpartDDL(params?: any) {
    const data = await requestSamember(`/BaseInfo/GetServerpartDDL`, {
        method: 'GET',
        params
    });

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

    return data.Result_Data.List;
}



export async function getFieldEnumTreeNoSession(params: any) {
    const data = await requestSamember(`/FrameWork/GetFieldEnumTree`, {
        method: 'GET',
        params,
    });
    if (data.Result_Code !== 100) {
        return [];
    }

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


export async function uploadAHYDPicture(uploadFilds?: any) {
    return await requestSamember(`/Picture/UploadPicture`, {
        method: 'POST',
        data: uploadFilds
    })
}

// 删除图片
export async function handleDeletePicture(params: any) {

    const data = await requestSamember(`/Picture/DeletePicture`, {
        method: 'GET',
        params
    })

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

    return data
}
/* 获取枚举列表 */
export async function getFieldEnum(params: any): Promise<{ label: string; value: string | number }[]> {
    // const FieldExplain = session.get(params.sessionName ? params.sessionName : params.FieldExplainField);
    // if (FieldExplain) {
    // 	return [...FieldExplain];
    // }
    const data = await requestSamember(`/FrameWork/GetFieldEnumByField`, {
        method: 'GET',
        params,
    });
    if (data.Result_Code !== 100) {
        return [];
    }

    const options = params.notformate ? data.Result_Data.List : formateField(data.Result_Data.List);
    // session.set(params.sessionName ? params.sessionName : params.FieldExplainField, options);
    return options;
}

// 转换options数据value类型为 number
export function formateField(list: { label: string; value: string | number }[]) {
    const valueNumber: { label: string; value: number }[] = [];

    list.map((n: any) => {
        if (!isNaN(Number(n.value))) {
            valueNumber.push({
                label: n.label,
                value: numeral(n.value).value(),
            });
        }
    });
    return valueNumber.length > 0 ? valueNumber : list;
}

// 获取详细数据
export async function getDetail(RegisterCompactId: any) {
    const data = await requestSamember(
        `/Contract/GetRegisterCompactDetail?RegisterCompactId=${RegisterCompactId}`, {
        method: 'GET',
    },
    );

    if (data.Result_Code !== 100) {
        return data.Result_Desc;
    }
    return data.Result_Data;
}
// 查询附属合同详细信息
export async function getSubDetail(RegisterCompactId: any) {
    const data = await requestSamember(
        `/Contract/GetRegisterCompactSubDetail?RegisterCompactId=${RegisterCompactId}`, {
        method: 'GET',
    },
    );

    if (data.Result_Code !== 100) {
        return {};
    }
    return data.Result_Data;
}

// 获取列表数据
export async function getProjectList(params?: any) {
    const search = params ? {
        searchParameter: { ...params }, keyWord: params.keyWord || null,
        PageIndex: params.current, sortstr: params.sortstr, pagesize: params.pageSize
    } : {};
    const data = await requestSamember(`/BusinessProject/GetBusinessProjectList`, {
        method: 'POST',
        data: search,
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            otherData: null,
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        };
    }
    return tableList(data.Result_Data);
}

export function tableList(list: any) {
    return {
        data: list.List || [],
        current: list.PageIndex || 1,
        pageSize: list.pageSize || 10,
        total: list.TotalCount || 0,
        otherData: list?.OtherData || '',
        success: true,
    };
}
//  获取附件图片
export async function getPictureList(id: number, TableType?: string) {
    const data = await requestSamember(`/Picture/GetPictureList?TableId=${id}&TableType=${TableType || ''}`, {
        method: 'GET'
    })

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }
    return tableList(data.Result_Data);
}

// 获得关联合同列表
export async function handleGetRelatedList(params: any) {
    const data = await requestSamember(`/Contract/GetRegisterCompactList`, {
        method: 'POST',
        data: params,
    });
    if (data.Result_Code !== 100) {
        return []
    }
    return tableList(data.Result_Data);
}
// 删除门店关联
export async function handleDeletePROPERTYSHOP(params?: any) {
    const data = await requestSamember(`/BaseInfo/DeletePROPERTYSHOP`, {
        method: 'POST',
        data: params
    });

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

    return data
}

// 拿到门店列表
export async function handleGetServerpartShopList(params?: any) {
    const data = await requestSamember(`/BaseInfo/GetServerpartShopList`, {
        method: 'POST',
        data: params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return data.Result_Data.List
}

// 历史经营项目的勾选数据
export async function handleGetPROPERTYSHOPList(params?: any) {
    const data = await requestSamember(`/BaseInfo/GetPROPERTYSHOPList`, {
        method: 'POST',
        data: params
    });

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

    return data.Result_Data.List
}
// 资产变更记录
export async function handleGetPROPERTYASSETSLOGList(params?: any) {
    const data = await requestSamember(`/BaseInfo/GetPROPERTYASSETSLOGList`, {
        method: 'POST',
        data: params
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return data.Result_Data.List
}

// 获取详细数据
export async function getProjectDetail(businessProjectId: any) {
    const data = await request(
        `/BusinessProject/GetBusinessProjectDetail?businessProjectId=${businessProjectId}`, {
        method: 'GET',
    },
    );

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

    return data.Result_Data;
}

// 获取详细数据
export async function getCOMPACTDetail(RegisterCompactId: any) {
    const data = await request(
        `/Contract/GetRegisterCompactDetail?RegisterCompactId=${RegisterCompactId}`, {
        method: 'GET',
    },
    );

    if (data.Result_Code !== 100) {
        return data.Result_Desc;
    }
    return data.Result_Data;
}

// 获取列表数据
export async function getServerpartShopList(params?: any) {

    const data = await request(`/BaseInfo/GetServerpartShopList`, {
        method: 'POST',
        data: {
            SearchParameter: { ...params }, keyWord: params?.keyWord,
            PageIndex: params?.current, pagesize: params?.pageSize,
            SortStr: params?.SortStr, ShowWholePower: params?.ShowWholePower
        },
    });

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

    return tableList(data.Result_Data);
}

// 获取列表【门店应收拆分明细表】
export async function getList(params?: any) {
    const search = params ? { ...params, SortStr: params?.SortStr, keyWord: params?.keyWord, PageIndex: params?.searchParameter?.current } : {};
    const data = await request(`/BusinessProject/GetSHOPROYALTYDETAILList`, {
        method: 'POST',
        data: search,
    });

    if (data.Result_Code !== 100) {
        return {
            data: [],
            current: 1,
            pageSize: 10,
            total: 0,
            success: false
        }
    }

    return tableList(data.Result_Data);
}

export async function handleGetBusinessDate(params: any) {
    const data = await request('/Revenue/GetBusinessDate', {
        method: 'GET',
        params
    })

    if (data.Result_Code !== 100) {
        return {
            data: [],
            otherData: '',
            current: 1,
            pageSize: 20,
            total: 0,
            success: true,
        };
    }

    return data.Result_Data
}

// 获取门店详情
export async function handleGetServerPartShopNewDetail(params?: any) {

    const data = await requestSamember(`/BaseInfo/GetServerPartShopNewDetail`, {
        method: 'GET',
        params,
    });

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

    return data.Result_Data;

}


// 获取门店详情
export async function handleGetServerpartList(params?: any) {

    const data = await requestSamemberCode(`/BaseInfo/GetServerpartList`, {
        method: 'GET',
        params,
    });

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

    return data.Result_Data.List;

}

// 查询业务环节配置的列表
export async function handleGetAPPROVALROUTEList(params?: any) {

    const data = await requestSamember(`/BusinessProcess/GetAPPROVALROUTEList`, {
        method: 'POST',
        data: params,
    });

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

    return data.Result_Data.List;

}

// 获取账号分类树
export async function getUserTypeTree(params?: any) {
    const data = await requestSamember('/FrameWork/GetUserTypeTree', {
        method: 'GET',
        params
    })

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

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


// 获取账号
export async function handleGetUserList(params?: any) {
    const data = await requestSamember('/Platform/GetUserList', {
        method: 'POST',
        data: params
    })

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

    return data.Result_Data.List
}

export async function getOnwer(params?: any) {
    const data = await requestSamember('/BaseInfo/BindingOwnerUnitDDL', {
        method: 'GET',
        params
    })

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

    return data.Result_Data.List
}

// 配置审批环节
export async function handleSynchroAPPROVALROUTE(params?: any) {
    const data = await requestSamember('/BusinessProcess/SynchroAPPROVALROUTE', {
        method: 'POST',
        data: params
    })

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

    return data
}

// 删除配置审批环节
export async function handleDeleteAPPROVALROUTE(params?: any) {
    const data = await requestSamember(`/BusinessProcess/DeleteAPPROVALROUTE`, {
        method: 'GET',
        params
    });
    if (data.Result_Code !== 100) {
        return data
    }
    return data;
}

// 获取用户建议表列表
export async function handleGetSUGGESTIONList(params?: any) {
    const data = await requestSamember('/SiteManage/GetSUGGESTIONList', {
        method: 'POST',
        data: params
    })

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

    return data.Result_Data.List
}

// 同步用户建议表
export async function handleSynchroSUGGESTION(params?: any) {
    const data = await requestSamember('/SiteManage/SynchroSUGGESTION', {
        method: 'POST',
        data: params
    })

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

    return data
}

// 删除用户建议表
export async function handleDeleteSUGGESTION(params?: any) {
    const data = await requestSamember(`/SiteManage/DeleteSUGGESTION`, {
        method: 'GET',
        params
    });
    if (data.Result_Code !== 100) {
        return data
    }
    return data;
}

// 同步审批意见
export async function handleSynchroAPPLYAPPROVE(params?: any) {
    const data = await requestSamember(`/BusinessProcess/SynchroAPPLYAPPROVE`, {
        method: 'POST',
        data: params
    });
    if (data.Result_Code !== 100) {
        return data
    }
    return data;
}

// 审批意见的流程过程
export async function handleGetAPPLYAPPROVEList(params?: any) {
    const data = await requestSamember(`/BusinessProcess/GetAPPLYAPPROVEList`, {
        method: 'POST',
        data: params
    });
    if (data.Result_Code !== 100) {
        return data
    }
    return data.Result_Data.List;
}

// 用户建议报表页面
export async function handleGetSUGGESTIONReport(params?: any) {
    const data = await requestSamember(`/SiteManage/GetSUGGESTIONReport`, {
        method: 'POST',
        data: params
    });
    if (data.Result_Code !== 100) {
        return data
    }
    return wrapTreeNode(data.Result_Data);
}
