<script lang="ts">
import RootStore from "@xkit-yx/im-store-v2";
// import V2NIM, { V2NIMConst } from "nim-web-sdk-ng/dist/v2/NIM_UNIAPP_SDK";
// import { getMsgContentTipByType } from "./pages/NEUIKit/utils/msg";
// import { STORAGE_KEY } from "./pages/NEUIKit/utils/constants";
// import { isWxApp } from "./pages/NEUIKit/utils";
// import { V2NIMMessage, V2NIMMessagePushConfig } from "nim-web-sdk-ng/dist/v2/NIM_UNIAPP_SDK/V2NIMMessageService";
// import { unix } from "dayjs";
import.meta.env.VITE_APP_THEMECOLOR = "#000";
// console.log("系统信息：", uni.getSystemInfoSync());

export default {
	globalData: {
		version: "1.0.0",
		serverVersion: "1.0.1",
		uid: 0,
		isUseNewJob: true,
		messageTab: 0,
		messageId: "",
		title: "",
		// themeColor: "#3578f6",
		// themeBackgroundColor: "#3578f621",
		themeColor: "#00B666",
		themeBackgroundColor: "#00B66621",
		logo: "",
		// #ifdef MP-WEIXIN
		"g-open-env": "MP-MINI",
		appId: uni.getAccountInfoSync().miniProgram.appId,
		// #endif
		// #ifdef MP-TOUTIAO
		"g-open-env": "MP-TOUTIAO",
		appId: tt.getEnvInfoSync().microapp.appId,
		// #endif
		// #ifdef MP-KUAISHOU
		"g-open-env": "MP-KUAISHOU",
		appId: uni.getAccountInfoSync().miniProgram.appId,
		// #endif
	},
	data() {
		return {
			themeColor: "#3578f6",
			themeBackgroundColor: "#3578f621",
			currentInfo: {},
		};
	},
	onShow: function (options) {
		let that = this;
		let requestComeFrom = this.G.globalConstantData.requestComeFrom;
		uni.setStorageSync("requestComeFrom", requestComeFrom && (requestComeFrom == "assistant_miniapp" || requestComeFrom == "supply_chain_miniapp") ? "isToB" : "isToC");

		uni.onNetworkStatusChange((res) => {
			// console.log("res", res);
			// console.log("网络状态改变");
		});
		if (that.globalData.appId == "wxb0c590fd696b79be") {
			that.globalData.title = "稻田工作";
			that.themeColor = "#ff4400";
			that.themeBackgroundColor = "#ff440011";
			that.globalData.themeColor = "#ff4400";
			that.globalData.themeBackgroundColor = "#ff440011";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/bocaigongyinglian/logo.png";
		} else if (that.globalData.appId == "wx15bc9c758f4eb129") {
			that.globalData.title = "业小满招聘";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/yexiaoman.png";
		} else if (that.globalData.appId == "wxcd0e4a001e1d87bd") {
			that.globalData.title = "会聘工作";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/kuaipin.jpg";
		} else if (that.globalData.appId == "wx581db3f39c49ed2b") {
			that.globalData.title = "英华招工";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/yinghua.png";
		} else if (that.globalData.appId == "wxd522e45fa0d4be09") {
			that.globalData.title = "鑫都直聘";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/xindu.png";
		} else if (that.globalData.appId == "wx5e1d6e017a193336") {
			that.globalData.title = "春雷人力";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/chunleilogo.png";
		} else if (that.globalData.appId == "wx57e965905e6b4530") {
			that.globalData.title = "人海劳务";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/renhailaowu.png";
		} else if (that.globalData.appId == "wx5fa8701bfff4357d") {
			that.globalData.title = "靖杨劳务";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/renhailaowu.png";
		} else if (that.globalData.appId == "wx70153a578d847bc1") {
			that.globalData.title = "打工猫";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/zhi_logo.png";
		} else if (that.globalData.appId == "wx89702db2259e0247") {
			that.globalData.title = "鑫职一聘";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/xinyi_logo.png";
		} else if (that.globalData.appId == "wx21f6c6445a194d4d") {
			that.globalData.title = "铂诚人力";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/pugongying/bocheng_logo.png";
		} else if (that.globalData.appId == "wxf14dd76421b8ce8f") {
			that.globalData.title = "工企创联";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/80d0daf4-82f8-4ddf-88dc-169dde051df8_gongqichuanglian.png";
		} else if (that.globalData.appId == "wxbc6d9dbb9dff5b37") {
			that.globalData.title = "伯才稻田";
			that.themeColor = "#ff4400";
			that.themeBackgroundColor = "#ff440011";
			that.globalData.themeColor = "#ff4400";
			that.globalData.themeBackgroundColor = "#ff440011";
			that.globalData.logo = uni.getStorageSync("miniApp-info").logo || "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/1shoudan/daotianLogo.png";
		}

		// #ifdef MP-WEIXIN

		const updateManager = uni.getUpdateManager();
		updateManager.onCheckForUpdate(function (res) {
			// console.log("检测是否有新版本：", res.hasUpdate);
		});
		updateManager.onUpdateReady(function (res) {
			uni.showModal({
				title: "更新提示",
				content: "新版本已经准备好，需重启应用",
				showCancel: false,
				success(res) {
					if (res.confirm) {
						// console.log("新版本已经准备好，需重启应用");
						updateManager.applyUpdate();
					}
				},
			});
		});
		updateManager.onUpdateFailed(function (res) {
			uni.showModal({
				title: "更新提示",
				content: "新版本下载异常，需重启应用",
				showCancel: false,
				success(res) {
					if (res.confirm) {
						// console.log("新版本下载异常，需重启应用");
						updateManager.applyUpdate();
					}
				},
			});
		});
		this.G.getAgencyInfo();
		this.G.getServiceQRCode();

		// #endif

		// #ifdef APP-PLUS
		// that.globalData.nim && that.globalData.nim.V2NIMSettingService.setAppBackground(false);
		// #endif

		if (uni.getStorageSync("apply-token")) {
			that.G.checkToken();
		}
	},
	onHide: function () {
		// #ifdef APP-PLUS
		// this.globalData.nim && this.globalData.nim.V2NIMSettingService.setAppBackground(true);
		// #endif
	},
	onLaunch(options) {
		let that = this;

		console.log("0127 APP", options);

		if (options.scene) {
			// console.log("XXXXXXXXXXXX");
			//扫小程序码携带参数
			var sceneStr = decodeURIComponent(options.query.scene);
			var sceneJson = this.G.sceneToJson(sceneStr);

			console.log("sceneJson===", sceneJson);

			if (sceneJson.tk) {
				uni.setStorageSync("apply-token", sceneJson.tk);
			}
		}

		// that.G.getAgencyInfo();
		uni.$on("isGlogin", function (data) {
			// console.log("app.vue 接收", data);
			if (uni.getStorageSync("apply-token")) {
				that.initWyyx();
			}
		});

		// const applyToken = uni.getStorageSync("apply-token");
		//   const applyUid = uni.getStorageSync("apply-uid");

		// 无token直接返回
		if (uni.getStorageSync("apply-token")) {
			try {
				// 有uid直接初始化；无uid则先校验token
				if (uni.getStorageSync("apply-uid")) {
					that.initWyyx();
				} else {
					that.G.checkToken().then(() => {
						console.log("已执行");
						that.initWyyx();
					});
				}
			} catch (err) {
				console.error("初始化wyyx失败：", err);
				// 可补充token失效后的处理，比如清除无效token、跳转登录页等
				// uni.removeStorageSync("apply-token");
				// uni.navigateTo({ url: '/pages/login/login' });
			}
		}

		// if (uni.getStorageSync("apply-token") && uni.getStorageSync("apply-uid")) {
		// 	// console.log("app.vue 是否调用");
		// 	that.initWyyx();
		// }else{
		// 	if(uni.getStorageSync("apply-token")){
		// 		that.G.checkToken().then(()=>{
		// 			console.log("已执行")
		// 			that.initWyyx();
		// 		})
		// 	}
		// }
		uni.removeStorageSync("selectedCity");
		uni.showShareMenu({
			shareTypes: "normal",
		});
		// this.getLocation().then(() => {
		// 	// console.log("that.currentInfo", that.currentInfo);
		// 	if (that.currentInfo.longitude) {
		// 		that.G.Get(that.api.getCityNameByLatLng, { lng: that.currentInfo.longitude, lat: that.currentInfo.latitude }, (res) => {
		// 			// console.log("res", res);
		// 			if (res) {
		// 				let currentCity = res.replace("市", "");
		// 				uni.setStorageSync("selectedCity", currentCity);
		// 				uni.$emit("setCity");
		// 			}
		// 		});
		// 	} else {
		// 		uni.$emit("setCity");
		// 	}
		// });
		// // 隐藏默认底部
		// uni.hideTabBar();
	},
	methods: {
		getLocation() {
			let that = this;
			return new Promise((resolve, reject) => {
				uni.authorize({
					// 检测是否授权位置信息
					scope: "scope.userLocation",
					success() {
						uni.getLocation({
							type: "wgs84",
							success(res) {
								that.currentInfo = {
									latitude: res.latitude,
									longitude: res.longitude,
									timestamp: new Date().getTime(),
								};
								resolve();
							},
							fail(err) {
								console.log(err);
							},
							complete() {
								resolve();
							},
						});
					},
					fail(err) {
						console.log(err);
						uni.showToast({
							title: "请在设置中开启定位权限以获取当前城市定位",
							icon: "none",
						});
						resolve();
						// that.showRefuseLocationPermission();
					},
				});
			});
		},
		initWyyx() {
			let that = this;
		},
		async initNim(opts) {
			// let that = this;
			// // 初始化 nim sdk
			// const nim = (uni.$UIKitNIM = V2NIM.getInstance(
			// 	{
			// 		appkey: opts.appkey,
			// 		needReconnect: true,
			// 		// "reconnectionAttempts": 5,
			// 		debugLevel: "warning",
			// 		apiVersion: "v2",
			// 		enableV2CloudConversation: true,
			// 	},
			// 	{
			// 		V2NIMLoginServiceConfig: {
			// 			lbsUrls: isWxApp ? ["https://lbs.netease.im/lbs/wxwebconf.jsp"] : ["https://lbs.netease.im/lbs/webconf.jsp"],
			// 			linkUrl: isWxApp ? "wlnimsc0.netease.im" : "weblink.netease.im",
			// 			/**
			// 			 * 使用固定设备 ID，
			// 			 */
			// 			isFixedDeviceId: true,
			// 		},
			// 	},
			// ));
			// that.globalData.nim = nim;
			// // 初始化 store
			// const store = (uni.$UIKitStore = new RootStore(
			// 	nim,
			// 	{
			// 		addFriendNeedVerify: false,
			// 		// 是否需要显示 p2p 消息、p2p 会话列表消息已读未读，默认 false
			// 		p2pMsgReceiptVisible: true,
			// 		// 是否需要显示群组消息已读未读，默认 false
			// 		teamMsgReceiptVisible: true,
			// 		teamAgreeMode: V2NIMConst.V2NIMTeamAgreeMode.V2NIM_TEAM_AGREE_MODE_NO_AUTH,
			// 		sendMsgBefore: async (options: { msg: V2NIMMessage; conversationId: string; serverExtension?: Record<string, unknown> }) => {
			// 			const pushContent = getMsgContentTipByType({
			// 				text: options.msg.text,
			// 				messageType: options.msg.messageType,
			// 			});
			// 			// options.serverExtension = JSON.stringify({
			// 			// 	messageComeFrom: 'zgb',
			// 			// 	messageComeFromAppId: uni.getAccountInfoSync().miniProgram.appId
			// 			// })
			// 			const yxAitMsg = options.serverExtension
			// 				? options.serverExtension.yxAitMsg
			// 				: {
			// 						forcePushIDsList: "[]",
			// 						needForcePush: false,
			// 						test: "qqq",
			// 					};
			// 			// 如果是 at 消息，需要走离线强推
			// 			const { forcePushIDsList, needForcePush } = yxAitMsg
			// 				? // @ts-ignore
			// 					store.msgStore._formatExtAitToPushInfo(yxAitMsg, options.msg.text)
			// 				: { forcePushIDsList: "[]", needForcePush: false };
			// 			// console.log("forcePushIDsList: ", forcePushIDsList);
			// 			const { conversationId } = options;
			// 			const conversationType = nim.V2NIMConversationIdUtil.parseConversationType(conversationId);
			// 			const targetId = nim.V2NIMConversationIdUtil.parseConversationTargetId(conversationId);
			// 			// console.log("targetIdtargetId app", targetId, " 当前：", uni.getStorageSync("ima-cod"));
			// 			const pushPayload = JSON.stringify({
			// 				// oppo
			// 				oppoField: {
			// 					click_action_type: 4, // 参考 oppo 官网
			// 					click_action_activity: "", // 各端不一样 TODO
			// 					action_parameters: {
			// 						sessionId: targetId,
			// 						sessionType: conversationType,
			// 					}, // 自定义
			// 				},
			// 				// vivo
			// 				vivoField: {
			// 					pushMode: 0, //推送模式：0 表示正式推送、1 表示测试推送。不填则默认为 0。
			// 				},
			// 				// huawei
			// 				hwField: {
			// 					click_action: {
			// 						type: 1,
			// 						action: "", // 各端不一样 TODO
			// 					},
			// 					androidConfig: {
			// 						category: "IM",
			// 						data: JSON.stringify({
			// 							sessionId: targetId,
			// 							sessionType: conversationType,
			// 						}),
			// 					},
			// 				},
			// 				// 通用
			// 				sessionId: targetId,
			// 				sessionType: conversationType,
			// 			});
			// 			const pushConfig: V2NIMMessagePushConfig = {
			// 				pushEnabled: uni.getStorageSync("im-robotOpen") == 1 ? true : false,
			// 				pushNickEnabled: true,
			// 				forcePush: needForcePush,
			// 				forcePushContent: pushContent,
			// 				forcePushAccountIds: forcePushIDsList,
			// 				pushPayload: "{}",
			// 				pushContent,
			// 			};
			// 			options.messageComeFrom = "zgb";
			// 			options.messageComeFromAppId = uni.getAccountInfoSync().miniProgram.appId;
			// 			return { ...options, pushConfig };
			// 		},
			// 	},
			// 	"UniApp",
			// ));
			// // #ifdef APP-PLUS
			// const nimPushPlugin = uni.requireNativePlugin("NIMUniPlugin-PluginModule");
			// nim.V2NIMSettingService.setOfflinePushConfig(nimPushPlugin, {
			// 	miPush: {
			// 		appId: "2882303761520397320",
			// 		appKey: "5102039734320",
			// 		certificateName: "xiaomiCertificateName",
			// 	},
			// 	vivoPush: {
			// 		certificateName: "vivoCertificateName",
			// 	},
			// 	oppoPush: {
			// 		appId: "32967155",
			// 		appKey: "b98618cb272944dea324af6421d42a79",
			// 		secret: "0b7ce705a2304a17b78f20011c18890c",
			// 		certificateName: "opopCertificateName",
			// 	},
			// 	honorPush: {
			// 		certificateName: "rongyaoCertificateName",
			// 	},
			// 	apns: {
			// 		// certificateName: 'AuthKey_98P8URRXUD_test', //测试用:
			// 		certificateName: "AuthKey_98P8URRXUD", //正式用
			// 	},
			// });
			// // console.log("是否执行 推送");
			// // #endif
			// nim.V2NIMLoginService.login(opts.account, opts.token).then(() => {});
			// nim.V2NIMConversationService.on("onConversationChanged", function (conversationList: V2NIMConversation[]) {
			// 	// console.log("初始化监听会话:", conversationList);
			// 	let _bool = conversationList.filter((item) => {
			// 		return Number(item.unreadCount) > 0;
			// 	});
			// 	// console.log("初始化监听会话bool:", _bool);
			// 	if (_bool && _bool.length > 0) {
			// 		that.playAudio(); // 播放音频
			// 	}
			// });
		},
		playAudio() {
			let that = this;
			const innerAudioContext = uni.createInnerAudioContext();
			innerAudioContext.autoplay = true;
			innerAudioContext.src = "https://bocai-cms.oss-cn-beijing.aliyuncs.com/web-resource/ibocai/tip.mp3";
			innerAudioContext.onPlay(() => {
				// console.log("开始播放");
			});
			uni.vibrateShort();

			innerAudioContext.onError((res) => {
				// console.log(res.errMsg);
				// console.log(res.errCode);
			});
		},
	},
};
</script>
<style lang="scss">
@import "./uni_modules/vk-uview-ui/index.scss";
@import "./static/css/base.scss";
@import "./static/css/iconfont.css";

uni-page-body {
	height: 100%;
}

uni-page-body > uni-view {
	height: 100%;
}

.g-components-panel-hr {
	padding-bottom: 16px;

	.doc-left {
		width: 43px;
		height: 2px;
		border: 1px solid;
		border-image: linear-gradient(270deg, rgba(255, 255, 255, 0.2), #cccccc) 1 1;
		transform: rotate(180deg);
	}

	.doc-right {
		width: 43px;
		height: 2px;
		border: 1px solid;
		border-image: linear-gradient(270deg, rgba(255, 255, 255, 0.2), #cccccc) 1 1;
	}
}
</style>
