<template>
	<view class="container">
		<view class="content"> </view>
	</view>
</template>

<script lang="ts" setup>
import { useOnLoad } from '@/hook';
import {
	useSetMenuList,
	useLoadViewMenu,
	useIsToAuthPage,
	useGetDefaultMember,
} from '@kasite/uni-app-base';
import { GetRoomId, GetMemberChatList } from '@kasite/uni-app-base/service/base';
import { common, menuClick } from '@/utils';
import { menu } from '@/config';

const app = getApp();

const main = async (options: any = {}) => {
	let menuList = common.isEmpty(uni.getStorageSync('menuList'))
		? await useSetMenuList(menu)
		: uni.getStorageSync('menuList');
	let skipWay = 'redirectTo'; // 跳转方式 默认关闭当前页面跳转
	let url = ''; // 页面地址
	/**
	 * 用于判断特定页面跳转是否需要查询就诊人
	 * 为true时 memberId为必须条件 前端才可反向查询当前人的数据
	 */
	let isQueryMember = false;
	/**
	 * 跳转特定页面需要查询就诊人时 且当前特定页面需要就诊卡或住院号 1：就诊卡 14 住院号
	 * 当当前页面为一定有卡号或住院号的 一定要传卡号 cardNo
	 */
	let cardType;
	let queryBean; // 跳转特定页面需要一些特定参数
	// 二维码扫码跳转进入
	if (options.q) {
		let url_q = decodeURIComponent(options.q);
		url_q.replace(/([^?&=]+)=([^&]+)/g, (_, k, v) => (options[k] = v));
		// 基层预约挂号支付
		if (url_q && url_q?.split('?')[1]?.split('=')[0] == 'OrderId') {
			options.type = 'pay';
			options.orderId = url_q?.split('?')[1]?.split('=')[1];
		}
	}
	if (common.isNotEmpty(options.type)) {
		if (options.type == 0) {
			skipWay = 'reLaunch';
			/** 首页 **/
			url = `/pages/st1/business/tabbar/homePage/homePage`;
			options.isViewMenu && (await useLoadViewMenu());
		} else if (options.type == 1) {
			skipWay = 'reLaunch';
			/** 互联网医院 **/
			url = `/pages/st1/business/tabbar/netHosIndex/netHosIndex`;
		} else if (options.type == 2) {
			skipWay = 'reLaunch';
			/** 消息中心 **/
			url = `/pages/st1/business/tabbar/MsgContent/MsgContent`;
		} else if (options.type == 3) {
			skipWay = 'reLaunch';
			/** 个人中心 **/
			url = `/pages/st1/business/tabbar/personalCenter/personalCenter`;
		} else if (options.type == 4) {
			/** 订单详情页 **/
			url = `/pagesPatient/st1/business/pay/payState/payState?orderId=${options.orderId}`;
		} else if (options.type == 5) {
			/** AI到诊回调科室排班页 **/
			let queryBean = {
				DeptCode: options.deptCode,
				DeptName: decodeURIComponent(options.deptName),
			};
			url = `/pagesPatient/st1/business/yygh/yyghDoctorList/yyghDoctorList?queryBean=${JSON.stringify(
				queryBean
			)}`;
		} else if (options.type == 'satisfactionQuestions') {
			// CRM随访满意度问卷
			url = `/pagesAdmin/satisfaction/satisfactionQuestions/satisfactionQuestions?subjectId=${options.subjectId}`;
		} else if (options.type == 'articleDetail') {
			// CRM文章详情
			url = `/pagesAdmin/article/business/article/detail/detail?id=${options.articleId}&msgId=${options.msgId}`;
		} else if (options.type == 'fuchat') {
			// HCRM-我的随访
			const chatData = await this.getMemberChatList(options);
			if (!chatData) return;
			app.globalData.currentUser = { memberId: chatData.MemberId };
			url = `/pagesCrm/st1/business/myFollowUp/followUpList/followUpList?type=${chatData.Type}&roomId=${chatData.Id}&deptName=${chatData.DeptName}&doctorSn=${chatData.Doctorsn}`;
		} else if (options.type == 'fuvisit') {
			// HCRM-我的随访
			const roomIdData = await this.getRoomId(options);
			options.roomId = roomIdData.RoomId;
			const chatData = await this.getMemberChatList(options);
			if (!chatData) return;
			app.globalData.currentUser = { memberId: chatData.MemberId };
			url = `/pagesCrm/st1/business/myFollowUp/followUpList/followUpList?type=${chatData.Type}&roomId=${chatData.Id}&deptName=${chatData.DeptName}&doctorSn=${chatData.Doctorsn}`;
		} else if (options.type == 'article') {
			// 点击分享的链接跳转文章详情页面
			url = `/pagesAdmin/teamYy/st1/business/disease/articlenInfo/articlenInfo?heaId=${ata.heaId}`;
		} else if (options.type == 'pay') {
			/** 订单支付页 **/
			url = `/pagesPatient/st1/business/pay/payMent/payMent?orderId=${options.orderId}`;
		} else if (options.type == 'myddc') {
			// 满意度调查
			let subjectId = options.subjectId;
			let taskId = options.taskId;
			url = `/pagesAdmin/satisfaction/business/satisfactionQuestions/satisfactionQuestions?subjectId=${subjectId}&taskId=${taskId}`;
		} else if (options.type == 'overduePerson') {
			let overdueOptions = common.isJSON(uni.getStorageSync('overdueOptions'))
				? JSON.parse(uni.getStorageSync('overdueOptions'))
				: uni.getStorageSync('overdueOptions');
			const options = overdueOptions.options;
			if (overdueOptions.route.indexOf('yyghClinicMsg') != -1) {
				app.globalData.queryBean = overdueOptions.options.queryBean;
			}
			overdueOptions.options.queryBean = common.isJSON(overdueOptions.options.queryBean)
				? overdueOptions.options.queryBean
				: JSON.stringify(overdueOptions.options.queryBean);
			const queryString = Object.keys(options)
				.map((key) => `${key}=${options[key]}`)
				.join('&');
			url = `/${overdueOptions.route}?${queryString}`;
			await checkAuthAndDefaultMember(url);
			uni.setStorageSync('overdueOptions', '');
		} else {
			// 不等于前面特定页面时 走菜单获取 type值为菜单名称
			const childArray = this.getAllChildren(menuList.map((item) => item.Children));
			let menuItem = childArray.filter((item) => item.MenuName == options.type);
			// 判断当前过滤的菜单名称列表不为空时，走定义的菜单跳转
			if (common.isNotEmpty(menuItem)) {
				menuItem = menuItem[0];
				menuClick(menuItem, this, skipWay);
				return;
			}
		}
	}
	if (common.isEmpty(url)) {
		common.showModal(`<<${options.type}>>跳转地址为空，请联系管理员`, () => {
			common.goToUrl(`/pages/st1/business/tabbar/homePage/homePage`, {
				skipWay: 'reLaunch',
			});
		});
		return;
	}
	common.goToUrl(url, {
		skipWay: skipWay,
	});
};
/** HCRM-我的随访-获取房间ID */
const getRoomId = async (data) => {
	let params = {
		Doctorsn: data.doctorsn,
		MemberId: data.memberId,
	};

	let { resp, resData } = await GetRoomId(params);
	if (common.isNotEmpty(resp)) {
		return resp[0];
	}
	return null;
};
/** HCRM-我的随访-获取会话列表 */
const getMemberChatList = async (data) => {
	const params = {
		MemberId: data.memberId,
		RoomId: data.roomId,
		ChatType: '0',
	};
	const { resp, resData } = await GetMemberChatList(params);
	if (common.isNotEmpty(resp[0])) {
		return resp[0];
	} else {
		common.showModal('未查询到记录');
		return null;
	}
};
/** 判断公众号授权和默认就诊人 */
const checkAuthAndDefaultMember = async (url) => {
	/**跳转到挂号页面 需要判断就诊人 和公众号授权*/
	if (useIsToAuthPage()) {
		return;
	}
	// 获取当前设置的患者信息 判断是否可以无卡预约 取不同的值允许无卡预约或者默认就诊人信息 否者获取就诊卡信息，如当前默认就诊人无就诊卡，跳转选卡界面
	let lastOperatorResp = await useGetDefaultMember({
		cardType: '1',
		url: url,
		isKeepPage: true,
		pageType: 'doctorYygh', //个性化预约挂号
	});
	// 判断返回的炒作人为null 代表跳转选择界面了
	if (lastOperatorResp === null) return;
	if (common.isEmpty(lastOperatorResp)) {
		app.globalData.toUrl = url; //保存添加成功后跳转地址
		app.globalData.cardType = app.globalData.withoutCard ? '' : '1'; //保存添加成功，要查询的默认人信息
		url = `/pagesPersonal/st1/business/patientManagement/addMember/addMember`;
	}
};
/** 菜单数组处理 */
const getAllChildren = (arr) => {
	return arr.flatMap((item) => {
		if (Array.isArray(item)) {
			return this.getAllChildren(item);
		} else if (item.Children) {
			return [item, ...this.getAllChildren(item.Children)];
		} else {
			return [item];
		}
	});
};

useOnLoad((options) => {
	uni.showLoading();
	main(options);
});
</script>

<style lang="scss" scoped></style>
