const mp = uni.requireNativePlugin('uniMP');
import {
	uniMPgetLocation,
	test
} from "@/utils/uniMPfunction.js"
console.log(123)
export const initEvent = () => {
	mp.onUniMPEventReceive(async (ret) => {
		console.log('小程序事件: ', ret);

		// 用户如果没有注册商户 从小程序返回 入驻页面
		if (ret.event == 'closeAppToOpenShop') {
			mp.closeUniMP(ret.fromAppid, (ret) => {
				uni.navigateTo({
					url: "/pages/store/settled/index"
				})
			});
		}

		// 关闭app
		if (ret.event == 'backApp') {
			mp.closeUniMP(ret.fromAppid, (ret) => {});
		}

		if (ret.event == 'closeApp') {
			mp.closeUniMP(ret.fromAppid, (ret) => {
				console.log('closeUniMP: ' + JSON.stringify(ret));
			});
		}
		if (ret.event == 'getLocation') {
			try {
				console.log('获取定位');
				let res = await uniMPgetLocation();
				console.log(res);
				// plus.geolocation.getCurrentPosition(function(position) {
				//   console.log('经度:' + position.coords.longitude);
				//   console.log('纬度:' + position.coords.latitude);
				//   // that.markers[1].latitude = position.coords.longitude;
				//   // that.markers[1].longitude = position.coords.longitude;
				//   mp.sendUniMPEvent(
				//     ret.fromAppid,
				//     'getLocation', {...position.coords},
				//     (ret) => {
				//       console.log('Host sendEvent: ' + JSON.stringify(ret));
				//     });
				// }, function(error) {
				//   console.error('获取位置失败:', error.message);
				// }, {provider:'gps'});
				mp.sendUniMPEvent(
					ret.fromAppid,
					'getLocation', {
						...res
					},
					(ret) => {
						console.log('Host sendEvent: ' + JSON.stringify(ret));
					});
			} catch (e) {
				console.log(e);
			}
		}
		if (ret.event == 'test') {
			console.log('测试');
			mp.sendUniMPEvent(
				ret.fromAppid,
				'test', {
					'key': 'value',
					'name': 'data'
				},
				(ret) => {
					console.log('Host sendEvent: ' + JSON.stringify(ret));
				});
		}
		if (ret.event == 'log') {
			// console.log('2222222')

			console.log('收到小程序事件: ' + JSON.stringify(ret));
		}

		//直播小程序跳转商城购买商品
		if (ret.event == 'live_shop') {
			console.log(JSON.stringify(ret))
			let arr1 = ret.data
			mp.closeUniMP(ret.fromAppid, (ret) => {
				// console.log('closeUniMP: ' + JSON.stringify(ret));
				setTimeout(() => {
					uni.navigateTo({
						url: '/pages/goods_details/index?id=' + arr1
							.app_goods_id
					}, 1000)
				})
			});
			// console.log('收到小程序事件: ' + JSON.stringify(ret));
		}
	});
}