import request from '@/utils/request/index.js'
import config from '@/config/config.js'
import cache from '@/utils/cache'
import {
	LOGIN_TOKEN
} from '@/config/cache'
/**
 * 病害监测模块
 */

// 识别基类 (uni.request 对formData数据不支持，暂时先写成独立的基类，后期遇到好的方案可改进)
export const baseDiscern = (url, img_file) => {
	return new Promise((resolve, reject) => {
		uni.uploadFile({
			url: config.baseUrl +
				'api/api_gateway?method=pest.pests.' + url, //仅为示例，非真实的接口地址
			formData: {
				'img_file': img_file,
				token: cache.get(LOGIN_TOKEN)
			},
			success: (res) => {
				let resule=JSON.parse(res.data ?? '{}')
				if(resule.message){
					uni.showToast({
						title:resule.message,
						duration: 1500,
						mask: true,
						icon: 'none'
					});
					return resolve(null);
				}

				resolve(resule.data);
			},
			fail:(e) =>{
				resolve(null);
			}
		});
	})
}


// 病虫害库
export const loadPestList = async (data) => {
	const res = await request.post(`api/api_gateway?method=pest.pests.pests_info`, data);
	return res?.data;
}

// 虫害识别
export const handleInsectDiscern = (img_file) => baseDiscern('insect_discern', img_file);

// 病害识别
export const handlePlantDiscern = (img_file) => baseDiscern('plant_discern', img_file);
