import { request } from '@vtx/utils';
import { useRequest } from 'ahooks';
const BaseService = {
    useBaseInfo: (id: string, startDate: string, endDate: string) => {
        const { data: baseInfoRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/collect/point/get', {
                    data: { id: id, coordinateType: 'bd09' },
                }),
            { refreshDeps: [id] },
        );
        const info = baseInfoRes?.data;
        const baseInfo = [
            {
                label: '行政区划',
                key: 'divisionName',
                value: info?.divisionName,
            },

            {
                label: '垃圾类型',
                key: 'productTypeNames',
                value: info?.productTypeNames,
            },
            {
                label: '管理单位',
                key: 'manageUnitName',
                value: info?.manageUnitName,
            },
            {
                label: '收运单位',
                key: 'accUnitName',
                value: info?.accUnitName,
            },

            {
                label: '地址',
                key: 'address',
                value: info?.address,
            },
        ];
        // 今日作业
        const { data: todayWorkRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/kanban/collectRateByProduct', {
                    data: {
                        collectPointId: id,
                        startDate,
                        endDate,
                        coordinateType: 'bd09',
                    },
                }),
            { refreshDeps: [id, startDate, endDate] },
        );
        // 车辆作业信息
        const { data: carWorkRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/kanban/getCarCollectInfoByPointId', {
                    data: {
                        collectPointId: id,
                        startDate,
                        endDate,
                        coordinateType: 'bd09',
                    },
                }),
            { refreshDeps: [id, startDate, endDate] },
        );
        return {
            baseInfo,
            rules: info?.rules || [],
            todayWorkRes: todayWorkRes?.data || [],
            dataSource: carWorkRes?.data || [],
        };
    },
};

const SyService = {
    useBaseInfo: (id: string, startDate: string, endDate: string) => {
        const { data: baseInfoRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/kanban/collectRateByProduct', {
                    data: { collectPointId: id, startDate, endDate },
                }),
            { refreshDeps: [id, startDate, endDate] },
        );
        const baseInfo = baseInfoRes?.data || [];

        const { data: collectWeightRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/kanban/collectWeightAnalyze', {
                    data: { collectPointId: id, startDate, endDate },
                }),
            { refreshDeps: [id, startDate, endDate] },
        );
        const collectWeight = collectWeightRes?.data;
        return {
            baseInfo,
            collectWeight,
        };
    },
    useRecordInfo: (id: string, startTime: string, endTime: string, page: number) => {
        const { data: recordRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/collect/record/day/page', {
                    data: { productTypeId: id, startTime, endTime, page, size: 10 },
                }),
            { refreshDeps: [id, startTime, endTime, page] },
        );
        const records = recordRes?.data || { total: 0, rows: [] };
        return {
            records,
        };
    },
};

const AlarmService = {
    //信息汇总
    useAlarmSummary: (id: string, dateValue: string) => {
        const { data: alarmSummaryRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/kanban/getAlarmStatisticsByCollectPointId', {
                    data: {
                        collectPointId: id,
                        dateValue,
                    },
                }),
            { refreshDeps: [id, dateValue] },
        );
        const alarmSummary = alarmSummaryRes?.data || [];

        return {
            alarmSummary,
        };
    },

    // 类型分析
    useAlarmTrend: (id: string, startDate: string, endDate: string) => {
        const { data: alarmTypeRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/kanban/alarmStatisticsByAlarmType', {
                    data: {
                        collectPointId: id,
                        startDate,
                        endDate,
                    },
                }),
            { refreshDeps: [id, startDate, endDate] },
        );
        const alarmTypes = alarmTypeRes?.data || [];

        return {
            alarmTypes,
        };
    },

    // 月度趋势分析
    useAlarmMonthTrend: (id: string, startDate: string, endDate: string) => {
        const { data: alarmMonthTrendRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/kanban/alarmStatisticsByMonth', {
                    data: {
                        collectPointId: id,
                        startDate,
                        endDate,
                    },
                }),
            { refreshDeps: [id, startDate, endDate] },
        );

        const alarmMonthTrend = alarmMonthTrendRes?.data || [];

        return {
            alarmMonthTrend,
        };
    },

    // 报警记录
    useAlarmRecord: (
        id: string,
        alarmTypeCode: string,
        start: string,
        end: string,
        page: number,
    ) => {
        const { data: alarmRecordRes } = useRequest(
            () =>
                request.get('/cloud/ljsy-reborn/api/collect/point/alarm/alarmPage', {
                    data: {
                        collectPointIds: id,
                        alarmTypeCode,
                        startDate: start,
                        endDate: end,
                        page: page,
                        size: 10,
                    },
                }),
            {
                paginated: true,
                refreshDeps: [id, alarmTypeCode, start, end, page],
            },
        );
        const dataSource = alarmRecordRes?.data?.rows || [];
        const total = alarmRecordRes?.data?.total;
        return { detail: { dataSource, total } };
    },
};

export { BaseService, SyService, AlarmService };
