<template>
	<view class="ezy-my-page">
		<view class="icon-title-navBar-box">
			<view class="nav-bar-title"></view>
		</view>
		<view class="my-page-box">
			<view class="my-head-box">
				<icon class="head-img-box" :style="{backgroundImage: 'url(' + myInfoData.icon + ')'}"></icon>
				<view class="head-content-box">
					<text>{{myInfoData.nickName}}</text>
					<!-- <view class="ezy-jf-box" v-if="loginFlag">
						<icon class="jf-icon"></icon>
						<text class="jf-text">{{myInfoData.credit}}</text>
					</view> -->
				</view>
			</view>
			<!-- 图 -->
			<view class="my-banner-img"></view>

			<!-- 设置 -->
			<view class="my-list-box">
				<view class="list-item" @click="shangcheng">
					<icon class="list-icon sc-icon"></icon>
					<text>商城</text>
				</view>
				<view class="list-item" @click="ddBtn">
					<icon class="list-icon dd-icon"></icon>
					<text>订单</text>
				</view>
				<view v-if="appleCode=='true'&&currentPlatform=='ios' || currentPlatform=='android'" class="list-item" @click="duihuamaDuihuan">
					<icon class="list-icon dhm-icon"></icon>
					<text>兑换码</text>
				</view>
				<view class="list-item" @click="bangPhone">
					<icon class="list-icon bdsjh-icon"></icon>
					<text>绑定手机号</text>
				</view>
				<view class="list-item" @click="yingyongshezhi">
					<icon class="list-icon yysz-icon"></icon>
					<text>应用设置</text>
				</view>
			</view>


		</view>

		<goLogin ref="loginRef" @success="handleSuccess"></goLogin>
		<!-- 底部 -->
		<custom-tab-bar :show="true" :current-index="currentTabIndex" />
	</view>
</template>

<script setup>
	import cacheManager from '@/utils/cacheManager';
	import {
		myInfo,
		zhuxiao,
		duihuanmaCode,
		myCardList,
		commonCardList
	} from '@/api/my.js'
	import CustomTabBar from '@/components/custom-tabbar/index.vue';
	import goLogin from "@/components/goLogin/goLogin.vue"
	import {
		onLoad,
		onShow
	} from '@dcloudio/uni-app';
	import {
		reactive,
		ref
	} from "vue";
	import {
		getUserIsYouke,
		toast
	} from "../../utils/common";

	let appleCode = ref(null)
	let currentPlatform = ref(null);
	let authInfo = ref(null);
	let loginRef = ref(null);
	let currentTabIndex = ref(3)
	let myInfoData = reactive({
		userImg: '',
		userName: '',
		credit: '',
		vipFlag: '',
		nickName: '',
		icon: '',
	});
	let pageFrom = ref(null)

	onShow(() => {
		currentTabIndex.value = 3
		if (pageFrom.value === 'myInfo') {
			getMyInfo();
		}
	})
	/***************** 测试 ******************/
	const testRef = ref(null)

	function handleTest() {
		testRef.value.showPopup(7)
	}
	/***************** 测试 ******************/
	function bangPhone(){
		uni.navigateTo({
			url: '/pages/bindPhone/bindPhone?from=my'   
		})
	}

	function bingPhoneSuccess(flag, tel) {
		toast("绑定成功")
		cacheManager.updateObject('auth', {
			userName: tel,
		})
	}
	// 获取用户数据
	function getMyInfo() {
		myInfo({}).then(res => {
			myInfoData.userName = res.data.userName;
			myInfoData.nickName = res.data.nickName;
			pageFrom.value = ''
			appleCode.value = res.data.appleCode.toString()
			if (res.data.nickName) {
				myInfoData.nickName = res.data.nickName;
			} else {
				myInfoData.nickName = '鹅状元';
			}
			if (res.data.icon) {
				myInfoData.icon = res.data.icon;
			} else {
				getUserImg(res.data.growthType)
			}
		})
	}

	// 获取用户头像
	function getUserImg(data) {
		switch (data) {
			case 0:
				myInfoData.icon = 'static/images/my/head-img0.png'
				break;
			case 1:
				myInfoData.icon = 'static/images/my/head-img1.png'
				break;
			case 2:
				myInfoData.icon = 'static/images/my/head-img2.png'
				break;
			case 3:
				myInfoData.icon = 'static/images/my/head-img3.png'
				break;
			default:
				myInfoData.icon = 'static/images/my/head-unlogin-img.png'
				break;

		}
	}

	function yingyongshezhi() {
		pageFrom.value = 'myInfo'
		uni.navigateTo({
			url: '/pages/chanpinMy/myInfo'
		})
	}

	function ddBtn() {
		uni.navigateTo({
			url: '/pages/chanpinMy/order'
		})
	}

	function duihuamaDuihuan() {
		// 兑换码兑换策略
		if (getUserIsYouke()) {
			// 非登录 -- 登录
			loginRef.value.showDl()
		} else {
			// 已登录
			uni.navigateTo({
				url: '/pages/chanpinMy/duihuanma'
			})
		}
	}

	function handleSuccess(authInfo) {
		// 登录成功更改用户名
		myInfoData.userName = authInfo.userName;
		// 前往兑换码
		uni.navigateTo({
			url: '/pages/chanpinMy/duihuanma'
		})
	}

	onLoad((options) => {
		uni.hideTabBar()
		getAuthData();
		getMyInfo();
		isIOSorAndroid()
	})

	function isIOSorAndroid() {
		const systemInfo = uni.getSystemInfoSync();

		if (systemInfo.platform == 'ios') {
			currentPlatform.value = 'ios'
		} else {
			currentPlatform.value = 'android'
		}
	}

	function getAuthData() {
		authInfo.value = cacheManager.get('auth');
	}

	// 商城
	function shangcheng() {
		uni.navigateTo({
			url: '/pages/chanpinShop/index'
		})
	}
</script>
