<template>
	<view class="page">
		<!-- 顶部距离 -->
		<view class="header" :style="'margin-top: ' + statusBarHeight + 'px'"></view>
		<!-- 用户信息 -->
		<ws-user-card :data="userInfo" :is-login="isLogin" @openLogin="openLogin" />
		<block v-if="hideTools">
			<uni-card :border="false" :padding="'18px 8px'">
				<uv-skeleton rows="2" :loading="hideTools" :title="false"></uv-skeleton>
			</uni-card>
			<uni-card :border="false" :padding="'18px 8px'">
				<uv-skeleton rows="9" :loading="hideTools" :title="false"></uv-skeleton>
			</uni-card>
		</block>
		<block v-else>
			<!-- 收藏|关注|足迹|作品 -->
			<!-- <ws-user-collect></ws-user-collect> -->
			<!-- 我的资产 -->
			<ws-my-asset :data="userInfo" :is-login="isLogin" @done="openLogin"></ws-my-asset>
			<!-- 我的订单 -->
			<!-- <ws-user-order :order="order" :is-login="isLogin" @done="openLogin"></ws-user-order> -->
			<!-- 其他功能 -->
			<uni-card :border="false" :padding="0">
				<!-- 场馆管理可见-->
				<block v-if="hasMerchantRole || hasMerchantClerkRole">
					<view class="server" style="min-height: 200px">
						<uni-list :border="false">
							<block v-for="(item, index) in server" :key="index">
								<uni-list-item :thumb="item.icon" link @click="navTo(item)">
									<template v-slot:body>
										<view class="slot-box">
											<text class="slot-text" :style="{ color: item.color ? item.color : '#333333' }">{{ item.title }}</text>
										</view>
									</template>
								</uni-list-item>
							</block>
						</uni-list>
					</view>
				</block>
				<block v-else>
					<!-- 普通会员菜单 -->
					<view class="server" style="min-height: 200px">
						<uni-list :border="false">
							<block v-for="(item, index) in server" :key="index">
								<uni-list-item :thumb="item.icon" link @click="navTo(item)">
									<template v-slot:body>
										<view class="slot-box">
											<text class="slot-text" :style="{ color: item.color ? item.color : '#333333' }">{{ item.title }}</text>
										</view>
									</template>
								</uni-list-item>
							</block>
						</uni-list>
					</view>
				</block>
			</uni-card>
			<!-- 登录组件 -->
			<ws-login ref="login" :logo="logo" @done="reload" />
			<!-- 退出按钮 -->
			<ws-logout v-if="isLogin" @done="reload"></ws-logout>
		</block>
	</view>
</template>

<script>
import { useUserStore } from '@/store/modules/user';
import * as UserApi from '@/api/booking/users';
import * as MpMenuApi from '@/api/cms/mp-menu/index';
import * as UserCardApi from '@/api/booking/userCard';
import { updateUsers } from '@/api/shop/users';
import { openUrl } from '@/utils/common';
import { getWxOpenId } from '@/api/passport/login';

import { useTenantStore } from '@/store/modules/tenant';

const tenantStore = useTenantStore();

const userStore = useUserStore();

export default {
	data() {
		return {
			statusBarHeight: 104,
			isLogin: false,
			userInfo: {},
			fields: {},
			logo: '',
			bookingUser: {},
			merchantList: [],
			server: [],
			order: [],
			hideUser: true,
			hideTools: true,
			// 是否拥有场馆管理员权限
			hasMerchantRole: false,
			// 是否拥有场馆职员权限
			hasMerchantClerkRole: false,
			// 是否拥有超级管理员权限
			hasSuperAdminRole: false
		};
	},
	onLoad() {
		this.reload();
	},
	methods: {
		async reload(e) {
			const app = this;
			// 获取系统信息
			uni.getSystemInfo({
				success(data) {
					if (data) {
						app.statusBarHeight = data.statusBarHeight + 50;
					}
				}
			});
			// 当前租户信息
			const tenantInfo = await tenantStore.fetchTenantInfo();
			app.logo = tenantInfo.websiteLogo;
			if (tenantInfo.fields) {
				tenantInfo.fields.map((d) => {
					const key = d.name;
					app.fields[key] = d.value;
				});
			}

			// 当前登录用户信息
			const userInfo = await userStore.fetchUserInfo();
			if (Object.keys(userInfo).length > 0) {
				app.isLogin = true;
				app.userInfo = userInfo;
				uni.setStorageSync('user_id', app.userInfo.userId);

				// 判断用户角色
				app.hasMerchantRole = userInfo.roles.find((d) => d.roleCode == 'merchant');
				app.hasMerchantClerkRole = userInfo.roles.find((d) => d.roleCode == 'merchantClerk');
				app.hasSuperAdminRole = userInfo.roles.find((d) => d.roleCode == 'superAdmin');

				// 获取用户副表信息
				UserApi.getByPhone({
					phone: userInfo.phone
				}).then((data) => {
					if (data) {
						app.hideUser = false;
						app.userInfo.ouid = data.userId;
						app.userInfo = Object.assign({}, app.userInfo, data);
					}
				});
			} else {
				app.isLogin = false;
				app.userInfo = {};
			}
			// 菜单数据
			MpMenuApi.pageMpMenu({ type: 0, limit: 100 })
				.then((res) => {
					app.order = res.list.filter((d) => d.rows == 0);
					app.server = res.list.filter((d) => d.rows == 1);
					app.hideTools = false;
				})
				.catch(() => {
					uni.showToast({
						title: '请检查您的网络.',
						icon: 'none'
					});
					app.hideTools = false;
				});
				
			UserCardApi.pageUserCard({payStatus:1, userId: uni.getStorageSync('UserId')}).then(res => {
				console.log('res8888889: ',res);
				app.userInfo.cards = res.count
				app.userInfo.coupon = 1;
			})
			
			// #ifdef MP-WEIXIN
			uni.login({
				success: (res) => {
					getWxOpenId({
						code: res.code
					}).then((response) => {
						// console.log("response401: ", response);
						// console.log('app.userInfo.openid: ', app.userInfo.openid);
						if (!app.userInfo.openid) {
							updateUsers({
								userId: app.userInfo.ouid,
								openid: response.openid,
								sessionKey: response.session_key,
								unionid: response.unionid
							});
						}
					});
				}
			});
			// #endif
		},
		navTo(item) {
			if (!this.isLogin) {
				this.$refs.login.open('bottom');
				return false;
			} else {
				openUrl(item);
			}
		},
		// 弹出登录提示框
		openLogin() {
			if (!this.isLogin) {
				this.$refs.login.open('bottom');
				return false;
			}
		}
	}
};
</script>
<style>
page {
	background: url('https://oss.wsdns.cn/20240502/75fc744657544a38a32f83a825688286.png') no-repeat;
	background-size: 100%;
	background-repeat: no-repeat;
	background-color: #f0f2f5;
	width: 750rpx;
	overflow-x: hidden;
}
</style>
<style lang="scss"></style>