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/jcss-reborn/facility/get', {
                    data: { id: id, coordType: 'bd09' },
                }),
            { refreshDeps: [id] },
        );
        const info = baseInfoRes?.data;
        const baseInfo = [
            {
                label: '行政区划',
                key: 'divisionName',
                value: info?.divisionName,
            },
            {
                label: '管理单位',
                key: 'manageUnitName',
                value: info?.manageUnitName,
            },

            {
                label: '使用单位',
                key: 'useUnitName',
                value: info?.useUnitName,
            },
            {
                label: '公厕状态',
                key: 'facilityStatusName',
                value: info?.facilityStatusName,
                color: '#52C41A',
            },
            {
                label: '详细地址',
                key: 'address',
                value: info?.address,
            },
        ];
        const photos = info?.photoIds ? JSON.parse(info.photoIds) : [];

        // 实时运行情况
        const { data: situationRes } = useRequest(
            () =>
                request.post('/cloud/sjzt-reborn/api/v101/dataSet/execute', {
                    data: JSON.stringify({
                        code: 'ZHGC_DIFF_DATATYPE_SUM_MONITOR_VALUE_BY_TYPE',
                        parameters: {
                            gcId: id,
                            startDate,
                            endDate,
                        },
                    }),
                }),
            { refreshDeps: [id, startDate, endDate] },
        );

        return { baseInfo, photos, situationRes: situationRes?.data || [] };
    },
    useBaseAlarmInfo: (id: string, startTime: string, endTime: string) => {
        //本月报警类型分析
        const { data: alarmTypeRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/standardDataAlarm/statisticsByStandardCode', {
                    data: {
                        baseInfoId: id,
                        startDateStr: startTime,
                        endDateStr: endTime,
                    },
                }),
            { refreshDeps: [id, startTime, endTime] },
        );

        return { alarmTypeRes: alarmTypeRes?.data || [] };
    },

    useBaseAlarmTypes: (id: string, startTime: string, endTime: string) => {
        //报警明细类型
        const { data: alarmTypeRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/standardDataAlarm/statisticsByStandardCode', {
                    data: {
                        baseInfoId: id,
                        startDateStr: startTime,
                        endDateStr: endTime,
                    },
                }),
            { refreshDeps: [id, startTime, endTime] },
        );

        return { alarmTypeRes: alarmTypeRes?.data || [] };
    },
    useAlarmDetails: (
        id: string,
        standardCode: string,
        start: string,
        end: string,
        page: number,
        rows: number,
    ) => {
        const { data: res } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/standardDataAlarm/queryList.smvc', {
                    data: {
                        baseInfoId: id,
                        standardCode,
                        startDateStr: start,
                        endDateStr: end,
                        page: page,
                        rows: rows,
                    },
                }),
            {
                paginated: true,
                refreshDeps: [id, standardCode, start, end, page],
            },
        );
        const dataSource = res?.data?.rows || [];
        const total = res?.data?.total;
        return { detail: { dataSource, total } };
    },
};

const MoniotrService = {
    useRealtime: (id: string, date: string) => {
        const { data: realtimeRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/imageTextBoard/queryMonitorData.smvc', {
                    data: {
                        baseInfoId: id,
                        date,
                    },
                }),
            { refreshDeps: [id, date] },
        );

        return { realtimeList: realtimeRes?.data?.standardList || [] };
    },
    useMonitorType: (id: string) => {
        const { data: monitorTypeRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/imageTextBoard/loadStandardType.smvc', {
                    data: {
                        baseInfoId: id,
                    },
                }),
            { refreshDeps: [id] },
        );
        const names = ['PersonNum', 'OdorValue', 'SeatNum'];
        const monitorType = monitorTypeRes?.data
            ? monitorTypeRes.data
                  .filter((ele: any) => names.includes(ele.standardCode))
                  .map((ele: any) => ({
                      label: ele.standardCodeName,
                      key: ele.standardCode,
                  }))
            : [];

        return { monitorType };
    },
    useMonitorChart: (id: string, startDate: string, endDate: string, filterKey: string) => {
        const url = getUrl(filterKey);
        const { data: monitorChartRes } = useRequest(
            () =>
                request.get(url, {
                    data: {
                        baseInfoId: id,
                        startDate,
                        endDate,
                        statisticalPeriod: 'day', // 默认按照天查询
                    },
                }),
            { refreshDeps: [id, startDate, endDate, filterKey] },
        );
        let series = [];
        if (Array.isArray(monitorChartRes?.data?.series)) {
            series = monitorChartRes.data.series.map((ele: any) => ({
                data: ele.data,
                name: ele.name,
                type: 'line',
            }));
        }
        const monitorChart = { ...monitorChartRes?.data, series } || {};

        return { monitorChart };
    },
};

// 监测信息，根据监测类型获取不同的url
const getUrl = (key: string) => {
    let url = '';
    switch (key) {
        case 'PersonNum':
            url = 'personNum/getEcharts.smvc';
            break;
        case 'OdorValue':
            url = 'odor/getEcharts.smvc';
            break;
        case 'temperature':
            url = 'temperature/getEcharts.smvc';
            break;
        case 'humidity':
            url = 'humidity/getEcharts.smvc';
            break;
        case 'WaterUse':
            url = 'waterBySensor/getEcharts.smvc';
            break;
        case 'ElecUse':
            url = 'electricityPowerBySensor/getEcharts.smvc';
            break;
        case 'SeatNum':
            url = 'seat/getEcharts.smvc';
            break;
        default:
            break;
    }
    return `/cloud/zhgc/api/np/v1/${url}`;
};

const AlarmService = {
    //信息汇总
    useAlarmSummary: (id: string, dateValue: string) => {
        const { data: alarmSummaryRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/standardDataAlarm/statisticsByYearOnYear', {
                    data: {
                        baseInfoId: id,
                        dateValue,
                    },
                }),
            { refreshDeps: [id, dateValue] },
        );
        const alarmSummary = alarmSummaryRes?.data || [];

        return {
            alarmSummary,
        };
    },
    // 报警记录
    useAlarmRecord: (
        id: string,
        standardCode: string,
        start: string,
        end: string,
        page: number,
    ) => {
        const { data: alarmRecordRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/standardDataAlarm/queryList.smvc', {
                    data: {
                        baseInfoId: id,
                        standardCode,
                        startDateStr: start,
                        endDateStr: end,
                        page: page,
                        rows: 10,
                    },
                }),
            {
                paginated: true,
                refreshDeps: [id, standardCode, start, end, page],
            },
        );
        const dataSource = alarmRecordRes?.data?.rows || [];
        const total = alarmRecordRes?.data?.total;
        return { detail: { dataSource, total } };
    },
    // 类型分析
    useAlarmTrend: (id: string, startDateStr: string, endDateStr: string) => {
        const { data: alarmTrendRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/standardDataAlarm/statisticsByMonth', {
                    data: {
                        baseInfoId: id,
                        startDateStr,
                        endDateStr,
                    },
                }),
            { refreshDeps: [id] },
        );
        const alarmTrend = alarmTrendRes?.data || [];

        return {
            alarmTrend,
        };
    },
};

const AppraiseService = {
    //信息汇总
    useAppraisSummary: (id: string, startDate: string, endDate: string) => {
        const { data: appraiseSummaryRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/baseInfoAssess/baseInfoAssessStatistics', {
                    data: {
                        baseInfoId: id,
                        startDateStr: startDate,
                        endDateStr: endDate,
                    },
                }),
            { refreshDeps: [id, startDate, endDate] },
        );
        const appraiseSummary = appraiseSummaryRes?.data || [];

        return {
            appraiseSummary,
        };
    },
    // 报警记录
    useAppraiseRecord: (
        id: string,

        page: number,
    ) => {
        const { data: appraiseRecordRes } = useRequest(
            () =>
                request.get('/cloud/zhgc/api/np/v1/baseInfoAssess/queryList.smvc', {
                    data: {
                        baseInfoId: id,
                        page: page,
                        rows: 10,
                    },
                }),
            {
                paginated: true,
                refreshDeps: [id, page],
            },
        );
        const dataSource = appraiseRecordRes?.data?.rows || [];
        const total = appraiseRecordRes?.data?.total;
        return { records: { dataSource, total } };
    },
};

export { BaseService, MoniotrService, AlarmService, AppraiseService };
