const util = require('../../../utils/util.js');
const app = getApp();
Page({
	data: {
		flag:true,
		isGet:false,//是否已经刮过奖
	  	startX: 0,
	  	startY: 0,
	  	startT:0,
	  	totleT:0,
	  	judgeX:false,
	  	judgeY:false,
	  	prize:0,
      orderid:'',
      coinNum:0
	},
	onLoad(options) {
    let that = this;
    // that.ctx = wx.createCanvasContext('myCanvas');
    // wx.getSystemInfo({
    //   success: function(res) {
    //     let screenWidth = res.screenWidth,pixelRatio = res.pixelRatio;
    //     let imgH = 366 / (750 / screenWidth);
    //     that.ctx.drawImage('/images/3.png', 0, 0, screenWidth, imgH);
    //     that.ctx.draw();
    //   }
    // })
    this.setData({coinNum:options.coin,isGet:options.coin&&options.coin>0?true:false})
    // this.userPrize(options.orderid);
    let pages = getCurrentPages(), prevPage = pages[pages.length - 2];
    if (prevPage.route == "pages/order/detail/index") {
      prevPage.data.query.otype = 0;
    }
  },
  jumpPage(e){
    let url=e.currentTarget.dataset.url;
    wx.navigateTo({
      url: url,
    })
  },
	userPrize(orderid){
		let that = this;
		util.ajax({
	        func:'userPrize/scrapePrize',
	        data:{"orderid":orderid}
	    },function(res){
	    	if(res.code == 0){
	    		that.setData({prize:res.data.prize,orderid:orderid,isGet:true})
	    	}else if(res.code == -1){
	    		that.setData({isGet:false});
	    	}else{
          util.showTips(res.reason);
	    	}
	    })
	},
	touchStart(e){
    var startX1 = e.changedTouches[0].x;
    var startY1 = e.changedTouches[0].y;
    this.ctx.save();
    this.ctx.beginPath();
    this.ctx.clearRect(startX1, startY1, 20, 20);
    this.ctx.restore();
    this.setData({startT:e.timeStamp,startX:startX1,startY:startY1});
	},
	touchMove(e){
    var startX1 = e.changedTouches[0].x;
    var startY1 = e.changedTouches[0].y;
    this.ctx.save();
    this.ctx.moveTo(this.startX, this.startY);
    this.ctx.clearRect(startX1, startY1,20,20);
    this.ctx.restore();
    this.startX = startX1;
    this.startY = startY1;
    wx.drawCanvas({
        canvasId: 'myCanvas',
        reserve: true,
        actions:this.ctx.getActions()
    })
    let startY = this.data.startY,startX = this.data.startX;
    if(Math.abs(startX - startX1) > 100){
      this.setData({judgeX:true})
    }
    if(Math.abs(startY - startY1) > 30){
      this.setData({judgeY:true})
    }
	},
	touchEnd(e){
		let totleT = this.data.totleT + e.timeStamp - this.data.startT,judgeX = this.data.judgeX,judgeY = this.data.judgeY;
		if((totleT > 1500 && judgeX && judgeY) || totleT > 3000 ){
			this.setData({flag:false})
			this.upData()
		}else{
			this.setData({totleT});
		}
	},
	upData(){
		let that = this;
		util.ajax({
      func:'userPrize/update',
      data:{"orderid":that.data.orderid},
      load:false
    },function(res){
      wx.reportAnalytics('click_scratch', {
        orderid:that.data.orderid
      });
    })
	}
})