<template>
	<view class='my-page'>
		<view v-if="mineData" class='my-top'>

			<!-- 头部 -->
			<view class='my-header'>
				<image class='header-img' src='https://ddh.yyhock.com/ddhMall/my-icon.png'></image>
				<view class='header-des'>
					<view class='des-name' v-if="isLogin">{{ mineData['user']['cell'] }}</view>
					<view class='des-name' v-else @click='GoLogin'>注册/登录</view>
					<view class='des-text'>花花有米承诺保护您的信息安全</view>
				</view>
			</view>

			<!-- 我的还款&申请记录 -->
			<template v-if="isLogin && loanSwitch">
				<view class='my-line'>
					<view class='line-left' v-for="item in mineData['menuBar']" :key="item.id" @click="goBar(item)">
						<image class='left-img' :src='item.iconUrl'></image>
						<view class='l-text'>{{ item.title }}</view>
						<i class='l-go'></i>
					</view>
				</view>
			</template>


			<!-- 金刚栏位 -->
			<template v-if="mineData['extMenuBarList'] && mineData['extMenuBarList'].length > 0">
				<view class='my-box-s'>
					<view class='s-con'>
						<view class='con-item' @click="goPage(item)" v-for="item in mineData['extMenuBarList']"
							:key="item.id">
							<image class="item-img" :src="item.iconUrl"></image>
							<view class='item-text'>{{ item.title }}</view>
						</view>
					</view>
				</view>
			</template>

			<!-- 更多服务 -->
			<view v-if="mineData['baseMenuBar'] && mineData['baseMenuBar'].length > 0" class="my-contain">
				<i class='con-top'></i>
				<view class='con-box'>
					<view class="box-line" v-for="item in mineData['baseMenuBar']" @click='goItem(item)' :key="item.id">
						<view class='line-item'>
							<view class='item-l'>
								<image class='l-img' :src='item.iconUrl'></image>
								<view class='l-text'>{{ item.title }}</view>
							</view>
							<view class='item-r'></view>
						</view>
					</view>
				</view>
			</view>

			<!-- 联系我们 -->
			<view class='my-footer'>
				<view class='footer-btn' @click="GoTel">
					<i class='btn-tel'></i>
					<view class='btn-text'>联系我们</view>
				</view>
				<view class='footer-des'>周一至周日 09:00-18:00</view>
				<view class='footer-time'>(12:00-13:00休息)</view>
			</view>

		</view>
	</view>
</template>

<script>
import { switchShowTabbar } from '@/utils/index'
const $mineData = uni.getStorageSync('mineData') ? uni.getStorageSync('mineData') : null;

export default {
	data() {
		return {
			isLogin: false,
			loanSwitch: false,

			mineData: $mineData
		}
	},
	onShow() {
		const token = uni.getStorageSync('token')
		const loanSwitch = uni.getStorageSync('loanSwitch')

		this.isLogin = token ? true : false
		this.loanSwitch = loanSwitch ? true : false

		// 显示隐藏tabBar
		switchShowTabbar()
		this.initMine()
		// 埋点
		this.buryingPoint('app:my_pageView')
	},
	// 页面下拉刷新
	async onPullDownRefresh() {
		switchShowTabbar()
		await this.initMine()
		setTimeout(() => {
			uni.stopPullDownRefresh();
		}, 1000);
	},

	methods: {
		toJSON() { },
		// 跳转到登录
		GoLogin() {
			this.Go('uni:/pages/login/login')
		},
		// 初始化个人中心页面
		initMine() {
			const self = this
			self.$request('/app/v1/userIndex', 'GET').then(res => {
				self.mineData = res.result
				uni.setStorageSync('mineData', res.result)
			}).catch(err => {
				console.log(err, '初始化我的报错')
			})
		},
		goPage(item) {
			if (!this.isLogin) {
				return this.Go('uni:/pages/login/login')
			}
			switch (item.title) {
				case "我的浏览":
					this.buryingPoint('app:my_browsing_history_click')
					break;
				case "我的需求":
					this.buryingPoint('app:my_requests_click')
					break;
				case "我的关注":
					
					this.buryingPoint('app:my_favorites_click')
					break;
				case "用户反馈":
					this.buryingPoint('app:my_user_feedback_click')
					break;
				default:
					break;
			}

			let linkUrl = item.linkUrl;

			// 这里代码兼容
			if (linkUrl.includes('pages/')) {
				linkUrl = linkUrl.replace('pages/', 'subPackage/');
				console.log(linkUrl, '日日')
			}

			this.Go(linkUrl)
		},

		goBar(item) {
			try {
				const config = JSON.parse(uni.getStorageSync('APP-SETTING'));
				const timestamp = new Date().getTime()
				const token = uni.getStorageSync('tokenFinance')
				const mobile = uni.getStorageSync('l-mobile')
				const $src = config['h5Url'] + `?token=${token}&mobile=${mobile}&timestamp=${timestamp}#/${item.linkUrl}`
				const newSrc = $src.split('?')
				const targetSrc = `${newSrc[0]}?${encodeURIComponent(newSrc[1])}`
				switch (item.title) {
					case "我的还款":
						this.buryingPoint('app:my_repayment_click')
						break;
					case "申请记录":
						this.buryingPoint('app:my_application_record_click')
						break;
				}
				uni.navigateTo({
					url: `/pages/web/websrc?url=${targetSrc}`
				})

			} catch (err) {
				console.log(err)
			}
		},
		GoTel() {
			try {
				this.buryingPoint('app:my_contact_us_click')
				const APP_SETTING = uni.getStorageSync('APP-SETTING');
				const serviceCall = APP_SETTING ? JSON.parse(APP_SETTING)['phone'] : ''
				this.handleCall(serviceCall)
			} catch (err) {
				console.log('日')
			}
		},


		goItem(item) {
			if (!this.isLogin) {
				return this.Go('uni:/pages/login/login')
			}
			switch (item.title) {
				case "设置":
					this.buryingPoint('app:my_settings_click')
					break;
				case "帮助中心":
					this.buryingPoint('app:my_help_center_click')
					break;
				default:
					break;
			}

			if (item.linkUrl) {
				let linkUrl = item.linkUrl;
				if (linkUrl.includes('pages/')) {
					linkUrl = linkUrl.replace('pages/', 'subPackage/');
					console.log(linkUrl, '日日')
				}
				this.Go(linkUrl)
			}
		}
	}
}
</script>
<style src='./index.less' lang="less" scoped></style>
