import request from '@/utils/request/index.js'
import {
	timeFormat
} from '@/utils/utils.js'

/**
 * 虫情监测模块
 */

//  虫情监测模块请求父类函数

const wormRequest = async (url, data) => {
	const res = await request.post(`api/api_gateway?method=forecast.worm_lamp.${url}`, data);
	return res?.data;
}

// 测报灯列表
export const getWormLampList = (params) => wormRequest('lamp_list', params);



// 测报灯详情
export const getWormLampDetails = (params) => wormRequest('device_status_data', params);

// 害虫预警
export const getPestWarningList = (params) => wormRequest('cbd_pest_warning', params);

// 图片列表
export const getWormImageList = async (params) => {
	const res = await request.post(`api/api_gateway?method=forecast.forecast_system.device_photo_list`, params);
	return res?.data;
}

// 害虫数据详情
export const getWormDetails = (params) => wormRequest('worm_list', params);

// 虫情分析-始见期
export const getPestRaiseInfo = (params) => wormRequest('pest_raise_info', params);

// 虫情分析-折线图
export const getPestStatisticsChart = async (params) => {
	const {
		char_data,
		pest_total,
		at_ah_info
	} = await wormRequest('pest_statistics_char_new', params);

	//	at_ah_info组装成 categories: [],series: [{name: "",data: []}] 格式
	let lineChart = {
		categories: [],
		series: [{
			name: '湿度',
			data: []
		}, {
			name: '温度',
			data: []
		}],
	}
	for (let i = 0; i < at_ah_info.length; i++) {
		lineChart.categories.push(timeFormat(at_ah_info[i].addtime, 'mm-dd'))
		lineChart.series[0].data.push(at_ah_info[i].ah)
		lineChart.series[1].data.push(at_ah_info[i].at)
	}
	// at_ah_info组装 完成
	//饼状图数据组装
	let pieChart = {
		series: [{
			data: []
		}]
	}
	for (let key in pest_total) {
		pieChart.series[0].data.push({
			name: key,
			value: pest_total[key]
		})
	}
	return {
		lineChart,
		pieChart,
		pestTotal: pest_total,
	}
};

// 虫情分析-害虫图片溯源
export const getPestImageSource = (params) => wormRequest('pest_image_source', params);

// 虫情分析-害虫基础信息溯源
export const getPestBaseData = (params) => wormRequest('pest_base_data', params);


// 历史数据
export const getWormLampHistory = (params) => wormRequest('device_history_data', params);
