<template>
	<view class="ezy-my-page">
		<view class="icon-title-navBar-box my-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>
			<!-- 手机banner图 -->
			<swiper class="my-hyqy-swiper ezy-hidden-pad" circular :indicator-dots="hyqyData.indicatorDots" :autoplay="hyqyData.autoplay"
				:interval="hyqyData.interval" :duration="hyqyData.duration" indicator-color="#5195d3"
				indicator-active-color="#83d9ff" v-if="bannerArr">
				<!-- 1.数学 2.英语-->
				<swiper-item v-for="(item, index) in bannerArr" :key="index" class="hyqy-box" @click="hyqyBtn(item)">
					<img :src="item.addCover" class="my-banner-img" />
				</swiper-item>
			</swiper>
			<!-- padbanner图 -->
			<swiper class="my-hyqy-swiper ezy-hidden-phone" circular :indicator-dots="hyqyData.indicatorDots" :autoplay="hyqyData.autoplay"
				:interval="hyqyData.interval" :duration="hyqyData.duration" indicator-color="#5195d3"
				indicator-active-color="#83d9ff" v-if="bannerArr">
				<!-- 1.数学 2.英语-->
				<swiper-item v-for="(item, index) in bannerPadArr" :key="index" class="hyqy-box" @click="hyqyBtn(item)">
					<img :src="item.addCover" class="my-banner-img" />
				</swiper-item>
			</swiper>
			
			<!-- <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" v-if="isShowBindPhone" @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>

    <tipMiddleDialog ref="goLoRef" @confirm-btn="btntxt" :content="tishiTxt" qrBtnName="去登录"></tipMiddleDialog>

		<!-- 底部 -->
		<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 tipMiddleDialog from '@/components/dialog/tipMiddleDialog.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 isShowBindPhone = ref(false);
	let myInfoData = reactive({
		userImg: '',
		userName: '',
		credit: '',
		vipFlag: '',
		nickName: '',
		icon: '',
	});
	let pageFrom = ref(null)
  const goLoRef = ref(null)
  let tishiTxt = ref('您现在是游客身份，请先登录再进行兑换操作！')
  // banner
  let hyqyData = reactive({
  	indicatorDots: true,
  	autoplay: true,
  	interval: 10000,
  	duration: 500
  });
  const bannerArr = ref([
    { addCover: 'static/images/phone/my/my-banner1.png' }, // 替换为实际图片路径
    { addCover: 'static/images/phone/my/my-banner2.png' }  // 替换为实际图片路径
  ]);
  const bannerPadArr = ref([
    { addCover: 'static/images/pad/my/my-banner1.png' }, // 替换为实际图片路径
    { addCover: 'static/images/pad/my/my-banner2.png' }  // 替换为实际图片路径
  ]);

	onShow(() => {
		console.log('pageFrom.value', pageFrom.value);
		currentTabIndex.value = 3
		if (pageFrom.value === 'myInfo') {
			getMyInfo();
		}
		if (getUserIsYouke()) {
			isShowBindPhone.value = true
		} else {
			isShowBindPhone.value = false
		}
	})

  function btntxt() {
    loginRef.value.showDl()
  }

	/***************** 测试 ******************/
	const testRef = ref(null)

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

	// 获取用户数据
	function getMyInfo() {
		return myInfo({}).then(res => {
			console.log('res', res);
			myInfoData.userName = res.data.userName;
			myInfoData.nickName = res.data.nickName;
			pageFrom.value = ''
			appleCode.value = res.data.appleCode.toString()
			myInfoData.nickName = res.data.nickName;
			if (res.data.icon) {
				myInfoData.icon = res.data.icon;
			} else {
				getUserImg()
			}
		})
	}

	// 获取用户头像
	function getUserImg() {
		myInfoData.icon = 'static/images/common/my/head-img1.png'
	}

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

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

	function duihuamaDuihuan() {
		// 兑换码兑换策略
		if (getUserIsYouke()) {
			// 非登录 -- 登录
      goLoRef.value.handleShow();

		} else {
			// 已登录
			uni.navigateTo({
				url: '/pages/chanpinMy/duihuanma'
			})
		}
	}

	function handleSuccess(authInfo) {
		// 登录成功更改用户名
		// myInfoData.userName = authInfo.userName;
		getMyInfo().then(() => {
			// 前往兑换码
			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>