<template>
	<view class="prizePage" :class="{'type8':code==='digit_number_icon', 'type7':(code==='digit_icon' && direction==4), 'type6':(code==='digit_icon' && direction==2)}">
		<view class="prizeBg" :style="{'background-image':`url('${ticketInfo.bg_image}')`}"></view>
		<view class="prizeContent" :style="{'background-image':`url('${ticketInfo.bg_image}')`}"
			 :class="{'type8':code==='digit_number_icon', 'type7':(code==='digit_icon' && direction==4), 'type6':(code==='digit_icon' && direction==2)}" >
			<!-- <view class="bg" :style="{'background-image':`url('${ticketInfo.bg_image}')`}" v-if="gai"></view> -->
			
			<!-- 上下刮板分体 -->
			<view class="scratchTop" v-if="code && code==='digit_number_icon' && 0">
				<!-- 刮奖结果图片 -->
				<view class="result_img" :class="showBackboard?'zIndex1':'hide'" style="flex-direction: row;justify-content: space-between;background-color: transparent;">
					<view style="display: flex;align-items: center;justify-content: space-around;width:284.72rpx;background-color: #ddd;border-radius: 10px;">
						<view v-for="(prizeItem1,prizeIndex1) in prizesIconList[0]" :key="prizeIndex1" class="data_item">
							<image :src="prizeItem1" mode="aspectFill"></image>
						</view>
					</view>
					<view style="display: flex;align-items: center;justify-content: space-around;width:349.30rpx;background-color: #ddd;border-radius: 10px;">
						<view v-for="(prizeItem2,prizeIndex2) in prizesIconList[1]" :key="prizeIndex2" class="data_item">
							<image :src="prizeItem2" mode="aspectFill"></image>
						</view>
					</view>
				</view>
				<!-- 刮奖canvas容器 -->
				<view class="" style="width: 100%;height: 100%;">
					<canvas v-if="!isWin && !isThank" ref="leftCa"
					  class="canvas-left"
					  canvas-id="guaguaLeft"
					  :disable-scroll="true"
			      @touchstart="$refs.leftCa.touchStart"
			      @touchmove="$refs.leftCa.touchMove"
			      @touchend="$refs.leftCa.touchEnd"
					></canvas>
					<canvas v-if="!isWin && !isThank" ref="rightCa"
					  class="canvas-right"
					  canvas-id="guaguaRight"
					  :disable-scroll="true"
			      @touchstart="$refs.rightCa.touchStart"
			      @touchmove="$refs.rightCa.touchMove"
			      @touchend="$refs.rightCa.touchEnd"
					></canvas>
				</view>
			</view>
			
			<view class="scratch" :class="{'type8':code==='digit_number_icon', 'type7':(code==='digit_icon' && direction==4), 'type6':(code==='digit_icon' && direction==2)}">
			  <view class="box">
			    <!-- 刮奖结果图片 -->
					<view :class="showBackboard?'zIndex1':'hide'" style="" class="result_img">
						<!-- 上下刮板一体 -->
						<!-- <block v-if="code && code==='digit_number_icon'">
							<view style="font-size: 12px;text-align: center;margin-top: 8px;">中奖图标</view>
							<view style="border-bottom: 1px solid #333;width: 90%;margin: 5px auto;">
								<view v-for="(iconListItem,index) in prizesIconList" :key="index" class="img_row">
									<view v-for="(prizeItem2,prizeIndex2) in iconListItem" :key="prizeIndex2" class="data_item">
										<image :src="prizeItem2" mode="aspectFill"></image>
									</view>
								</view>
							</view>
						</block> -->
						<!--  -->
						<view v-if="code && code!=='digit_icon'" style="font-size: 12px;text-align: center;margin-top: 8px;">中奖号码</view>
						<view v-if="code && code!=='digit_icon'" class="img_row" style="border-bottom: 1px solid #333;width: 90%;margin: 5px auto;">
							<view v-for="(prizeItem1,prizeIndex1) in prizeList" :key="prizeIndex1" class="data_item">
								<image :src="prizeItem1" 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 style="line-height: 10px;">￥{{lineItem.amount}}</view>
								<view style="font-size: 12px;font-weight: normal;transform: scale(0.6);line-height: 8px;">{{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 class="backBtn">{{ '<返回' }}</view> -->
	</view>
  
</template>
 
<script>
import API from '@/common/js/api.js'
import Scratch from './scratch.js'
// import ScratchT from './scratchTools.js'
// import VConsole from './renderjs.js'
export default {
  data () {
    return {
      id: '',
			ticketInfo: {
				bg_image: '',
				price: '20',
			},
			awards_amount: 0,
			prizeList: ['','',''],
			prizesIconList: [],
			// dataList: [
			// 	[
			// 		{image: '',amount:'100.00', is_awards: false,pinyin: ''},
			// 	],
			// 	[],
			// 	[],
			// ],
			dataList: [],
      toDraw: false, // 是否开始刮奖,
      showResult: false, // 是否显示抽奖结果
      isWin: false, // 是否中奖
      scratchWidth: 330, // 绘制刮奖范围宽-375屏幕330
      scratchHeight: 300, // 绘制刮奖范围高-375屏幕300
			scratchTopWidthL: 0,
			scratchTopWidthR: 0,
			scratchTopHeight: 0,
			
      scratchSize: 10, // 触手大小
      scratchScale: 0.8, // 需刮开百分比
      prize_img: '', // 中奖图片
      isScratching: false, // 是否正在刮奖,
			isThank: false,
			guaCanvas: null,
			guaCanvasL: null,
			guaCanvasR: null,
			isShow: false,
			isShowLeft: false,
			isShowRight: false,
			c_r_id: '',
			gai: true,
			screenWidth: 375,
			showBackboard: false,// 页面初始，底下的结果层先不渲染
			code: '',//奖券类型
			direction: '',//号码行数
			transverse: '',//每行个数
			showCount: 0,//888刮开数量
    }
  },
	updated() {
		// console.log(this.show);
	},
	watch: {
		isShow(n, o){
			// console.log(n,o);
			if(n && n!=o){
				
				// if(this.code==='digit_number_icon'){
				// 	this.changeShowCount();
				// 	return;
				// }
				this.showResult = true;
				this.gai = false;
				this.doPrizeScratchTicketFun();
			}
		},
		isShowLeft(n, o){
			if(n && n!=o){
				this.changeShowCount();
			}
		},
		isShowRight(n, o){
			if(n && n!=o){
				this.changeShowCount();
			}
		},
	},
	created() {
		
	},
	onLoad(e) {
		uni.setNavigationBarTitle({
			title: e.type,
			// +'元专区'
		})
		this.id = e.id;
		this.initInfo();
	},
  methods: {
		initInfo(){
			let _this = this;
			API.request('/zone/beginInitInfo', {zone_goods_id: this.id}, res=>{
				this.ticketInfo = res.data.data;
				this.code = res.data.data.code;
				this.direction = res.data.data.direction;//号码行数
				this.transverse = res.data.data.transverse;//每行个数
				
				const result = uni.getSystemInfoSync();
				_this.screenWidth = result.screenWidth;
				_this.scratchWidth = 950/1080 * _this.screenWidth || 330;
				_this.scratchHeight = 866/1080 * _this.screenWidth || 300;
				
				if(this.code==='digit_number_icon'){//888
					// this.scratchHeight = 1087/1080 * this.screenWidth;// 上下刮板一体
					this.scratchHeight = 700/1080 * this.screenWidth;// 上下刮板分体
					this.scratchTopWidthL = 410/1080 * _this.screenWidth || 330;
					this.scratchTopWidthR = 503/1080 * _this.screenWidth || 330;
					this.scratchTopHeight = 128/1080 * this.screenWidth;
				}
				if(this.code==='digit_icon'&&this.direction==2){//666
					this.scratchHeight = 300/1080 * this.screenWidth;
				}
				if(this.code==='digit_icon'&&this.direction==4){//777
					this.scratchHeight = 696/1080 * this.screenWidth;
				}
				
				// console.log(this.scratchWidth, this.scratchHeight);
				
				let timer = setInterval(()=>{
					if(this.guaCanvas && this.guaCanvas.show){
						this.isShow = true;
						clearInterval(timer);
					}
				}, 500)
				if(_this.code==='digit_number_icon'){
					let timer1 = setInterval(()=>{
						if(this.guaCanvasL && this.guaCanvasL.show){
							this.isShowLeft = true;
							clearInterval(timer1);
						}
					}, 500)
					let timer2 = setInterval(()=>{
						if(this.guaCanvasR && this.guaCanvasR.show){
							this.isShowRight = true;
							clearInterval(timer2);
						}
					}, 500)
				}
			})
			
		},
    initCanvas () {
			let _this = this;
      // 刮奖初始化信息必须在onReady后，不然h5不支持（小程序可在onLoad执行）
      this.guaCanvas = new Scratch(this, {
        canvasId: 'guagua',
        width: this.scratchWidth,
        height: this.scratchHeight,
        size: (this.code==='digit_icon' && this.direction==2)?6:this.scratchSize,///666的笔触要小
        scale: (this.code==='digit_icon' && this.direction==2)?0.95:this.scratchScale,///666的刮开比例要高一点
				bgImg: this.ticketInfo.bg_image,
				screenWidth: this.screenWidth,
				codeType: this.code,
				direction: this.direction,//号码行数
				transverse: this.transverse,//每行个数
      }, function(){
				setTimeout(()=>{
					_this.showBackboard = true;
				}, 1000)
			})
			if(this.code==='digit_number_icon'){
				return;
				this.guaCanvasL = new Scratch(this.$refs.leftCa, {
				  canvasId: 'guaguaLeft',
				  width: this.scratchTopWidthL,
				  height: this.scratchTopHeight,
				  size: this.scratchSize,
				  scale: this.scratchScale,
					bgImg: this.ticketInfo.bg_image,
					screenWidth: this.screenWidth,
					codeType: this.code,
					direction: this.direction,//号码行数
					transverse: this.transverse,//每行个数
				})
				this.guaCanvasR = new Scratch(this.$refs.rightCa, {
				  canvasId: 'guaguaRight',
				  width: this.scratchTopWidthR,
				  height: this.scratchTopHeight,
				  size: this.scratchSize,
				  scale: this.scratchScale,
					bgImg: this.ticketInfo.bg_image,
					screenWidth: this.screenWidth,
					codeType: this.code,
					direction: this.direction,//号码行数
					transverse: this.transverse,//每行个数
				})
			}
			
    },
		// 开始刮奖获取信息
		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;
				this.prizesIconList = res.data.prizes_icon_data;
				// console.log(this.dataList, 'dataList----');
			})
		},
    // 请求刮奖结果
    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.isShowLeft = false;
			this.isShowRight = false;
			this.showBackboard = false;
			this.awards_amount = 0;
			this.showCount = 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)
			if(this.code==='digit_number_icon'){
				let timer1 = setInterval(()=>{
					if(this.guaCanvasL && this.guaCanvasL.show){
						this.isShowLeft = true;
						clearInterval(timer1);
					}
				}, 500)
				let timer2 = setInterval(()=>{
					if(this.guaCanvasR && this.guaCanvasR.show){
						this.isShowRight = true;
						clearInterval(timer2);
					}
				}, 500)
			}
		},
		close(type){
			if(type===1){
				this.isWin = false;
			}
			if(type===2){
				this.isThank = false;
			}
			this.showResult = false;
		},
		changeShowCount(){
			this.showCount++;
			if(this.showCount===3){
				this.showResult = true;
				this.gai = false;
				this.doPrizeScratchTicketFun();
			}
		}
  },
}
</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));
	overflow: hidden;
	
	&.type8{
		min-height: calc(100vh - 88rpx - var(--status-bar-height));
		height: auto;
	}
	&.type7{
		min-height: calc(100vh - 88rpx - var(--status-bar-height));
		height: auto;
	}
	&.type6{
		
	}
}
.prizeBg{
	width: 750rpx;
	min-height: calc(100vh - 88rpx - var(--status-bar-height));
	background-repeat: no-repeat;
	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);
	// 背景色和背景图相差的高度
	
	&.type8{
		height: calc(750rpx * 2163 / 1080);
		margin-top: calc((100vh - 88rpx - var(--status-bar-height) - (750rpx * 2163 / 1080)) / 2);
		position: relative;
		margin-top: 0px;
	}
	&.type7{
		height: calc(750rpx * 2163 / 1080);
		margin-top: calc((100vh - 88rpx - var(--status-bar-height) - (750rpx * 2163 / 1080)) / 2);
		margin-top: 0px;
	}
	&.type6{
		height: calc(750rpx * 1080 / 1080);
		margin-top: calc((100vh - 88rpx - var(--status-bar-height) - (750rpx * 1080 / 1080)) / 2);
	}
}
.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%;
		flex: 1;
		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: 100%;
			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;
			}
		}
	}
}
.scratchTop{
	position: absolute;
	top: 644rpx;
	left: 45rpx;
	width: 660rpx;
	height: 88.88rpx;
	// background-color: #fff;
	
	.canvas-left{
		position: absolute;
		top: 0;
		left: 0;
		width: 284.72rpx;
		height: 100%;
		overflow: hidden;
		z-index: 2;
		// background-color: #bbb;
	}
	.canvas-right{
		position: absolute;
		top: 0;
		right: 0;
		width: 349.30rpx;
		height: 100%;
		overflow: hidden;
		z-index: 2;
		// background-color: #ccc;
	}
}
.scratch {
	position: relative;
	padding-top: 500rpx;
	width: 660rpx;
  height: 600rpx;
	margin: 0px auto;
	
	&.type8{
		// padding-top: 569.4rpx;//readme.md文件中的顶部距离// 上下刮板一体
		// height: 754.8rpx;//readme.md文件中的刮奖区高度// 上下刮板一体
		padding-top: 838.2rpx;// 上下刮板分体
		height: 486.1rpx;// 上下刮板分体
		
		.box{
			.tip{
				.btn {
				  // margin: 470rpx auto 0;// 上下刮板一体
				  margin: 270rpx auto 0;// 上下刮板分体
				}
			}
		}
	}
	&.type7{
		padding-top: 831.2rpx;
		height: 483.32rpx;
		.box{
			.tip{
				.btn {
				  margin: 190rpx auto 0;
				}
			}
		}
	}
	&.type6{
		padding-top: 366.66rpx;
		height: 208.32rpx;
		
		.box{
			.tip{
				.btn {
				  margin: 72rpx auto 0;
				}
			}
		}
	}
  .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);
	}
}
.backBtn{
	font-size: 14px;
	padding: 10px 20px;
	color: #12488a;
	// background-color: #12488a;
	position: fixed;
	z-index: 999;
	right: 10px;
	bottom: 30px;
	border-radius: 100px;
	font-weight: bold;
	text-shadow: 1px 1px 3px #fff;
}
</style>