<template>
	<view class="prizePage" >
		<view class="prizeBg" :style="{'background-image':`url('${ticketInfo.bg_image}')`}"></view>
		<view class="prizeContent" :style="{'background-image':`url('${ticketInfo.bg_image}')`}">
			<!-- <view class="bg" :style="{'background-image':`url('${ticketInfo.bg_image}')`}" v-if="gai"></view> -->
			<view class="scratch">
			  <view class="box">
			    <!-- 刮奖结果图片 -->
					<view :class="showBackboard?'zIndex1':'hide'" style="" class="result_img">
						<view style="font-size: 12px;text-align: center;margin-top: 8px;">中奖号码</view>
						<view class="img_row" style="border-bottom: 1px solid #333;width: 90%;margin: 5px auto;">
							<view v-for="(prizeItem,prizeIndex) in prizeList" :key="prizeIndex" class="data_item">
								<image :src="prizeItem" mode="aspectFill"></image>
							</view>
						</view>
						<view v-for="(item,index) in dataList" :key="index" class="img_row">
							<view v-for="(lineItem, lineIndex) in item" :key="lineIndex" class="data_item"
								style="border-bottom: 1px dashed #333;">
								<image :src="lineItem.image" mode="aspectFill"></image>
								<view class="">￥{{lineItem.amount}}</view>
								<view style="font-size: 12px;font-weight: normal;transform: scale(0.8);">{{lineItem.pinyin}}</view>
								<view class="circle" :class="(lineItem.is_awards&&awards_amount)?'show':''"></view>
							</view>
						</view>
					</view>
			    <!-- 刮奖canvas容器 -->
			    <canvas v-if="!isWin && !isThank"
			      class="canvas-box"
			      canvas-id="guagua"
			      :disable-scroll="true"
			      @touchstart="touchStart"
			      @touchmove="touchMove"
			      @touchend="touchEnd"
			    ></canvas>
			    <!-- 刮奖前提示消息，开始刮奖后隐藏 -->
			    <cover-view class="tip" v-if="!toDraw">
			      <!-- 开始刮奖按钮 -->
			      <cover-view class="btn" @tap="scratchStart()">
			        <cover-view class="text">立即刮奖</cover-view>
			      </cover-view>
			    </cover-view>
			  </view>
				
			</view>
			<view class="mask" v-if="showResult">
				
			</view>
			<view class="winPrize" v-if="isWin">
				{{awards_amount}}元
				<cover-view class="again" @click="again"></cover-view>
				<view class="close" @click="close(1)"></view>
			</view>
			<view class="noPrize_box" v-if="isThank">
				<cover-view class="noPrize" v-if="isThank"></cover-view>
				<cover-view class="again" v-if="isThank"></cover-view>
				<cover-view class="again_mask" @click="again" v-if="isThank"></cover-view>
				<cover-view class="close" @click="close(2)" v-if="isThank"></cover-view>
			</view>
			
		</view>
	</view>
  
</template>
 
<script>
import API from '@/common/js/api.js'
import Scratch from './scratch.js'
// import VConsole from './renderjs.js'
export default {
  data () {
    return {
      id: '',
			ticketInfo: {
				// bg_image: '../../static/home/4.jpg',
				bg_image: '',
				price: '20',
				content: `刮开覆盖膜，如果出现背书标志“1倍”、“2倍”、“5倍”、“10倍”、“20倍”、
					“50倍”、“100倍”，既中得该标志下方所示的金额乘以该倍数；
					如果在“全中”区出现标志“赢”，即中图刮开区内所示的20个金额之和。`
			},
			awards_amount: 0,
			prizeList: ['','',''],
			// dataList: [
			// 	[
			// 		{image: '',amount:'100.00'},
			// 		{image: '',amount:'100.00'},
			// 		{image: '',amount:'100.00'}
			// 	],
			// 	[
			// 		{image: '',amount:'100.00'},
			// 		{image: '',amount:'100.00'},
			// 		{image: '',amount:'100.00'},
			// 		{image: '',amount:'100.00'}
			// 	],
			// 	[
			// 		{image: '',amount:'100.00'},
			// 		{image: '',amount:'100.00'},
			// 		{image: '',amount:'100.00'}
			// 	],
			// ],
			dataList: [],
      toDraw: false, // 是否开始刮奖,
      showResult: false, // 是否显示抽奖结果
      isWin: false, // 是否中奖
      scratchWidth: 330, // 绘制刮奖范围宽-375屏幕330
      scratchHeight: 300, // 绘制刮奖范围高-375屏幕300
      scratchSize: 10, // 触手大小
      scratchScale: 0.8, // 需刮开百分比
      prize_img: '', // 中奖图片
      isScratching: false, // 是否正在刮奖,
			isThank: false,
			guaCanvas: null,
			isShow: false,
			c_r_id: '',
			gai: true,
			screenWidth: 375,
			showBackboard: false,// 页面初始，底下的结果层先不渲染
    }
  },
	updated() {
		// console.log(this.show);
	},
	watch: {
		isShow(n, o){
			console.log(n,o);
			if(n && n!=o){
				this.showResult = true;
				this.gai = false;
				this.doPrizeScratchTicketFun();
			}
		}
	},
	created() {
		let _this = this;
		uni.getSystemInfo({
			success(res) {
				// console.log(res, '系统');
				_this.screenWidth = res.screenWidth;
				_this.scratchWidth = 0.88 * _this.screenWidth || 330;
				_this.scratchHeight = 0.8 * _this.screenWidth || 300;
			}
		})
	},
	onLoad(e) {
		uni.setNavigationBarTitle({
			title: e.type,
			// +'元专区'
		})
		this.id = e.id;
		this.initInfo();
	},
  methods: {
		initInfo(){
			API.request('/zone/beginInitInfo', {zone_goods_id: this.id}, res=>{
				this.ticketInfo = res.data.data;
			})
			
			let timer = setInterval(()=>{
				if(this.guaCanvas && this.guaCanvas.show){
					this.isShow = true;
					clearInterval(timer);
				}
			}, 500)
		},
    initCanvas () {
      // 刮奖初始化信息必须在onReady后，不然h5不支持（小程序可在onLoad执行）
      this.guaCanvas = new Scratch(this, {
        canvasId: 'guagua',
        width: this.scratchWidth,
        height: this.scratchHeight,
        size: this.scratchSize,
        scale: this.scratchScale,
				bgImg: this.ticketInfo.bg_image,
				screenWidth: this.screenWidth
      })
			
    },
		// 开始刮奖获取信息
		beginLottery(){
			API.beginLottery({zone_goods_id: this.id}, res=>{
				this.c_r_id = res.data.c_r_id;
				this.toDraw = true;
				this.initCanvas();
				this.dataList = res.data.list;
				this.prizeList = res.data.prizes_data;
				// console.log(this.dataList, 'dataList----');
				setTimeout(()=>{
					this.showBackboard = true;
				}, 1000)
			})
		},
    // 请求刮奖结果
    doPrizeScratchTicketFun () {
			uni.showLoading({
				title: '加载中'
			})
      API.endLottery({c_r_id: this.c_r_id}, res=>{
				uni.hideLoading();
				if(res.data.awards_amount){
					this.awards_amount = res.data.awards_amount;
					this.isWin = true;
					this.guaCanvas = null;
				}else{
					this.isWin = false
					this.isThank = true;
					this.guaCanvas = null;
				}
				
			}, fail=>{
				// this.isScratching = false
				uni.hideLoading();
			})
    },
    // 点击按钮开始刮奖
    scratchStart () {
      this.beginLottery();
    },
		// 再刮一次
		again(){
			this.isWin = false;
			this.isThank = false;
			this.showResult = false;
			this.gai = true;
			this.isScratching = false;
			this.isShow = false;
			this.showBackboard = false;
			this.awards_amount = 0;
			
			API.request('/zone/beginInitInfo', {zone_goods_id: this.id}, res=>{
				this.ticketInfo = res.data.data;
				this.beginLottery();
			})
			
			let timer = setInterval(()=>{
				if(this.guaCanvas && this.guaCanvas.show){
					this.isShow = true;
					clearInterval(timer);
				}
			}, 500)
		},
		close(type){
			if(type===1){
				this.isWin = false;
			}
			if(type===2){
				this.isThank = false;
			}
			this.showResult = false;
		}
  },
}
</script>
 
<style lang="scss" scoped>
.prizePage{
	width: 750rpx;
	// min-height: calc(100vh - 88rpx - var(--status-bar-height));
	height: calc(100vh - 88rpx - var(--status-bar-height));
	// padding-top: calc((100vh - 88rpx - var(--status-bar-height) - (750rpx * 1700 / 1080)) / 2);
	// filter: blur(5px);
	overflow: hidden;
}
.prizeBg{
	width: 750rpx;
	min-height: calc(100vh - 88rpx - var(--status-bar-height));
	background-repeat: no-repeat;
	// background-size: 100% 100%;
	background-size: cover;
	background-position: center;
	filter: blur(5px);
	position: fixed;
	top: 88rpx;
	z-index: -1;
}
.prizeContent{
	width: 750rpx;
	height: calc(750rpx * 1700 / 1080);
	background-repeat: no-repeat;
	background-size: contain;
	
	margin-top: calc((100vh - 88rpx - var(--status-bar-height) - (750rpx * 1700 / 1080)) / 2);
	// 背景色和背景图相差的高度
	
	// background-size: auto 100%;
	// background-color: #fff;
	// height: 590px;
	// background-position: center top;
	// position: fixed;
	// top: calc((100vh - 88rpx - var(--status-bar-height) - (750rpx * 1700 / 1080)) / 2 + 88rpx);
	// z-index: 4;
}
.bg{
	position: fixed;
	z-index: 2;
	width: 750rpx;
	min-height: calc(100vh - 88rpx - var(--status-bar-height));
	background-repeat: no-repeat;
	background-size: contain;
	background-color: #fff;
}
.result_img {
	width: 100%;
	height: 100%;
	position: absolute;
	top: 0;
	left: 0;
	z-index: -1;
	background-color: #dddddd;
	display: flex;
	flex-direction: column;
	justify-content: space-around;
	
	&.hide{
		opacity: 0;
	}

	.img_row{
		display: flex;
		flex-wrap: wrap;
		justify-content: space-around;
	}
	&.zIndex1{
		z-index: 1;
		opacity: 1;
	}
	.data_item{
		width: 22%;
		text-align: center;
		font-size: 14px;
		font-weight: bold;
		position: relative;
		
		image{
			width: 60rpx;
			height: 60rpx;
			// background-color: #000;
		}
		
		.circle{
			position: absolute;
			top: 0%;
			left: 6%;
			z-index: 999;
			display: none;
			width: 88%;
			height: 80%;
			border-radius: 50%;
			background-color: transparent;
			box-sizing: border-box;
			// border: 3px solid #f00 !important;// border-color在部分手机浏览器无效
			background-image: url("../../static/home/circle.png");
			background-size: 100% 100%;
			
			&.show{
				display: block;
			}
		}
	}
}
.scratch {
	position: relative;
	padding-top: 500rpx;
	// padding-top: 280px;
	width: 660rpx;
  height: 600rpx;
	margin: 0px auto;
  .box {
    width: 100%;
    height: 100%;
    background: transparent;
    // border-radius: 20rpx;
    position: relative;
    overflow: hidden;
    
    .canvas-box {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
      // border-radius: 20rpx;
      overflow: hidden;
			z-index: 2;
    }
    .tip {
      position: absolute;
      left: 0;
      right: 0;
      width: 100%;
      height: 100%;
      z-index: 999;
      text-align: center;
      .text {
        font-size: 30rpx;
        font-weight: bold;
        color: #444;
        margin-top: 69rpx;
        .text-tip {
          display: inline-block;
          vertical-align: middle;
        }
        .light {
          // color: $red;
          display: inline-block;
          vertical-align: middle;
          margin: 0 6rpx !important;
        }
      }
      .btn {
        width: 400rpx;
        height: 88rpx;
        border: none;
        border-radius: 20rpx;
        margin: 245rpx auto 0;
        position: relative;
        background: #E33838;
        .text {
          display: inline;
          position: absolute;
          text-align: center;
          color: #fff;
          font-size: 30rpx;
          font-weight: bold;
          margin: 0;
          left: 50%;
          top: 50%;
          transform: translate(-50%, -50%);
					z-index: 1;
        }
      }
    }
    .award-box {
      width: 100%;
      height: 100%;
      border-radius: 20rpx;
      text-align: center;
      line-height: 270rpx;
      background: #FFF1B9;
      .text {
        font-size: 40rpx;
        font-weight: bold;
        color: #444;
      }
    }
  }
}
.mask{
	width: 100vw;
	height: 100vh;
	background-color: rgba(0, 0, 0, 0.2);
	position: fixed;
	top: 0px;
	left: 0px;
	z-index: 2;
}
.winPrize{
	width: 375px;
	height: 575px;
	background-image: url("../../static/home/prize.png");
	background-size: contain;
	position: fixed;
	top: 50%;
	left: 50%;
	z-index: 30;
	transform: translate(-50%, -50%);
	text-align: center;
	line-height: 550px;
	color: #FFF1B9;
	font-weight: bold;
	font-size: 60px;
	animation: showPrize 1s ease-in-out;
	
	.again{
		width: 300rpx;height: 200rpx;
		position: absolute;left: 225rpx;top: 630rpx;
		z-index: 31;
		// background: rgba(0, 0, 0, 0.5);
	}
	.close{
		width: 50px;
		height: 50px;
		background-image: url("../../static/home/close.png");
		background-size: contain;
		position: fixed;
		left: 50%;
		transform: translate(-50%, -50%);
		z-index: 32;
	}
}
.noPrize_box{
	width: 100vw;
	height: 100vh;
	position: fixed;
	top: 0px;
	left: 0px;
	text-align: center;
	z-index: 30;
	
	.noPrize{
		width: 550rpx;
		height: 550rpx;
		background-image: url("../../static/home/noPrize.png");
		background-size: contain;
		background-repeat: no-repeat;
		position: absolute;
		top: 400rpx;
		left: 100rpx;
		z-index: 31;
		text-align: center;
		font-size: 14px;
		animation: showNoPrize 1s ease-in-out;
	}
	
	.again{
		width: 240rpx;
		height: 66rpx;
		line-height: 66rpx;
		border-radius: 20px;
		position: absolute;
		left: 255rpx;
		top: 650rpx;
		color: #F05859;
		background-image: url("../../static/home/btn_11.png");
		background-size: 100% 100%;
		z-index: 32;
		animation: showNoPrize 1s ease-in-out;
	}
	.again_mask{
		width: 340rpx;
		height: 150rpx;
		position: absolute;
		left: 205rpx;
		top: 610rpx;
		z-index: 33;
		// background: rgba(0, 0, 0, 0.5);
	}
	.close{
		width: 100rpx;
		height: 200rpx;
		background-image: url("../../static/home/close.png");
		background-size: contain;
		position: absolute;
		left: 325rpx;
		top: 770rpx;
		z-index: 32;
		animation: showNoPrize 1s ease-in-out;
		// background-color: rgba(255, 0, 0, 0.5);
		background-repeat: no-repeat;
		background-position: bottom;
	}
}
@keyframes showPrize {
	0%{
		transform: translate(-50%, -50%) scale(0.4);
	}
	100%{
		transform: translate(-50%, -50%) scale(1);
	}
}
@keyframes showNoPrize {
	0%{
		transform: scale(0.4);
	}
	100%{
		transform: scale(1);
	}
}
</style>