import { request } from '@vtx/utils';
import { useRequest } from 'ahooks';
const BaseService = {
    useRepairMaintain: (id: string, dateValue: string, endDate: string) => {
        // 汇总
        const { data: rmSummaryRes } = useRequest(
            () =>
                request.get(
                    '/cloud/jcss-reborn/maintainRecord/getMaintainRecordStatisticsByFacilityId',
                    {
                        data: { facilityId: id, dateValue },
                    },
                ),
            { refreshDeps: [id, dateValue] },
        );
        const rmSummary = rmSummaryRes?.data || {};

        //维保类型
        const { data: rmTypeRes } = useRequest(
            () =>
                request.get('/cloud/jcss-reborn/maintainRecord/maintainRecordStatisticsByType', {
                    data: { facilityId: id, dateValue },
                }),
            { refreshDeps: [id, dateValue] },
        );
        let rmType = [];
        if (Array.isArray(rmTypeRes?.data)) {
            rmType = rmTypeRes.data.map((ele: any) => {
                return {
                    name: ele.typeName,
                    value: ele.count,
                };
            });
        }

        //月度趋势
        const { data: rmMonthTrendRes } = useRequest(
            () =>
                request.get('/cloud/jcss-reborn/maintainRecord/maintainRecordStatisticsByMonth', {
                    data: { facilityId: id, startDate: dateValue, endDate },
                }),
            { refreshDeps: [id, dateValue, endDate] },
        );
        const rmMonthTrend = rmMonthTrendRes?.data || { legends: [], names: [], values: [] };

        return {
            rmSummary,
            rmType,
            rmMonthTrend,
        };
    },

    useRmRecord: (id: string, applyTimeBegin: string, applyTimeEnd: string, page: number) => {
        const { data: rmRecordRes } = useRequest(
            () =>
                request.get('/cloud/jcss-reborn/maintainRecord/maintainAllPage', {
                    data: {
                        facilityIds: id,
                        applyTimeBegin,
                        applyTimeEnd,
                        page,
                        size: 10,
                    },
                }),
            { refreshDeps: [id, applyTimeBegin, applyTimeEnd, page] },
        );
        const rmRecord = rmRecordRes?.data || { total: 0, rows: [] };

        return {
            rmRecord,
        };
    },
};

export { BaseService };
