import { request, downloadFile } from '@vtx/utils';

import { useRequest } from 'ahooks';
const BaseData = {
    useBaseInfo: id => {
        const { data: baseInfoRes } = useRequest(
            () =>
                request.get('/cloud/ljfljc/api/v101/throwPoint/queryById', {
                    data: { id, coordinateType: 'bd09' },
                }),
            { refreshDeps: [id] },
        );
        const baseInfo = baseInfoRes?.data;
        /** 基础信息 */
        const info = [
            {
                label: '名称',
                key: 'pointName',
                value: baseInfo?.pointName,
            },
            {
                label: '投放点类型',
                key: 'pointTypeDesc',
                value: baseInfo?.pointTypeDesc,
            },
            {
                label: '所属小区',
                key: 'jcssName',
                value: baseInfo?.jcssName,
            },
            {
                label: '行政区划',
                key: 'classTypeName',
                value: baseInfo?.divisionName,
            },
            {
                label: '投放点地址',
                key: 'specificAddress',
                value: baseInfo?.specificAddress,
            },
            {
                label: '投放时段',
                key: 'implementTime',
                value: (baseInfo?.pointThrowPeriods || [])
                    .map(res => {
                        return res.startTime + ' ~ ' + res.endTime;
                    })
                    .join(','),
            },
        ];
        const coords = baseInfo?.latLngDTO.lngLats.split(',').map(i => Number(i)) || null;
        return { info, coords };
    },
    useBaseProdStat: (id, start, end) => {
        const { data } = useRequest(
            () =>
                request.get('/cloud/ljfljc/api/v101/throwDataAnalysis/productionAnalysis', {
                    data: { jcssId: id, startDate: start, endDate: end },
                }),
            {
                refreshDeps: [id, start, end],
            },
        );
        return data?.data;
    },
    useBaseProdTrend: (id, start, end) => {
        const { data } = useRequest(
            () =>
                request.get('/cloud/ljfljc/api/v101/throwDataAnalysis/productionTrend', {
                    data: { jcssId: id, startDate: start, endDate: end, dateDimension: 'day' },
                }),
            {
                refreshDeps: [id, start, end],
            },
        );
        return data?.data;
    },
    getGarbageType: () => {
        const { data } = useRequest(() => request.get('/cloud/jcss-reborn/garbageType/list', {}));
        return data?.data;
    },
};

const ThrowData = {
    useThrowList: ({ page, size }, { id, start, end }) => {
        const { data } = useRequest(
            () =>
                request.get('/cloud/ljfljc/api/v101/trow/record/detail/page', {
                    data: {
                        page,
                        size,
                        subjectId: id,
                        startTime: start,
                        endTime: end,
                        beenPermission: 'false',
                    },
                }),
            {
                refreshDeps: [id, start, end],
            },
        );

        return data?.data;
    },

    getThrowList: (page, size, id, start, end) => {
        return request
            .get('/cloud/ljfljc/api/v101/trow/record/detail/page', {
                data: {
                    page,
                    size,
                    subjectId: id,
                    startTime: start,
                    endTime: end,
                    beenPermission: 'false',
                },
            })
            .then(res => {
                if (res.result === 0) {
                    res.data.list = res.data.rows;
                }
                return res.data;
            });
    },
    getSum: () => {
        const { data } = useRequest(() =>
            request.get('/cloud/ljfljc/api/v101/trow/record/detail/sum', {}),
        );
        return data?.data;
    },

    exportData: params => {
        return downloadFile(
            '/cloud/ljfljc/api/v101/trow/record/detail/export',
            {
                data: {
                    ...params,
                    beenPermission: 'false',
                },
            },
            'blob',
        );
    },
};
const DeviceData = {
    deviceList: id => {
        const { data } = useRequest(
            request.get('/cloud/ljfljc/api/v101/equipment/select', {
                data: {
                    throwPointId: id,
                },
            }),
            {
                refreshDeps: [id],
            },
        );
        return data?.data;
    },
    deviceData: id => {
        const { data: baseInfoRes } = useRequest(
            () =>
                request.get('/cloud/ljfljc/api/v101/equipment/get', {
                    data: { id },
                }),
            { refreshDeps: [id] },
        );
        const baseInfo = baseInfoRes?.data || {};
        /** 基础信息 */
        const info = [
            {
                label: '设备编码',
                key: 'code',
                value: baseInfo?.code,
            },
            {
                label: '设备名称',
                key: 'name',
                value: baseInfo?.name,
            },
            {
                label: '设备类型',
                key: 'typeName',
                value: baseInfo?.typeName,
            },
            {
                label: '行政区划',
                key: 'divisionName',
                value: baseInfo?.divisionName,
            },
            {
                label: '所属主体名称',
                key: 'housingEstateName',
                value: baseInfo?.housingEstateName,
            },
            {
                label: '所属投放点',
                key: 'throwPointName',
                value: baseInfo.throwPointName,
            },
            {
                label: '投用时间',
                key: 'putTime',
                value: baseInfo.putTime,
            },
            {
                label: '运营单位',
                key: 'operateUnitName',
                value: baseInfo.operateUnitName,
            },
            {
                label: '负责人',
                key: 'inChargeStaffName',
                value: baseInfo.inChargeStaffName,
            },
            {
                label: '联系电话',
                key: 'contactTel',
                value: baseInfo.contactTel,
            },
        ];
        return { info };
    },
    getThrowList: (page, size, id, start, end) => {
        return request
            .get('/cloud/ljfljc/api/v101/trow/record/detail/page', {
                data: {
                    page,
                    size,
                    subjectId: id,
                    startTime: start,
                    endTime: end,
                    beenPermission: 'false',
                },
            })
            .then(res => {
                if (res.result === 0) {
                    res.data.list = res.data.rows;
                }
                return res.data;
            });
    },
    overflowList: (id, start, end) => {
        const { data } = useRequest(
            request.get('/cloud/ljfljc/api/v101/overflowRecord/page', {
                data: { throwPointId: id, startDate: start, endDate: end },
            }),
            {
                refreshDeps: [id, start, end],
            },
        );
        return data?.data;
    },
    operation: (id, month) => {
        const { data } = useRequest(
            request.get('/cloud/ljfljc/api/v101/equipment/getOperationCalendar', {
                data: { equipmentId: id, month },
            }),
            {
                refreshDeps: [id, month],
            },
        );
        return data?.data;
    },

    useThrowStat: (id, start, end) => {
        const { data } = useRequest(
            request.get('/cloud/ljfljc/api/v101/throwDataAnalysis/memberAnalysis', {
                data: { jcssId: id, startDate: start, endDate: end },
            }),
            {
                refreshDeps: [id, start, end],
            },
        );
        return data?.data;
    },
};

const CollectData = {
    deviceList: id => {
        const { data } = useRequest(
            request.get('/cloud/ljfljc/api/v101/equipment/select', {
                data: {
                    throwPointId: id,
                },
            }),
            {
                refreshDeps: [id],
            },
        );
        return data?.data;
    },
    collectDataList: ({ page, size, id, start, end, productTypeIds }) => {
        return request
            .get('/cloud/ljsy-reborn/api/collect/record/detail/page', {
                data: {
                    collectPointIds: id,
                    startTime: start,
                    endTime: end,
                    productTypeIds: productTypeIds,
                    page,
                    size,
                },
            })
            .then(res => {
                if (res.result === 0) {
                    res.data.list = res.data.rows;
                }
                return res.data;
            });
    },

    getSum: (id, start, end, productTypeIds) => {
        const { data } = useRequest(
            request.get('/cloud/ljsy-reborn/api/collect/record/detail/sum', {
                data: {
                    collectPointIds: id,
                    startTime: start,
                    endTime: end,
                    productTypeIds: productTypeIds,
                },
            }),
            {
                refreshDeps: [id],
            },
        );
        return data?.data;
    },
    exportData: params => {
        return downloadFile(
            '/cloud/ljsy-reborn/api/collect/record/detail/exportExcel',
            {
                data: {
                    ...params,
                    beenPermission: 'false',
                },
            },
            'blob',
        );
    },
};

export { BaseData, ThrowData, DeviceData, CollectData };
