<template>
	<view class="attach">
		<view class="flex-row justify-center">
			<view class="grid">
				<view class="flex-col gridItem" @click="navigateToPage('/pages/directPromotionList/directPromotionList')">
					<view  class="title flex-row justify-center">
						<text>累计邀约</text>
					</view>
					<view class="number flex-row justify-center">
						<text>{{homeStaticsData.totalInvite || 0}}</text>
					</view>
				</view>
				<view class="flex-col gridItem leftBorder">
					<view  class="title flex-row justify-center">
						<text>用券情况</text>
					</view>
					<view class="number flex-row justify-center">
						<text>{{homeStaticsData.useCoupon || 0}}/{{homeStaticsData.totalUseCoupon || 0}}</text>
					</view>
				</view>
				<view class="flex-col gridItem leftBorder">
					<view  class="title flex-row justify-center">
						<text>关联邀请</text>
					</view>
					<view class="number flex-row justify-center">
						<text>{{homeStaticsData.unionInvite || 0}}</text>
					</view>
				</view>
				<view class="flex-col gridItem" @click="navigateToPage('/pages/earningsList/earningsList')">
					<view  class="title flex-row justify-center">
						<text>业绩推广</text>
					</view>
					<view class="number flex-row justify-center">
						<text>{{homeStaticsData.performance || 0}}</text>
					</view>
				</view>
			</view>
		</view>


		<view class="flex-row">
			<view class="flex-col justify-center">
				<text class="roleSelectLabel">选择规则：</text>
			</view>
			<view class="roleSelect">
				<uni-data-select v-model="value" :localdata="roleBindRuleList" @change="selectChange" :clear="false"></uni-data-select>
			</view>
		</view>

		<view class="flex-row justify-center" v-if="showQrCode">
			<view class="qrView" @click="qrCodePopup">
				<uv-qrcode ref="qrcode" size="600rpx" :value="qrcodeUrl" :options="options"></uv-qrcode>
			</view>
		</view>

		<uni-popup ref="createQrCodePopup" type="center">
			<view class="createQrCodePopup flex-col justify-center">
				<view class="flex-row justify-center">
					<uv-qrcode ref="qrcode" size="600rpx" :value="qrcodeUrl" :options="options"></uv-qrcode>
				</view>
				<view class="xoIcon" @click="closePopup">
					<u-icon name="close-circle" color="#666" size="35"></u-icon>
				</view>
			</view>
		</uni-popup>

	</view>
</template>

<script>
	import commonUtils from '../../../common/js/utils/commonUtils'
	export default {
		props:{
			height: {
				type: String,
				default () {
					return ''
				}
			},
		},
		data() {
			return {
				homeStaticsData:{},
				qrCreateTime:'',
				roleBindRuleList:[],
				userInfo:{},
				qrcodeUrl:this.$xcxUrl+'?expandUserId={expandUserId}&ruleId={ruleId}&timestamp={timestamp}&type=1&expand1=1',
				options: {
					// 指定二维码前景，一般可在中间放logo
					foregroundImagePadding:2,
					foregroundImageBorderRadius:5,
					foregroundImageSrc: '/static/logo.png'
				},
				value:0,
				showQrCode:true
			}
		},
		created() {
			this.userInfo = uni.getStorageSync('spreadUserInfo')
			this.getRuleByRoleId(this.userInfo.roleId)
			this.homeStatics()
		},
		methods: {

			selectChange(e){
				this.createQrCode()
			},

			homeStatics(){
				this.$api.service.homeStatics({
					type:1
				}).then(res=>{
					console.log('获取首页拓客数据',res)
					this.homeStaticsData = res.data.data
				})
			},

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

			qrCodePopup(){
				this.$refs.createQrCodePopup.open()
				console.log('++++qrcodeUrl+++++',this.qrcodeUrl)
			},

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


			//查询角色已经绑定的规则
			getRuleByRoleId(roleId){
				this.$api.service.getRuleByRoleId({
					roleId:roleId
				}).then(res=>{
					this.roleBindRuleList = res.data.data
					for (let i = 0;i<this.roleBindRuleList.length;i++){
						this.roleBindRuleList[i].value = i;
						this.roleBindRuleList[i].text = this.roleBindRuleList[i].title
					}
					this.createQrCode()
				})
			},

			createQrCode(){
				if (!this.roleBindRuleList.length || this.roleBindRuleList.length ===0){
					this.showQrCode = false
					uni.showToast({
						icon: 'error',
						duration: 2000,
						title: '当前没有推广规则'
					});
					return
				}
				this.showQrCode = true
				let data = {
					expandUserId:this.userInfo.id,
					ruleId: this.roleBindRuleList[this.value].id,
					timestamp:new Date().getTime() + (60 * 1000 * 60 * 24 * 365 * 10)
				};
				this.qrcodeUrl = this.$xcxUrl+'?expandUserId={expandUserId}&ruleId={ruleId}&timestamp={timestamp}&type=1&expand1=1';
				for (let key in data) {
					let regexp = new RegExp("{" + key + "}"); // 构造正则表达式
					this.qrcodeUrl = this.qrcodeUrl.replace(regexp, data[key]); // 执行替换操作
				}
				console.log('++++qrcodeUrl+++++',this.qrcodeUrl)
			},

			closePopup(){
				this.$refs.createQrCodePopup.close()
			},
		}
	}
</script>

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