<template>
	<view style="background-color:#1015F2; height: 1400rpx;">
		<!-- 背景图 -->
		<view class="wh-w750" style=" position: relative;">
			<image :src="domain + '/static/mobile/images/lottery.png'" mode="aspectFill"
				style="height: 1338rpx; width: 750rpx;"></image>
		</view>
		
		<!-- 转盘 -->
		<view class="luck-list"
			style="position: absolute;top: 415rpx;right: 0;left: 72rpx; width: 600rpx;">
			<view v-for="(item,index) in AwardList" :key="index">
				<view class="cell-item" :class="index==4||index===sel?'y-b':'w-b'"
					@tap="begin(index)">
					<view class="cell-content"
						:class="index==4||index===sel?'y':'w'">
						<image :src="item.image" style="width: 60rpx; height: 60rpx;" v-if="item.image"></image>
						<view style="margin-top: 5rpx;">
							<text style="font-size: 21rpx; font-weight: bold;">{{item.name}}</text>
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		props: {
			id: [Number,String],
			status: Boolean,
			// 列表
			AwardList:{
				type:Array,
				default:[]
			}
		},
		data() {
			return {
				domain: '',
				sel: '',
				FastNums: 0,
				SlowNums: 0,
				time: 1000,
				LoopStatus: true,
				noClick: true,
				sjNum: 0
			}
		},
		created() {
			this.domain = uni.$http.getDomain();
		},
		methods: {
			// 中奖后的逻辑
			updateMoney(sjNum){
				this.$emit('updateMoney',sjNum);
			},
			// 开始抽奖
			begin(index) {
				if (index == 4) {
					if (!this.noClick) {
						uni.showToast({
							title: '请勿频繁点击',
							icon: "none",
							position: 'bottom'
						});
						return ;
					}
					
					if(!this.status){
						uni.showToast({
							title: '抽奖活动已结束',
							icon: "none",
							position: 'bottom'
						});
						return ;
					}
					
					uni.showLoading({ title: "加载中...", mask: false });
					uni.$http.get("activity.lottery/prize",{ id: this.id }).then(res=>{
						uni.hideLoading();
						if(res.code){
							for(let index in this.AwardList){
								let resData = this.AwardList[index];
								if(resData.id == res.data.prizeId){
									this.sjNum = index;
									break;
								}
							}
							
							this.noClick 	= false;
							this.FastNums 	= 0;
							this.SlowNums 	= 0;
							this.time 		= 200;
							this.LoopStatus = true;
							this.loop();
						}else{
							uni.$layer.showToast(res.msg);
						}
					});
				}
			},
			// 抽奖过程 控制
			loop() {
				let sjNum = this.sjNum
				if (!this.sel || this.sel < 9) {
					if (this.sel == 3) {
						this.sel = 0;
					} else if (this.sel === '') {
						this.sel = 0;
					} else if (this.sel == 2) {
						this.sel = 5;
					} else if (this.sel == 5) {
						this.sel = 8;
					} else if (this.sel == 8) {
						this.sel = 7;
					} else if (this.sel == 7) {
						this.sel = 6;
					} else if (this.sel == 6) {
						this.sel = 3;
					} else {
						this.sel++;
					}
					this.FastNums++;
					if (this.FastNums == 4) {
						this.FastNums = 0;
						this.time = 50;
						this.SlowNums++;
					}
					if (this.SlowNums == 8) {
						this.SlowNums = 0;
						this.time = 500;
						this.FastNums = 5;
					}
					if (this.FastNums > 5) {
						if (this.sel == sjNum) {
							this.noClick = true;
							this.LoopStatus = false;
							// 成功的逻辑
							this.updateMoney(sjNum);
						}
					}
					if (this.LoopStatus) {
						setTimeout(() => {
							this.loop();
						}, this.time);
					}
				}
			}
		}
	
	}
	
</script>

<style scoped>
    /* 抽奖 */
	.luck-list{
		display: flex;
		align-items: center;
		justify-content: center;
		flex-wrap: wrap;
	}
    .cell-item {
    	width: 150rpx;
    	height: 158rpx;
    	border-radius: 30rpx;
    	margin: 5rpx;
    }
    
    .cell-content {
    	width: 150rpx;
    	height: 138rpx;
    	border-radius: 30rpx;
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
    }
    
    .w {
    	background-color: #fdf2ee;
    }
    
    .w-b {
    	background-color: #f8d0c3;
    }
    .y {
    	background-color: #fee610;
    }

    .y-b {
    	background-color: #EFCD22;
    }
</style>
