import { createPublish } from "applet-page-component";
import { queryToUrl } from "./index";

const getOptions = () => ({
	...createPublish(),
	data: {
		pageType: undefined,
		componentData: undefined,
		query: {},
		title: "",
		header: "show",
		background: "#1677FF",
		color: "#fff",
		backBtnColor: "#000000",
	},

	onReadyHeader({ height }) {
		const { componentData = {} } = this.data;
		componentData.headerHeight = height;
		this.setData({
			componentData: { ...componentData },
		});
	},

	async onLoad(query) {
		const {
			title,
			pageType,
			header = "show",
			background,
			color,
			backBtnColor,
			...other
		} = query;
		title &&
			my.setNavigationBar({
				title,
				backgroundColor: background || "#1677FF",
			});
		const app = getApp();
		const { globalData } = app;
		const { pageTemp = {} } = globalData;
		const componentData = pageTemp[pageType] || {};
		this.setData({
			title,
			header,
			pageType,
			componentData,
			query: other,
			background: background || "#1677FF",
			color: color || "#fff",
			backBtnColor: backBtnColor || false,
		});
		if (!app.globalData.templateUUID) {
			// 如果没有页面配置,需要获取页面配置,否则下面bind的时候没有templateUUID
			await this.dispatch("init", query);
			title &&
				my.setNavigationBar({
					title,
					backgroundColor: background || "#1677FF",
				});
		}
	},

	onUnload() {
		if (getApp().globalData.pageTemp) {
			delete getApp().globalData.pageTemp[this.data.pageType];
		}
	},

	// 分享
	onShareAppMessage() {
		const { pageType, query, title } = this.data;
		return {
			title,
			path: `${this.route}?pageType=${pageType}&title=${title}${queryToUrl(
				query
			)}`,
		};
	},
});

export default getOptions;