var app = getApp()
Page({
  data: {
    awardsList: {},
    animationData: {},
    btnDisabled: '',
    remainingCount:0,
    btnname: "getLottery",
    token:'',
    Lotterylist:[],
    high:true,
    token:'',
    secretKey:'',
    uid:0,
    groupId:0,
    lotteryId:0,
    lotteryRules:'',
  },
  getLottery: function () {
    let token = this.data.token;
    let secretKey = this.data.secretKey;
    let uid = this.data.uid;
    let groupId = this.data.groupId;
    let lotteryId = this.data.lotteryId;
    app.$apiJava('api/sell/lottery/getLotteryAward', { lotteryId: lotteryId,}, groupId, uid, token, secretKey).then(res => {
      console.log(res)

    var that = this;
    
    if (that.data.remainingCount >0 ){
      that.setData({ btnname:''})
      var len = app.awardsConfig.awards.length;
      // var awardIndex = Math.random() * len >>> 0;
      var awardIndex = 1;
      let Lotterylist = that.data.Lotterylist;
      for (var i = 0; i < Lotterylist.length; i++){
        if (res.awardName == Lotterylist[i].awardName){
            awardIndex = i;
        }
      }
      
      // 获取奖品配置

      var awardsConfig = app.awardsConfig,
        runNum = 8
    
      console.log(awardIndex)

      // 旋转抽奖

      app.runDegs = app.runDegs || 0
      console.log('deg', app.runDegs)
      app.runDegs = app.runDegs + (360 - app.runDegs % 360) + (360 * runNum - awardIndex * (360 / len ))

      console.log('deg', app.runDegs)

      var animationRun = wx.createAnimation({
        duration: 4000,
        timingFunction: 'ease'
      })
      that.animationRun = animationRun
      animationRun.rotate(app.runDegs).step()
      that.setData({
        animationData: animationRun.export(),
        // btnDisabled: 'disabled'
      })
      //减一次次数
      this.setData({ remainingCount: that.data.remainingCount-1})
      // 记录奖品
      var winAwards = wx.getStorageSync('winAwards') || { data: [] }
      winAwards.data.push(awardsConfig.awards[awardIndex].awardName + '1个')
      wx.setStorageSync('winAwards', winAwards)
      // 中奖提示
      setTimeout(function () {
        wx.showModal({
          title: '恭喜',
          content: '获得' + (awardsConfig.awards[awardIndex].awardName),
          showCancel: false,
          success(res){
            if (res.confirm){
              that.setData({ btnname: 'getLottery' })
            }
          }
        })
        if (awardsConfig.chance) {
          that.setData({
            btnDisabled: ''
          })
        }
        
      }, 4000);
      

    }else{
      wx.showToast({
        title: '抽奖次数已用完',
        icon: 'none',
        duration: 1000
      })
    }
    }).catch(err => {

    })
  
  },

  gotoList:function(){
    let token = this.data.token;
    let secretKey = this.data.secretKey;
    let uid = this.data.uid;
    let groupId = this.data.groupId;
    let lotteryId = this.data.lotteryId;
    wx.navigateTo({
      url: '../prizelist/prizelist?token=' + token + '&secretKey=' + secretKey + '&uid=' + uid + '&groupId=' + groupId + '&lotteryId=' + lotteryId,

    })

  },
  onLoad: function (option) {
    //获取屏幕高度来设置位移多少
    let heightH = wx.getSystemInfoSync().windowHeight;
    console.log(heightH)
    if (heightH> 700){
        this.setData({
          high:false,
        })
    }
    let token = option.token;
    let secretKey = option.secretKey;
    let uid = option.uid;
    let groupId = option.groupId
    let lotteryId = 0;
    let that = this;
    that.setData({
       token: token,
      secretKey: secretKey,
      uid: uid,
      groupId: groupId,
       })
       var list = [];
    // //获取抽奖列表
    app.$apiJava('api/sell/lottery/getB2bTaskLottery', {}, groupId, uid, token, secretKey).then(res => {
      let a = res.lotteryRules ;
      let b = a.replace(/<p>/g, '\n');
      let c = b.replace(/[</p>]/g, '')
      
      this.setData({
        lotteryId: res.id,
        Lotterylist: res.lotteryAwardItem,
        lotteryRules: c,
      })
      // //获取抽奖次数
     
      app.$apiJava('api/sell/lottery/getLotteryRemainingCount', { lotteryId: res.id }, groupId, uid, token, secretKey).then(res => {
        this.setData({
          remainingCount: res.remainingCount,
        })

      }).catch(err => {

      })
      list = res.lotteryAwardItem
      app.awardsConfig = {
        chance: true,
        awards: list,

      }

      // wx.setStorageSync('awardsConfig', JSON.stringify(awardsConfig))


      // 绘制转盘  


      var awardsConfig = app.awardsConfig.awards,
        len = awardsConfig.length,
        
        rotateDeg = 360 / len / 2 + 90,
        html = [],
        turnNum = 1 / len  // 文字旋转 turn 值
      that.setData({
        btnDisabled: app.awardsConfig.chance ? '' : 'disabled'
      })
      var ctx = wx.createContext();
      for (var i = 0; i < len; i++) {
        // 保存当前状态
        ctx.save();
        // 开始一条新路径
        ctx.beginPath();
        // 位移到圆心，下面需要围绕圆心旋转
        ctx.translate(150, 150);
        // 从(0, 0)坐标开始定义一条新的子路径
        ctx.moveTo(0, 0);
        // 旋转弧度,需将角度转换为弧度,使用 degrees * Math.PI/180 公式进行计算。
        ctx.rotate((360 / len * i - rotateDeg) * Math.PI / 180);
        // 绘制圆弧
        ctx.arc(0, 0, 150, 0, 2 * Math.PI / len, false);
      
        // 颜色间隔
        if (i % 2 == 0) {
          ctx.setFillStyle('rgba(255,184,32,.1)');
        } else {
          ctx.setFillStyle('rgba(255,203,63,.1)');
        }

        // 填充扇形
        ctx.fill();
        // 绘制边框
        ctx.setLineWidth(0.5);
        ctx.setStrokeStyle('rgba(228,55,14,.1)');
        ctx.stroke();

        // 恢复前一个状态
        ctx.restore();

        // 奖项列表
        html.push({ turn: i * turnNum + 'turn', lineTurn: i * turnNum + turnNum / 2 + 'turn', award: awardsConfig[i].awardName });
      }
      that.setData({
        awardsList: html
      });

    }).catch(err => {
     
    })
   

   

    

  }

})
