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

<!--		<view class="roleNameView">-->
<!--			<text>当前身份：<text class="roleName">{{userInfo.roleName}}</text></text>-->
<!--		</view>-->

		<view class="dataNullView flex-col" v-if="roleBindRuleList.length===0">
			<view class="flex-row justify-center">
				<image class="dataNullImage" src="/static/zhanwu.png"></image>
			</view>
			<text class="roleHint">当前角色没有拓客规则，请联系拓客经理处理</text>
		</view>

		<view :style="{marginTop:'24rpx'}" class="scroll-Y" v-else>
				<view class="flex-col outView " :class="{select:item.select}" v-for="(item,index) in roleBindRuleList" @click="selectRule(item,index)">
					<view class="flex-row justify-between">
						<text class="title">{{item.title}}</text>
						<u-icon name="checkbox-mark" color="#666" size="24" v-if="item.select"></u-icon>
					</view>
					<view class="flex-row justify-between  awardView">
						<text>直推：</text>
						<view>
							<view class="flex-row  award ">
								<text class="awardItem">注册现金：{{item.awardAmount}}元</text>
							</view>
							<view class="flex-row  award justify-between">
								<text class="awardItem">消费现金：{{item.consumeAmount}}元</text>
								<text class="awardItem">消费比例：{{item.consumeRatio * 100}}%</text>
							</view>
						</view>
					</view>
					<view class="flex-row justify-between  awardView">
						<text>间推：</text>
						<view class="flex-row  award justify-between">
							<text class="awardItem">间推现金：{{item.indirectAmount}}元</text>
							<text class="awardItem">间推比例：{{item.indirectRatio * 100}}%</text>
						</view>
					</view>
					<view class="flex-row justify-between  awardView">
						<text>优惠券：</text>
						<view class="flex-col">
							<view class="flex-col discount award" v-for="(item1,index1) in item.strategyList" :key="index1">
								<text>{{item1.name}} * {{item1.degree}}张</text>
								<text>有效期：{{item1.startTime.substring(0,10)}} 至 {{item1.endTime.substring(0,10)}}</text>
							</view>
						</view>
					</view>
				</view>
		</view>
		<view class="confirmButton" @click="createQrCode">
			<text>生成二维码</text>
		</view>

		<uni-popup ref="createQrCodePopup" type="center">
			<view class="createQrCodePopup">
				<view class="flex-row justify-center">
					<text class="popupTitle">拓客二维码</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="540rpx" :value="qrcodeUrl" :options="options"></uv-qrcode>
				</view>
			</view>
		</uni-popup>
	</view>
</template>

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

			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
					this.roleBindRuleList[0].select = true
				})
			},

			createQrCode(){
				let data = {
					expandUserId:this.userInfo.id,
					ruleId:'',
					timestamp:new Date().getTime() + (5 * 60 * 1000)
				};
				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.$refs.createQrCodePopup.open()
			},
			closePopup(){
				this.qrcodeUrl=this.$xcxUrl+'?expandUserId={expandUserId}&ruleId={ruleId}&timestamp={timestamp}',
				this.$refs.createQrCodePopup.close()
			},
		}
	}
</script>

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