<template>
	<view>
		<index ref="首页" v-show="page == '首页'" :sa_storeid="sa_storeid" />
		<store ref="活动" v-show="page == '活动'" />
		<my-case ref="案例" v-show="page == '案例'" />
		<videos ref="视频" v-show="page == '视频'" />
		<user-center ref="我的" v-show="page == '我的'" />

		<workbench ref="工作台" v-show="page == '工作台'" />
		<dataBank ref="资料库" v-show="page == '资料库'" />
		<school ref="商学院" v-show="page == '商学院'" />
		<product ref="单品" v-show="page == '单品'" />

		<bottom-suspension-frame ref="pages" @onChange="pageChange" />
		<preloadedPicture />
	</view>
</template>

<script>
import index from './index/index'
import store from './index/store'
import myCase from './index/myCase'
import userCenter from './index/userCenter'
import videos from './index/videos'

import dataBank from './cloud/dataBank'
import school from './cloud/school'
import product from './cloud/product'
import workbench from './cloud/workbench'

import preloadedPicture from "./modules/preloadedPicture"
import bottomSuspensionFrame from "./modules/bottomSuspensionFrame";
export default {
	components: { bottomSuspensionFrame, index, store, myCase, userCenter, dataBank, school, product, workbench, videos, preloadedPicture },
	data() {
		return {
			swiperItemID: 'index',
			page: '首页',
			sa_storeid: 0,
			funs: [],
		}
	},
	onLoad(options) {
		console.log("options", options)
		if (options.wechatBindUserid) {
			this.funs.push({
				fun: this.wechatBind.bind(this),
				value: {
					wechatBindUserid: options.wechatBindUserid,
					userName: options.userName,
				}
			})
		} else if (options.sa_storeid) {
			this.sa_storeid = options.sa_storeid;
		}
		if (options.q) {
			let data = this.getUrlParams(options.q);
			switch (data.funName) {
				case 'wechatBind':
					this.funs.push({
						fun: this.wechatBind.bind(this),
						value: data
					})
					break;
				case 'getStore':
					this.sa_storeid = data.sa_storeid;
					break;
				case 'BindSalesman':
					this.funs.push({
						fun: this.bindSalesman.bind(this),
						value: data
					})
					break;
			}
		}
	},
	onShow() {
		this.isInitializeLogin(render.bind(this))
		function render() {
			if (this.funs.length) Promise.all(this.funs.map(v => v.fun(v.value))).then(() => this.funs = [])
			this.$Http.changePage = function (itemId, page, params) {
				this.$refs.pages.onClick(itemId, page, false, params)
			}.bind(this)
			// #ifdef MP-WEIXIN
			this.updatePageData(this.swiperItemID, this.page)
			// #endif
		}
	},
	methods: {
		updatePageData(itemId, page) {
			if (this.$refs[this.page].updatePage) this.$refs.pages.onClick(itemId, page)
		},
		pageChange(detail) {
			uni.setNavigationBarTitle({
				title: detail.name,
			})
			this.page = detail.name;
			try {
				uni.pageScrollTo({
					scrollTop: 0,
					duration: 0,
				})
				this.$refs[detail.name].init(detail.callBack, detail.params)
			} catch (error) {
				detail.callBack()
			}
			console.log("切换页面", detail)
		},
		wechatBind(user) {
			console.log("被邀请绑定账号", user);
			return new Promise((resolve, reject) => {
				let that = this;
				uni.showModal({
					title: '提醒',
					content: `账号"${user.userName}"是否与当前微信号绑定`,
					confirmText: '确定绑定',
					success: function ({ confirm }) {
						if (confirm) uni.login({
							success(res) {
								that.$Http.basic({
									"id": 20240516090402,
									"content": {
										"wechat_code": res.code,
										"isbinging": 1,
										"wechatuserinfo": "",
										"sys_enterprise_hrid": user.wechatBindUserid
									}
								}).then(s => {
									console.log("绑定账号", s)
									uni.showModal({
										title: '提示',
										content: s.msg == '成功' ? '绑定成功' : s.msg,
										showCancel: false,
										success: function () {
											resolve()
											if (s.msg == '成功') that.$Login.wechatLogin().then(() => uni.reLaunch({
												url: '/pages/index/index?q=""'
											}))
										}
									});
								})
							}
						})
					}
				});
			})
		},
		bindSalesman(user) {
			return new Promise((resolve, reject) => {
				console.log("被邀请绑定业务员", user);
				let that = this;
				uni.showModal({
					title: '提醒',
					content: `业务员账号"${user.username}"是否与当前微信号绑定`,
					confirmText: '确定绑定',
					success: function ({ confirm }) {
						if (confirm) that.$Http.basic({
							"id": 20240524090002,
							"content": {
								"hrid": user.hrid
							}
						}).then(s => {
							resolve()
							console.log("绑定账号", s)
							uni.showModal({
								title: '提示',
								content: s.msg == '成功' ? '绑定成功' : s.msg,
								showCancel: false,
								success: function () {
									if (s.msg == '成功') that.$Login.wechatLogin().then(() => uni.reLaunch({
										url: '/pages/index/index?q=""'
									}))
								}
							});
						})
					}
				});
			})
		}
	}
}
</script>

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