<template>
	<view class="attach">
		<view class="flex-row justify-center">
			<view class="grid">
				<view class="flex-col gridItem">
					<view  class="title flex-row justify-center">
						<text>累计邀约</text>
					</view>
					<view class="number flex-row justify-center">
						<text>2008</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>20/80</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>2800</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>2008</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">
			<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 {
				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,
			}
		},
		created() {
			this.userInfo = uni.getStorageSync('spreadUserInfo')
			this.getRuleByRoleId(this.userInfo.roleId)
		},
		methods: {

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

			qrCodePopup(){
				this.$refs.createQrCodePopup.open()
			},

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

			selectRule(item,index){
				this.roleBindRuleList.forEach(i=>{
					i.select=false
				})
				item.select = true
				this.$set(this.roleBindRuleList,index,item)
			},

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

			createQrCode(){
				let data = {
					expandUserId:this.userInfo.id,
					ruleId:'',
					timestamp:new Date().getTime() + (60 * 1000 * 60 * 24 * 365 * 10)
				};
				this.roleBindRuleList.forEach(item=>{
					if (item.select){
						data.ruleId= item.id
					}
				})

				if (!data.ruleId){
					uni.showToast({
						icon: 'error',
						duration: 2000,
						title: '请选择推广规则'
					});
					return
				}
				for (let key in data) {
					let regexp = new RegExp("{" + key + "}"); // 构造正则表达式
					this.qrcodeUrl = this.qrcodeUrl.replace(regexp, data[key]); // 执行替换操作
				}
				console.log('++++data+++++',data)
				console.log('++++qrcodeUrl+++++',this.qrcodeUrl)
				this.formatDate()
				this.$refs.createQrCodePopup.open()
			},
			createQrCodePopupChange(e){
				console.log(e)
				if (!e.show){
					this.qrcodeUrl=this.$xcxUrl+'?expandUserId={expandUserId}&ruleId={ruleId}&timestamp={timestamp}&type=1&expand1=1'
				}
			},
			closePopup(){
				this.$refs.createQrCodePopup.close()
			},
		}
	}
</script>

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