import { ywtRequest } from "./request";
import { getAuthUser } from "../utils/jsapi";

// 进行授权
export async function ywtAuth(data) {
	const [err, res] = await ywtRequest({
		url: "/auth/bind",
		method: "post",
		data,
	});
	if (err || !res) return [err, null];
	return [null, res];
}

// 获取挂号时段
export async function doctorSchedule(data) {
	const [err, res] = await ywtRequest({
		url: "/register/doctorSchedule",
		method: "post",
		data,
	});
	if (err || !res) return [err, null];
	return [null, res];
}

// 获取就医须知
export async function getRegisteredAnnouncement(data) {
	const [err, res] = await ywtRequest({
		url: "/register/getRegisteredAnnouncement",
		method: "post",
		data,
	});
	if (err || !res) return [err, null];
	return [null, res];
}

// 获取就诊指南
export async function getGuideList(data) {
	const [err, res] = await ywtRequest({
		url: "/commonAnnouncement/getGuideList",
		method: "post",
		data,
	});
	if (err || !res) return [err, null];
	return [null, res];
}

// 支付完成后提交 authCode
export async function submitAuthCode(data) {
	const [err, res] = await ywtRequest({
		url: "/auth/submitAuthCode",
		method: "post",
		data,
	});
	if (err || !res) return [err, null];
	return [null, res];
}

// 消息推送授权
export async function getSubscribeAuth() {
	const [err, authResult] = await getAuthUser(["auth_user", "hospital_order"]);
	if (err) {
		my.showToast({
			type: "fail",
			content: "智能消息推送授权失败",
		});
		return [err, null];
	} else {
		const [error, result] = await submitAuthCode({
			authCode: authResult.authCode,
		});
		if (error) {
			my.showToast({
				type: "fail",
				content: "消息订阅失败",
			});
		}
		return [error, result];
	}
}
