<template>
  <!-- 补充页面根模板结构（适配uni-app规范） -->
  <page-meta :page-style="themeColor"></page-meta>
  <view class="member-page">
    <!-- 原有页面内容（因你未提供完整模板，此处保留核心结构，按钮添加在最后） -->
    
  </view>
</template>

<script>
// 👇 引入语言工具文件
import langUtil from '@/common/js/lang.js';

export default {
	data() {
		return {
			diyRoute: '/pages/member/index'
		};
	},
	computed: {},
	watch: {
		storeToken: function(nVal, oVal) {
			if (nVal) {
				this.initData();
				if (uni.getStorageSync('source_member')) this.$util.onSourceMember(uni.getStorageSync(
					'source_member'));
			}
		}
	},
	onLoad(data) {
		//刷新多语言
		this.$langConfig.refresh();
		
		uni.hideTabBar();
		this.name = 'DIY_VIEW_MEMBER_INDEX';
		if (data.code) {
			this.$api.sendRequest({
				url: '/wechat/api/wechat/authcodetoopenid',
				data: {
					code: data.code
				},
				success: res => {
					if (res.code >= 0) {
						if (res.data.userinfo.nickName) this.modifyNickname(res.data.userinfo.nickName);
						if (res.data.userinfo.avatarUrl) this.modifyHeadimg(res.data.userinfo
							.avatarUrl);
					}
				}
			});
		}
	},
	onShow() {
		
		// 刷新会员数据
		if (this.$refs.diyGroup) {
			if (this.$refs.diyGroup.$refs.diyMemberIndex) this.$refs.diyGroup.$refs.diyMemberIndex[0].init();
			if (this.$refs.diyGroup.$refs.diyMemberMyOrder) this.$refs.diyGroup.$refs.diyMemberMyOrder[0].getOrderNum();
		}
	},
	methods: {
		// 👇 新增：语言切换方法
		toggleLang() {
			const currentLang = this.$lang?.locale || 'zh-cn';
			const targetLang = currentLang === 'zh-cn' ? 'en-us' : 'zh-cn';
			langUtil.change(targetLang);
		},
		
		/**
		 * 查询会员信息
		 */
		initData() {
			if (this.storeToken) {
				this.$nextTick(() => {
					this.$refs.nsNewGift.init();
					this.$refs.birthdayGift.init();
				});
			}
		},
		/**
		 * 修改昵称
		 * @param {Object} nickName
		 */
		modifyNickname(nickName) {
			this.$api.sendRequest({
				url: '/api/member/modifynickname',
				data: {
					nickname: nickName
				},
				success: res => {
					if (res.code == 0) {
						this.memberInfo.nickname = nickName;
						this.$store.commit('setMemberInfo', this.memberInfo);
					}
				}
			});
		},
		/**
		 * 修改头像
		 */
		modifyHeadimg(headimg) {
			this.$api.sendRequest({
				url: '/api/member/modifyheadimg',
				data: {
					headimg: headimg
				},
				success: res => {
					if (res.code == 0) {
						this.memberInfo.headimg = headimg;
						this.$store.commit('setMemberInfo', this.memberInfo);
					}
				}
			});
		},
	},
};
</script>

<style scoped>
/* 原有页面样式（保留） */
.member-page {
  width: 100%;
  min-height: 100vh;
  box-sizing: border-box;
}

/* 👇 新增：语言切换按钮样式 */
.lang-switch-wrap {
  position: fixed !important;
  bottom: 120rpx !important; /* 避开底部tabBar（若显示） */
  right: 30rpx !important;
  z-index: 99999 !important; /* 层级拉满 */
  width: 120rpx;
  height: 60rpx;
  background-color: #ffffff !important;
  border: 1px solid #e5e5e5 !important;
  border-radius: 30rpx !important;
  display: flex !important;
  align-items: center !important;
  justify-content: center !important;
  box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.1) !important;
}
.lang-text {
  font-size: 28rpx !important;
  color: #333333 !important;
  font-weight: 500 !important;
}
.lang-switch-wrap:active {
  transform: scale(0.95) !important;
  transition: transform 0.1s ease !important;
}
</style>