/*************** 扫码相关方法 ***************/
import {
	chemicalAppletSelectStockInfo,
	laboratorySubPassOutIsotopeSubSign,
} from '@/pages/api/index.js'

// 公共扫码-方法
function scanCode(path){
		/*************** H5方法 ***************/
		// #ifdef WEB
		uni.navigateTo({
			url: "/pages/views/scanCodePage?path="+path,
		});
		// #endif
		/*************** 微信方法 ***************/
		// #ifdef MP-WEIXIN
		uni.scanCode({
			onlyFromCamera: true,
			success: function (res) {
				scanTheCodeDataTreating(path,res.result);
			},
			fail: (err) => {
				uni.showToast({
					mask: true,
					icon: "none",
					position: "center",
					title: err,
					duration: 2000
				});
				return false
			}
		});
		// #endif
}

//公共扫码-参数处理方法
function scanTheCodeDataTreating(path,result){
	if(result.indexOf('type') != -1){
		//项目二维码
		let list = result.split("?")[1].split("&");
		let codeData = {};
		list.forEach((item) => {
			codeData[item.split("=")[0]] = item.split("=")[1];
		})
		if (codeData.type == 1 || codeData.type == 2 || codeData.type == 3 || codeData.type == 7 ||
			codeData.type == 8 || codeData.type == 10 ||
			codeData.type == 11 || codeData.type == 12 || codeData.type == 13 || codeData.type == 14) {
			// 基础扫码逻辑
			goCodePage(path,result);
		}else if(codeData.type == 5){
			// 同位素实验室相关-暂时注释
			// laboratorySubPassOutIsotopeSubSignF(path,codeData.subId,result);
			uni.reLaunch({
				url: '/pages/views/saoCode/saoCode?q=' + encodeURIComponent(result)
			});
		}else if(codeData.type == 9){
			chemicalAppletSelectStockInfoF(path,codeData.code);
		}
	}else{
		chemicalAppletSelectStockInfoF(path,result);
	}
}

//通用二维码页面
function goCodePage(path,result){
	if(path == 'home'){
		uni.reLaunch({
			url: '/pages/views/saoCode/saoCode?q=' + encodeURIComponent(result)
		});
	}else{
		uni.showToast({
			mask: true,
			icon: "none",
			position: "center",
			title: "当前页面无法使用该二维码",
			duration: 2000
		});
		setTimeout(() => {
			uni.navigateBack();
		}, 2000);
	}
}
//查询是否是同位素实验并且有签退
async function laboratorySubPassOutIsotopeSubSignF(path,subId,result){
	if(path == 'home'){
		const {
			data
		} = await laboratorySubPassOutIsotopeSubSign({subId:subId});
		if (data.code == 200) {
			if(data.data.isotope){
				let obj = {
					subId:subId,
					subName:data.data.subName,
					passOutId:data.data.passOutId,
				}
				uni.navigateTo({
					url: "/pages_basics/views/photoInspection?item=" + encodeURIComponent(JSON.stringify(obj)),
				});
			}else{
				uni.reLaunch({
					url: '/pages/views/saoCode/saoCode?q=' + encodeURIComponent(result)
				});
			}
		}
	}else{
		uni.showToast({
			mask: true,
			icon: "none",
			position: "center",
			title: "当前页面无法使用该二维码",
			duration: 2000
		});
		setTimeout(() => {
			uni.navigateBack();
		}, 2000);
	}
}
//扫码查询-化学品基本信息
async function chemicalAppletSelectStockInfoF(path,code){
	if(path == 'home'){
		const {
			data
		} = await chemicalAppletSelectStockInfo({tagCode:code});
		if (data.code == 200) {
			if(data.data){
				data.data.code = code;
				//化学品信息
				uni.navigateTo({
					url: "/pages_basics/views/chemicalsInfo/chemicalsInfo?item=" + encodeURIComponent(JSON.stringify(data.data)),
				});
			}else{
				uni.showToast({
					mask: true,
					icon: "none",
					position: "center",
					title: '未找到相关信息,请扫描正确的二维码',
					duration: 2000
				});
			}
		}
	}else{
		uni.showToast({
			mask: true,
			icon: "none",
			position: "center",
			title: "当前页面无法使用该二维码",
			duration: 2000
		});
		setTimeout(() => {
			uni.navigateBack();
		}, 2000);
	}
}

export { scanCode , scanTheCodeDataTreating }