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

import { useRequest } from 'ahooks';
const BaseData = {
    useBaseInfo: id => {
        const { data: baseInfoRes } = useRequest(
            () =>
                request.get('/cloud/jcss-reborn/facility/get', {
                    data: { id, coordType: 'bd09' },
                }),
            { refreshDeps: [id] },
        );
        const baseInfo = baseInfoRes?.data;
        const { data: statInfoRes } = useRequest(
            () =>
                request.get('/cloud/ljfljc/api/v101/community/getStatistics', {
                    data: { jcssId: id },
                }),
            { refreshDeps: [id] },
        );
        const statInfo = statInfoRes?.data;
        /** 基础信息 */
        const info = [
            {
                label: '行政区划',
                key: 'divisionName',
                value: baseInfo?.divisionName,
            },
            {
                label: '居民户数（户）',
                key: 'householdNum',
                value: statInfo?.householdNum,
            },
            {
                label: '居民人数（人）',
                key: 'peopleNum',
                value: statInfo?.personNum,
            },
            {
                label: '小区类型',
                key: 'classTypeName',
                value: baseInfo?.dataJson.classTypeName,
            },
            {
                label: '地址',
                key: 'address',
                value: baseInfo?.address,
            },
            {
                label: '分类开始时间',
                key: 'implementTime',
                value: baseInfo?.dataJson.implementTime,
            },
            {
                label: '投放点数量（个）',
                key: 'throwPointNum',
                value: statInfo?.throwPointNum,
            },
            { label: '责任人', key: 'sqzrr', value: baseInfo?.dataJson.sqzrr },
            {
                label: '联系方式',
                key: 'sqzrrlxfs',
                value: baseInfo?.dataJson.sqzrrlxfs,
            },
        ];
        const coords = baseInfo?.geometryInfo?.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;
    },
};

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;
            });
    },

    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;
    },
    useCollectStat: (id, start, end) => {
        const { data } = useRequest(
            request.get('/cloud/ljsy-reborn/api/collect/record/detail/sum', {
                data: { collectPointIds: id, startTime: start, endTime: end },
            }),
            {
                refreshDeps: [id, start, end],
            },
        );
        return data?.data;
    },
    getCollectList: (page, size, id, start, end) => {
        return request
            .get('/cloud/ljsy-reborn/api/collect/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;
            });
    },
    exportData: params => {
        return downloadFile(
            '/cloud/ljfljc/api/v101/trow/record/detail/export',
            {
                data: {
                    ...params,
                    beenPermission: 'false',
                },
            },
            'blob',
        );
    },
};
export { BaseData, ThrowData };
