<template>
	<view class="page" :style="{height:height}">

		<!-- #ifdef H5-->
		<uni-nav-bar v-if="!$isWxBrowser()" :fixed="true" background-color="#FFE05C" :border="false" :statusBar="false"
			title="个人中心" />
		<!-- #endif -->



		<view class="flex-row use">
			<view class="avatar" @click="goUserInfo">
				<image :src="userInfo.imgUrl || '/static/ud4.png'"></image>
			</view>
			<view class="flex-col justify-evenly useMsg">
				<view class="flex-row">
					<text class="name">{{userInfo.name}}</text>
					<!--					<view class="tag">{{userInfo.isManager === 1? '拓客经理': userInfo.roleTag == '0' ? '拓客专员': userInfo.roleTag == '1' ? '合作伙伴':'推广人员'}}-->
					<!--					</view>-->
				</view>
				<view>
					<text class="phone">电话：{{userInfo.phone}}</text>
				</view>
			</view>
		</view>

		<view  class="statistics flex-col" v-if="false">
			<view class="flex-row justify-around" v-if="userInfo.isManager == '1'">
				<view class="flex-row justify-center out ">
					<view class="flex-col  statisticsItem "
						@click="navigateTo('/pages/specialistsList/specialistsList')">
						<view>
							<image src="/static/my/fuwuNmber.png" class="statisticsImg"></image>
						</view>
						<text class="statisticsKey">我的专员(人)</text>
						<text class="statisticsValue">{{statistics.myAttache}}</text>
					</view>
				</view>
				<view class="flex-row justify-center out leftBorder">
					<view class="flex-col statisticsItem " @click="navigateTo('/pages/cooperation/cooperation')">
						<view>
							<image src="/static/my/hezuohuoban.png" class="statisticsImg"></image>
						</view>
						<text class="statisticsKey">合作伙伴(人)</text>
						<text class="statisticsValue">{{statistics.myPartner}}</text>
					</view>
				</view>
			</view>

			<view class="flex-row justify-around " v-else>
				<view class="flex-row justify-center out ">
					<view class="flex-col  statisticsItem "
						@click="navigateTo('/pages/directPromotionList/directPromotionList')">
						<!--						<view>-->
						<!--							<image src="/static/my/fuwuTimeLong.png" class="statisticsImg"></image>-->
						<!--						</view>-->
						<text class="statisticsKey">我的直推(人)</text>
						<text class="statisticsValue">{{statistics.myMember}}</text>
					</view>
				</view>
				<view class="flex-row justify-center out leftBorder" v-if="userInfo.roleTag == '0'">
					<view class="flex-col statisticsItem " @click="navigateTo('/pages/earningsList/earningsList')">
						<!--						<view>-->
						<!--							<image src="/static/my/benyuerenwu.png" class="statisticsImg"></image>-->
						<!--						</view>-->
						<text class="statisticsKey">推广收益(人)</text>
						<text class="statisticsValue">{{statistics.myEarning}}</text>
						<view v-if="isCanWithdraw" @click.stop="navigateTo('/pages/withdraw/withdraw')">
							<text class="txbtn">去提现></text>
						</view>
					</view>
				</view>
			</view>
		</view>

		<view class="flex-col justify-start list">
			<uni-list :border="false">
				<uni-list-item v-if="userInfo.isManager == 1" :clickable="true" title="我的分组" to="/pages/myGroup/myGroup" showArrow thumb="/static/my/fenzu.png" thumb-size="sm" />
				<uni-list-item v-if="userInfo.isExtensionWorker == 1" :clickable="true" title="推广码" showArrow thumb="/static/my/qr.png" thumb-size="sm" @click="openQrCord" />
				<uni-list-item v-if="userInfo.roleTag == 1" :clickable="true" title="购买优惠券" to="/pages/purchaseCoupon/purchaseCoupon" showArrow thumb="/static/my/coupon.png" thumb-size="sm" />
				<uni-list-item v-if="userInfo.isManager == 1" :clickable="true" title="拓客排行榜" to="/pages/spreadRankingList/spreadRankingList" showArrow thumb="/static/my/ranking.png" thumb-size="sm"/>
				<uni-list-item v-if="userInfo.isManager == 0" :clickable="true" title="我的规则" to="/pages/myRules/index" showArrow thumb="/static/my/ranking.png" thumb-size="sm"/>
				<uni-list-item v-if="userInfo.isManager == 1" :clickable="true" title="我的角色" to="/pages/myRole/index" showArrow thumb="/static/my/ranking.png" thumb-size="sm"/>
			</uni-list>
		</view>

<!--		<button @click="anniu">按钮</button>-->

		<uni-popup ref="qrCodePopup" type="center" @change="qrCodePopupChange">
			<view class="flex-col qrCodePopupView">
				<view class="flex-row justify-center">
					<!--					标题不要了-->
					<text class="popupTitle">&nbsp;</text>
				</view>
				<view class="xoIcon" @click="closePopup">
					<u-icon name="close-circle" color="#666" size="24"></u-icon>
				</view>
				<view class="flex-row justify-center">
					<uv-qrcode ref="qrcode" size="440rpx" :value="qrcodeUrl" :options="options"></uv-qrcode>
				</view>
				<view class="flex-row justify-center qrCreateTime">
					<text>生成时间：{{qrCreateTime}}</text>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import UniPopup from "../../uni_modules/uni-popup/components/uni-popup/uni-popup";
	import commonUtils from "../../common/js/utils/commonUtils";
	export default {
		components: {
			UniPopup
		},
		data() {
			return {
				qrcodeUrl: this.$xcxUrl + '?expandUserId={expandUserId}&ruleId={ruleId}&timestamp={timestamp}&type=',
				options: {
					// 指定二维码前景，一般可在中间放logo
					foregroundImagePadding: 2,
					foregroundImageBorderRadius: 5,
					foregroundImageSrc: '/static/logo.png'
				},
				qrCreateTime: '',
				isWxBrowser: false,
				isCanWithdraw: true,
				staticsData: {},
				userInfo: {},
				height: '',
				statistics: {},
			}
		},
		onLoad() {
			let sysInfo = uni.getSystemInfoSync()
			this.height = sysInfo.windowHeight + 'px'
			this.isWxBrowser = getApp().globalData.isWxBrowser
		},

		onShow() {
			this.userInfo = uni.getStorageSync('spreadUserInfo')
			this.getImgUrlByOssId(this.userInfo.img)
			this.exStatistics()
			// this.canWithdraw()
		},

		methods: {
			anniu() {
				uni.navigateTo({
					url: '/pages/register/register?roleId=1772472312711086081&parentId=1772471873559068674&timestamp=2032242661103'
				})
			},

			closePopup() {
				this.$refs.qrCodePopup.close()
			},

			formatDate() {
				this.qrCreateTime = commonUtils.formatDate(new Date())
			},

			openQrCord() {
				this.$api.service.getExtensionId().then(res => {
					let data = {
						ruleId: res.data.data,
						expandUserId: this.userInfo.id,
						timestamp: new Date().getTime() + (60 * 1000 * 60 * 24 * 365 * 10)
					};
					for (let key in data) {
						let regexp = new RegExp("{" + key + "}"); // 构造正则表达式
						this.qrcodeUrl = this.qrcodeUrl.replace(regexp, data[key]); // 执行替换操作
					}
					if (this.userInfo.isManager == '1') {
						this.qrcodeUrl = this.qrcodeUrl + '4' + '&expand1=4'
					} else {
						this.qrcodeUrl = this.qrcodeUrl + '3' + '&expand1=3'
					}
					this.formatDate()
					console.log('生成的链接', this.qrcodeUrl)
					this.$refs.qrCodePopup.open()

				})
			},

			qrCodePopupChange(e) {
				console.log(e)
				if (!e.show) {
					this.qrcodeUrl = this.$xcxUrl +
						'?expandUserId={expandUserId}&ruleId={ruleId}&timestamp={timestamp}&type='
				}
			},

			canWithdraw() {
				this.$api.service.canWithdraw().then(res => {
					console.log(res)
					this.isCanWithdraw = res.data.data
				})
			},

			exStatistics() {
				this.$api.service.exStatistics().then(res => {
					this.statistics = res.data.data
				})
			},

			navigateTo(url) {
				uni.navigateTo({
					url: url
				})
			},

			getImgUrlByOssId(Id) {
				if (Id) {
					this.$api.service.getImage(Id).then(res => {
						this.userInfo.imgUrl = res.data.data[0].url.replace(/^http:/, "https:")
						uni.setStorageSync('spreadUserInfo', this.userInfo)
						this.$set(this.userInfo)
					});
				}
			},

			navigateTo(url) {
				uni.navigateTo({
					url: url
				})
			},

			goUserInfo() {
				uni.navigateTo({
					url: '/pages/userInfo/userInfo'
				})
			},
		}
	}
</script>

<style lang="scss" scoped>
	@import './index.rpx.css';
</style>
