<template>
	<view class="parent">
		<view class="item" v-for="(item, index) in list" :key="index">
			<block v-if="item.component.name == 'fix-top-bar'">
				<fix-top-bar
					v-bind="item.component.props"
					:statusBar="statusBar"
					@jump="jump"
				></fix-top-bar>
			</block>
			<block v-if="item.component.name == 'fix-positioning'">
				<fix-positioning
					v-bind="item.component.props"
					:index="index"
					@jump="jump"
				></fix-positioning>
			</block>
			<block v-if="item.component.name == 'fix-search'">
				<fix-search
					v-bind="item.component.props"
					:statusBar="statusBar"
					:index="index"
					@jump="jump"
				></fix-search>
			</block>
			<block v-if="item.component.name == 'fix-banner'">
				<fix-banner v-bind="item.component.props" :index="index" @jump="jump"></fix-banner>
			</block>
			<block v-if="item.component.name == 'fix-menus'">
				<fix-menus v-bind="item.component.props" :index="index" @jump="jump"></fix-menus>
			</block>
			<block v-if="item.component.name == 'fix-goods-list'">
				<fix-goods-list v-bind="item.component.props" :index="index"></fix-goods-list>
			</block>
			<block v-if="item.component.name == 'fix-picture'">
				<fix-picture
					v-bind="item.component.props"
					:index="index"
					@jump="jump"
				></fix-picture>
			</block>
			<block v-if="item.component.name == 'fix-hot-image'">
				<fix-hot-image
					v-bind="item.component.props"
					:index="index"
					@jump="jump"
				></fix-hot-image>
			</block>
			<block v-if="item.component.name == 'fix-coupon'">
				<fix-coupon v-bind="item.component.props" :index="index"></fix-coupon>
			</block>
			<block v-if="item.component.name == 'fix-title'">
				<fix-title v-bind="item.component.props" :index="index" @jump="jump"></fix-title>
			</block>
			<block v-if="item.component.name == 'fix-empty'">
				<fix-empty v-bind="item.component.props" :index="index"></fix-empty>
			</block>
			<block v-if="item.component.name == 'fix-line'">
				<fix-line v-bind="item.component.props" :index="index"></fix-line>
			</block>
			<block v-if="item.component.name == 'fix-rich-text'">
				<fix-rich-text v-bind="item.component.props" :index="index"></fix-rich-text>
			</block>
			<block v-if="item.component.name == 'fix-list-menu'">
				<fix-list-menu
					v-bind="item.component.props"
					:index="index"
					@jump="jump"
				></fix-list-menu>
			</block>
			<block v-if="item.component.name == 'fix-rubik-cube'">
				<fix-rubik-cube
					v-bind="item.component.props"
					:index="index"
					@jump="jump"
				></fix-rubik-cube>
			</block>
			<block v-if="item.component.name == 'fix-video'">
				<fix-video v-bind="item.component.props" :index="index"></fix-video>
			</block>
			<block v-if="item.component.name == 'fix-suspension'">
				<fix-suspension v-bind="item.component.props" @jump="jump"></fix-suspension>
			</block>
			<block v-if="item.component.name == 'fix-wechat'">
				<fix-wechat v-bind="item.component.props"></fix-wechat>
			</block>
		</view>
	</view>
</template>

<script lang="ts" setup name="fix-index">
import { type PropType } from "vue";
import { useCool } from "/@/cool";
const { router } = useCool();

const props = defineProps({
	list: {
		type: Array as PropType<{ component: { name: string; props: Record<string, any> } }[]>,
		default: () => [],
	},
	statusBar: Boolean,
});

// 将字符串 id=1&t=2 转成对象
function strToObj(str: string): Record<string, string> {
	if (!str) return {};
	return str.split("&").reduce((acc: { [key: string]: string }, pair) => {
		const [key, value] = pair.split("=");
		acc[key] = value;
		return acc;
	}, {});
}

// 统一的跳转函数
function jump(link: { name: string; page: string; type: string; appid: string }) {
	let [path, param = ""] = link.page.split("?");
	const query = strToObj(param);
	switch (link.type) {
		case "goodsType":
			// 分类
			router.push({
				path,
				query,
			});
			break;
		case "goodsDetails":
			// 商品
			router.push({
				path,
				query,
			});
			break;
		case "index":
			// 平台
			router.push(path);
			break;
		case "web":
			// 网页
			router.push({
				path: "/pages/index/web",
				query: {
					url: link.page,
					title: link.name,
				},
			});
			break;
		case "applet":
			// 小程序
			try {
				// @ts-ignore
				wx.openEmbeddedMiniProgram({
					appId: link.appid,
					path: link.page,
					allowFullScreen: true,
				});
			} catch {
				// @ts-ignore
				wx.navigateToMiniProgram({
					appId: link.appid,
					path: link.page,
				});
			}
			break;
		case "fixtures":
			// 自定义
			router.push({
				path,
				query,
			});
			break;
		default:
			break;
	}
}
</script>
