import { request, getUrlParam, downloadFile } from '@vtx/utils';
const { tenantId } = getUrlParam();

import { useRequest } from 'ahooks';
const BaseData = {
    useBaseInfo: (id, setCode) => {
        const { data: baseInfoRes } = useRequest(
            () =>
                request.get('/cloud/jcss-reborn/facility/get', {
                    data: { id, coordType: 'bd09' },
                }),
            { refreshDeps: [id] },
        );
        const baseInfo = baseInfoRes?.data;
        setCode(`${baseInfo?.typeCode}_${baseInfo?.code}`);
        // 基础信息
        let info = [
            { label: '行政区划', key: 'divisionName', value: baseInfo?.divisionName },
            { label: '管理单位', key: 'manageUnitName', value: baseInfo?.manageUnitName },
            { label: '启用日期', key: 'enableTime', value: baseInfo?.enableTime },
            { label: '详细地址', key: 'address', value: baseInfo?.address },
        ];
        if (baseInfo?.typeCode === 'gjd' || baseInfo?.typeCode === 'xnc') {
            info.push({
                label: '设计容量',
                key: 'sjrl',
                value: `${baseInfo?.dataJson?.sjrl || 0}万方`,
            });
        }
        if (baseInfo?.typeCode === 'clc') {
            info.push({
                label: '设计能力',
                key: 'cznl',
                value: `${baseInfo?.dataJson?.cznl || 0}吨/每天`,
            });
        }
        // 图片
        const photos = JSON.parse(baseInfo?.photoIds || '[]');
        return { info, photos };
    },
};

const WeighData = {
    // 列表统计
    useWeighStat: (id, startTime, endTime, page, size, code) => {
        const { data } = useRequest(
            () =>
                request.get('/cloud/zd/lm/api/cp/zdjgLmWeighData/summary.smvc', {
                    data: {
                        id,
                        startTime,
                        endTime,
                        tenantId,
                        page,
                        size,
                        disposeUnitCode: code.split('_')[1],
                    },
                }),
            { refreshDeps: [id, startTime, endTime, page, size] },
        );
        return data?.data;
    },
    // 列表数据
    useWeighList: (id, startTime, endTime, page, size, code) => {
        return request
            .get('/cloud/zd/lm/api/cp/zdjgLmWeighData/pageList.smvc', {
                data: {
                    id,
                    page,
                    rows: size,
                    startTime,
                    endTime,
                    beenPermission: 'false',
                    disposeUnitCode: code.split('_')[1],
                },
            })
            .then(res => {
                if (res.result === 0) {
                    res.data.list = res.data.rows;
                }
                return res.data;
            });
    },
    // 列表导出
    useWeighExport: params => {
        return downloadFile(
            '/cloud/zd/lm/api/cp/zdjgLmWeighData/exportData.smvc',
            {
                data: {
                    ...params,
                    tenantId,
                    beenPermission: 'false',
                },
            },
            'blob',
        );
    },
    // 图表数据
    useEchartsList: (id, startTime, endTime, code) => {
        const { data } = useRequest(
            () =>
                request.get('/cloud/zd/lm/api/np/weighSummary/sumDataByDate.smvc', {
                    data: {
                        id,
                        startDate: startTime,
                        endDate: endTime,
                        tenantId,
                        disposeUnitCode: code.split('_')[1],
                    },
                }),
            { refreshDeps: [id, startTime, endTime] },
        );
        return data?.data;
    },
};

const WorkCondition = {
    // 图表数据
    useEchartsTrend: (startTime, endTime, collectFrequency, deviceId, monitorTypeCodes) => {
        const { data } = useRequest(
            () =>
                request.get('/cloud/zd/jg/api/cp/historyValue/eCharts.smvc', {
                    data: {
                        tenantId,
                        startDateTime: startTime,
                        endDateTime: endTime,
                        collectFrequency,
                        deviceId,
                        monitorTypeCodes,
                    },
                }),
            {
                refreshDeps: [startTime, endTime, deviceId, monitorTypeCodes],
            },
        );
        return data?.data;
    },
    // 左上角页签值
    useDeviceList: selectKey => {
        return request.get('/cloud/zd/jg/api/cp/monitorType/getMonitorTypesByDeviceId.smvc', {
            data: {
                deviceId: selectKey,
                tenantId,
            },
        });
    },
    // 监测点下拉
    useSelectList: id => {
        const { data } = useRequest(
            () =>
                request.get('/cloud/zd/jg/api/np/device/select.smvc', {
                    data: { factoryId: id, tenantId },
                }),
            { refreshDeps: [id] },
        );
        return data?.data;
    },
};

const Audio = {
    // 下拉列表
    useVisTree: code => {
        const { data } = useRequest(() =>
            request.get('/cloud/vis/base/np/videoCommon/loadVideoInfos.smvc', {
                data: { tenantId, billId: code },
            }),
        );
        return data?.data;
    },
};

export { BaseData, WeighData, WorkCondition, Audio };
