// pages/zhi_xing_gong_neng/zhi_xing_gong_neng.js
const { throttle } = require('../../utils/util.js')
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    isZxgn:true,
    isAnswer:false,
    statusBar: app.globalData.statusBar,
    remainTime:0,
    mentalRotation:0,
    graphicReasoning:0,
    spaceImagination:0,
    executionFunctionality:0,
    visualSearch:0,
    workingMemory:0,
    times:0,
    isWay:0,
    level:0,
    isCorrect:false,
    isMistake:false,
    // ageGroup:16,
    smallList: ['一', '二', '三', '四','五'],
    bigList: ['六','七','八','九','十'],
    randomList: ['一','四','六','七','九'],
    zxgnList:[
      {
        score:0
      },
      {
        score: 0
      },
      {
        score: 0
      },
      {
        score: 0
      },
      {
        score: 0
      },
      {
        score: 0
      },
      {
        score: 0
      },
      {
        score: 0
      },
      {
        score: 0
      },
      {
        score: 0
      }
    ],
    exerciseList:[
      {
        idx: 1,
        title: '谁最大?',
        score:0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 0,
            status: ''
          },
          {
            text: 'B', 
            num: Math.floor(Math.random() * (5 - 10) + 10),
            isAnswer: 1,
            status: ''
          }
        ]
      }
    ],
    oneList:[
      {
        idx: 0,
        title: '谁最大?',
        score:0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 0,
            status: ''
          },
          {
            text: 'B', 
            num: Math.floor(Math.random() * (5 - 10) + 10),
            isAnswer: 1,
            status: ''
          }
        ]
      },
      {
        idx: 1,
        title: '谁最小?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 1,
            status: ''
          },
          {
            text: 'B',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 0,
            status: ''
          }
        ]
      },
      {
        idx: 2,
        title: '谁最小?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 1,
            status: ''
          },
          {
            text: 'B',
            num:'十',
            isAnswer: 0,
            status: ''
          }
        ]
      },
      {
        idx:3,
        title: '谁最大?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (3 - 7) + 7),
            isAnswer: 0,
            status: ''
          },
          {
            text: 'B',
            num:Math.floor(Math.random() * (9 - 10) + 10),
            isAnswer: 1,
            status: ''
          }
        ]
      },
      {
        idx: 4,
        title: '谁最小?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 0,
            status: ''
          },
          {
            text: 'B',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 1,
            status: ''
          }
        ]
      },
      {
        idx:5,
        title: '谁最大?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (6 - 10) + 10),
            isAnswer: 1,
            status: ''
          },
          {
            text: 'B',
            num:Math.floor(Math.random() * (2 - 5) + 5),
            isAnswer: 0,
            status: ''
          }
        ]
      },
      {
        idx: 6,
        title: '谁最小?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (4 - 8) + 8),
            isAnswer: 0,
            status: ''
          },
          {
            text: 'B',
            num:Math.floor(Math.random() * (9 - 10) + 10),
            isAnswer: 1,
            status: ''
          }
        ]
      },
      {
        idx: 7,
        title: '谁最大?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (0 - 3) + 3),
            isAnswer: 1,
            status: ''
          },
          {
            text: 'B',
            num:'九',
            isAnswer: 0,
            status: ''
          }
        ]
      },
      {
        idx: 8,
        title: '谁最大?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (4 - 6) + 6),
            isAnswer: 1,
            status: ''
          },
          {
            text: 'B',
            num:Math.floor(Math.random() * (0 - 3) + 3),
            isAnswer: 0,
            status: ''
          }
        ]
      },
      {
        idx: 9,
        title: '谁最小?',
        score: 0,
        answerList: [
          {
            text: 'A',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 0,
            status: ''
          },
          {
            text: 'B',
            num:Math.floor(Math.random() * (0 - 5) + 5),
            isAnswer: 1,
            status: ''
          }
        ]
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(options)
    // console.log(options)
    this.setData({
      isOne: Math.round(Math.random()),
      small2: Math.floor(Math.random() * (0 - 5) + 5),
      small: Math.floor(Math.random() * (1 - 5) + 5),
      big: Math.floor(Math.random() * (6 - 10) + 10),
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
 /**
   * 返回上一级页面
   */
  blockCtl: function () {
    this.redirectCtl({ url: 'me', method: {} }, true)
    this.endSetInter();
  },
  /**
   * 开始答题
   */
  actionAnswer: function () {
    const { isZxgn } = this.data;
    this.setData({ isZxgn: false })
  },
  /**
 * 底部导航跳转
 */
  redirectCtl: app.redirectCtl,
   /**
   * 练习关选择答案
   */
  exerciseSelectItems:throttle(function(e){
    const {lindex, mindex} = e.currentTarget.dataset;
    const { exerciseList, times,level} = this.data;
    var that = this;
    var temp = {};
    for (var i in exerciseList[lindex].answerList) {
      temp[`exerciseList[${lindex}].answerList[${i}].status`] = '';
    }
    if (exerciseList[lindex].answerList[mindex].isAnswer == 1) {
      that.setData({ isYes: true })
    } else {
      that.setData({ isYes: false })
    }
    temp[`exerciseList[${lindex}].answerList[${mindex}].status`] = exerciseList[lindex].answerList[mindex].status == '' ? 'active' : ''
    this.setData(temp);
    this.setData({ isAnswer: true})
    setTimeout(
      function () {
          that.setData({ level:1,isWhite:true,isAnswer:false,times: 0})
          that.hiddenShow()
      }, 500)
  },1000),
  
   /**
   * 选择答案
   */
  selectItems:throttle(function(e){
    const {lindex, mindex} = e.currentTarget.dataset;
    const { oneList, times,level} = this.data;
    // console.log(lindex, mindex)
    var that = this;
    var temp = {};
    for (var i in oneList[lindex].answerList) {
      temp[`oneList[${lindex}].answerList[${i}].status`] = '';
    }
    temp[`oneList[${lindex}].answerList[${mindex}].status`]='active';
    this.setData(temp);
    if (oneList[lindex].answerList[mindex].isAnswer == 1) {
      that.setData({ isYes: true,isAnswer: true })
      that.integraeRule(lindex, 1)
      that.endSetInter();
    } else {
      that.setData({ isYes: false,isAnswer: true })
        that.integraeRule(lindex, 0)
      that.endSetInter();
    }
    setTimeout(
      function () {
        if(lindex+1==10){
          that.endSetInter();
          that.tenEndTimes()
        }else{
          that.setData({ level:1,isWhite:true,isOneImgShow:false,isWay: lindex +1,isAnswer:false,times: 0})
          that.hiddenShow()
        }
      }, 300)
  },800),
  /**
  * 过渡页显示
  */
  hiddenShow:function(){
  const {isWhite} = this.data;
  var that = this;
  setTimeout(function () {
    that.setData({ isWhite:false})
    that.startSetInter()
  }, 500)
  },
  /**
  * 积分规则处理
  */
  integraeRule: function (bindex,tindex) {
    const { zxgnList, times, isWay,ageGroup} = this.data;
    var that = this;
    var temp = {};
    // console.log(ageGroup,bindex)
    that.saveCase(tindex)
    if (tindex==1){
      if (0<ageGroup&&ageGroup <= 8) {
        if(0<times&&times<1.4){
          temp[`zxgnList[${bindex}].score`] = 10;
        }else if(1.4<=times&&times<=1.7){
          temp[`zxgnList[${bindex}].score`] = 8;
        }else if(times>1.7){
          temp[`zxgnList[${bindex}].score`] = 5;
        }
      }else if (9<=ageGroup&&ageGroup <= 12) {
        if(0<times&&times<1.2){
          temp[`zxgnList[${bindex}].score`] = 10;
        }else if(1.2<=times&&times<=1.5){
          temp[`zxgnList[${bindex}].score`] = 8;
        }else if(times>1.5){
          temp[`zxgnList[${bindex}].score`] = 5;
        }
      }else if (13<=ageGroup&&ageGroup <= 15) {
        if(0<times&&times<1){
          temp[`zxgnList[${bindex}].score`] = 10;
        }else if(1<=times&&times<=1.2){
          temp[`zxgnList[${bindex}].score`] = 8;
        }else if(times>1.2){
          temp[`zxgnList[${bindex}].score`] = 5;
        }
      }else if (16<=ageGroup&&ageGroup <= 35) {
        if(0<times&&times<0.8){
          temp[`zxgnList[${bindex}].score`] = 10;
        }else if(0.8<=times&&times<=1.1){
          temp[`zxgnList[${bindex}].score`] = 8;
        }else if(times>1.1){
          temp[`zxgnList[${bindex}].score`] = 5;
        }
      }else if (36<=ageGroup&&ageGroup <= 49) {
        if(0<times&&times<1){
          temp[`zxgnList[${bindex}].score`] = 10;
        }else if(1<=times&&times<=1.2){
          temp[`zxgnList[${bindex}].score`] = 8;
        }else if(times>1.2){
          temp[`zxgnList[${bindex}].score`] = 5;
        }
      }else if (50<=ageGroup&&ageGroup <= 60) {
        if(0<times&&times<1.2){
          temp[`zxgnList[${bindex}].score`] = 10;
        }else if(1.2<=times&&times<=1.5){
          temp[`zxgnList[${bindex}].score`] = 8;
        }else if(times>1.5){
          temp[`zxgnList[${bindex}].score`] = 5;
        }
      }else if (ageGroup>60) {
        if(0<times&&times<1.4){
          temp[`zxgnList[${bindex}].score`] = 10;
        }else if(1.4<=times&&times<=1.7){
          temp[`zxgnList[${bindex}].score`] = 8;
        }else if(times>1.7){
          temp[`zxgnList[${bindex}].score`] = 5;
        }
      }
      
    }else{
      temp[`zxgnList[${bindex}].score`] = 0;
    }
    // console.log(temp)
    that.setData(temp);
    if (isWay == 10){
      clearTimeout(that.data.timer)
      that.tenEndTimes();
    }
  },
  
  /**
   * 结束计时
   */
  endSetInter: function () {
    var that = this;
    clearTimeout(that.data.setInter)
  },
  /**
   * 开始计时
   */
  startSetInter: function () {
    let time = 100;
    var that = this;
    let { times, isWay } = this.data;
    // console.log(isWay)
    var setTime = this.data.times + 0.1
    that.setData({ times: setTime })
    // console.log(setTime)
    that.data.setInter = setTimeout(this.startSetInter, time);

    if (setTime > 2) {
      if (isWay == 10) {
        that.endSetInter();
        that.tenEndTimes()
      } else {
        that.endSetInter();
        that.setData({ isWay: isWay + 1, times: 0,isWhite:true })
        that.hiddenShow()
      }
    }
  },
 
  /**
   * 页面跳转定时器
   */
  tenEndTimes: function () {
    var that = this;
    const { graphicReasoning, level, ageGroup,spaceImagination,executionFunctionality,visualSearch,workingMemory,mentalRotation,zxgnList,gameRespondentId} = that.data;
    var allScore=0
    for (var i in zxgnList){
      allScore+=zxgnList[i].score
    }
    console.log(zxgnList,allScore)
    setTimeout(
      function () {
          that.redirectCtl({ url: 'result', method: { graphicReasoning, level:1, ageGroup,spaceImagination,executionFunctionality:(Number(executionFunctionality)+allScore),visualSearch,workingMemory,mentalRotation,isRefer:1,gameRespondentId} }, true)
        
      }, 100)
  },
  /**
   * 答题情况保存
   */
  saveCase:function(str){
    const postData = { fn: this.getPersonInfo, param: { ...arguments } }
    const {isWay,times,gameRespondentId}=this.data
    app.post('/api/game/v2/answerInfoSave', {gameRespondentId,moduleCode:6,questionCode:isWay+1,usedTime:Math.round(times*1000),answerResult:str+1}, 0, postData).then(res => {
    })
  },
})