<template>
	<view class="prizePage" :style="{'background-image':`url('${ticketInfo.bg_image}')`}">
		<view class="ticketInfoPrice">面值{{ticketInfo.price}}元</view>
		<view class="ticketInfoPrice2">面值{{ticketInfo.price}}元</view>
		<view class="scratch">
		  <view class="box">
		    <!-- 刮奖结果图片 -->
		    <!-- <image src="../../static/home/logo.png" class="img" :class="toDraw?'zIndex1':''" v-if="showResult||1"></image> -->
				<view :class="toDraw?'zIndex1':''" style="" class="img">
					<view v-for="(item,index) in dataList" :key="index" class="img_row">
						<view v-for="(lineItem, lineIndex) in item" :key="lineIndex" class="data_item">
							<image :src="lineItem.image" mode=""></image>
							<view class="">￥{{lineItem.amount}}</view>
						</view>
					</view>
				</view>
		    <!-- 刮奖canvas容器 -->
		    <canvas
		      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 class="mask" v-if="isWin || isThank">
				
			</view>
			<view class="winPrize" v-if="isWin">
				{{awards_amount}}元
				<view class="close" @click="isWin = false"></view>
			</view>
			<view class="noPrize" v-if="isThank">
				<view class="thank">谢谢惠顾</view>
				<view class="thank2">再接再厉</view>
				<view class="again" @click="again">再刮一次</view>
				<view class="close" @click="isThank = false"></view>
			</view>
		</view>
		<view class="ticketInfoCon">{{ticketInfo.content}}</view>
		<view class="ticketInfoCon2">{{ticketInfo.content}}</view>
	</view>
  
</template>
 
<script>
	import API from '@/common/js/api.js'
import Scratch from './scratch.js'
export default {
  data () {
    return {
      id: '',
			ticketInfo: {
				// bgImg: '../../static/home/prize-bg.png',
				bgImg: '',
				price: '20',
				content: `刮开覆盖膜，如果出现背书标志“1倍”、“2倍”、“5倍”、“10倍”、“20倍”、
					“50倍”、“100倍”，既中得该标志下方所示的金额乘以该倍数；
					如果在“全中”区出现标志“赢”，即中图刮开区内所示的20个金额之和。`
			},
			awards_amount: '20',
			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, // 绘制刮奖范围宽
      scratchHeight: 280, // 绘制刮奖范围高
      scratchSize: 10, // 触手大小
      scratchScale: 0.6, // 需刮开百分比
      prize_img: '', // 中奖图片
      isScratching: false, // 是否正在刮奖,
			isThank: false,
			// show: false,
			guaCanvas: null,
			isShow: false,
			c_r_id: '',
    }
  },
	updated() {
		// console.log(this.show);
	},
	watch: {
		isShow(n){
			this.showResult = true;
			this.doPrizeScratchTicketFun();
		}
	},
	onLoad(e) {
		uni.setNavigationBarTitle({
			title: e.type,
			// +'元专区'
		})
		this.id = e.id;
		
		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)
	},
  methods: {
    initCanvas () {
      // 刮奖初始化信息必须在onReady后，不然h5不支持（小程序可在onLoad执行）
      this.guaCanvas = new Scratch(this, {
        canvasId: 'guagua',
        width: this.scratchWidth,
        height: this.scratchHeight,
        size: this.scratchSize,
        scale: this.scratchScale
      })
			
    },
		// 开始刮奖获取信息
		beginLottery(){
			API.beginLottery({zone_goods_id: this.id}, res=>{
				this.dataList = res.data.list;
				this.c_r_id = res.data.c_r_id;
				this.toDraw = true;
				this.initCanvas();
				// console.log(this.dataList, 'dataList----');
			})
		},
    // 请求刮奖结果
    doPrizeScratchTicketFun () {
      // this.isWin = true;
			// this.isThank = true;
      API.endLottery({c_r_id: this.c_r_id}, res=>{
				this.awards_amount = res.data.awards_amount;
				this.isWin = true;
			}, fail=>{
				this.isWin = false
				this.isThank = true;
				// this.isScratching = false
			})
    },
    // 点击按钮开始刮奖
    scratchStart () {
      
      this.beginLottery()
    },
		// 再刮一次
		again(){
			this.isThank = false;
		}
  },
}
</script>
 
<style lang="scss">
	.prizePage{
		width: 750rpx;
		min-height: 100vh;
		background-repeat: no-repeat;
		background-size: contain;
		background-color: #8c8c8c;
		
		.ticketInfoPrice{
			position: fixed;top: 60px;right: 20px;z-index: 3;
			color: #fff;
			font-size: 30px;
		}
		.ticketInfoPrice2{
			position: fixed;top: 64px;right: 16px;z-index: 2;
			color: #000;
			font-size: 30px;
			font-weight: bold;
		}
		.ticketInfoCon{
			position: fixed;top: 150px;left:10vw;
			width: 80vw;text-align: justify;
			color: #fff;
			z-index: 3;
		}
		.ticketInfoCon2{
			position: fixed;
			top: 152px;
			left:calc(10vw + 2px);
			width: 80vw;text-align: justify;
			font-weight: bold;
			color: #000;
			z-index: 2;
		}
	}
.scratch {
	position: relative;
	padding-top: 380px;
	// padding-top: 280px;
	width: 330px;
  height: 280px;
	margin: 0px auto;
  .box {
    width: 100%;
    height: 100%;
    background: transparent;
    // border-radius: 20rpx;
    position: relative;
    overflow: hidden;
    .img {
      width: 100%;
      height: 100%;
      position: absolute;
      top: 0;
      left: 0;
			z-index: -1;
			background-color: #DCDEDD;
			display: flex;
			flex-direction: column;
			justify-content: space-around;
			
			.img_row{
				display: flex;
				flex-wrap: wrap;
				align-content: space-around;
			}
			&.zIndex1{
				z-index: 1;
			}
			.data_item{
				width: 25%;
				text-align: center;
				font-size: 14px;
				font-weight: bold;
				
				image{
					width: 80rpx;
					height: 60rpx;
					// background-color: #000;
				}
			}
    }
    .canvas-box {
      position: absolute;
      top: 0;
      left: 0;
      width: 100.5%;
      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: 100%;
	height: 100%;
	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: 3;
	transform: translate(-50%, -50%);
	text-align: center;
	line-height: 550px;
	color: #FFF1B9;
	font-weight: bold;
	font-size: 60px;
	animation: showPrize 1s ease-in-out;
	
	.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: 4;
	}
}
.noPrize{
	width: 550rpx;
	height: 370rpx;
	background-image: url("../../static/home/noPrize.png");
	background-size: contain;
	position: fixed;
	top: 50%;
	left: 50%;
	z-index: 3;
	transform: translate(-50%, -50%);
	animation: showPrize 1s ease-in-out;
	text-align: center;
	font-size: 14px;
	
	.thank{
		width: 100%;
		color: #fff;
		position: absolute;
		top: 20rpx;
	}
	.thank2{
		line-height: 150px;
		color: #DB4542;
		font-weight: bold;
		font-size: 30px;
	}
	.again{
		width: 240rpx;
		height: 66rpx;
		line-height: 66rpx;
		border-radius: 20px;
		position: absolute;
		left: 155rpx;
		top: 256rpx;
		color: #F05859;
		background-color: #FFE1AE;
	}
	.close{
		width: 50px;
		height: 50px;
		background-image: url("../../static/home/close.png");
		background-size: contain;
		position: absolute;
		left: 50%;
		top: 230px;
		transform: translate(-50%, -50%);
		z-index: 4;
	}
}
@keyframes showPrize {
	0%{
		transform: translate(-50%, -50%) scale(0.4);
	}
	100%{
		transform: translate(-50%, -50%) scale(1);
	}
}
</style>