//index.js
//获取应用实例
const app = getApp()

Page({
  onShow() {
    wx.setNavigationBarTitle({
      title: '购房资格测试'
    })
  },
  data: {
    UserInfo: app.globalData.UserInfo, // 用户信息
    List: [{
      Question: '示例问题1',
      AnswerList: [{
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }, {
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }, {
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }]
    }, {
      Question: '示例问题2',
      AnswerList: [{
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }, {
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }, {
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }]
    }, {
      Question: '示例问题3',
      AnswerList: [{
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }, {
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }, {
        Value: '示例答案选项',
        Id: 'xxx',
        IsCheck: false
      }]
    }],
    ActiveIndex: 0,
    CanSubmit: false,
    TapOff: true,
    SubmitOff: true
  },
  CheckMyPermission() { // 查看我的购房资格
    if (this.data.SubmitOff) {
      this.setData({
        SubmitOff: false
      })
    }
  },
  CheckItem(e) { // 选择答案
    if (this.data.TapOff) {
      this.setData({
        TapOff: false
      })
      let arr = this.data.List.slice(0)
      arr[this.data.ActiveIndex].AnswerList.map((item, index) => {
        if (index !== e.target.dataset.index) {
          item.IsCheck = false
        } else {
          item.IsCheck = true
          if (this.data.ActiveIndex === this.data.List.length - 1) {
            setTimeout(() => {
              let aIndex = this.data.ActiveIndex === this.data.List.length - 1 ? this.data.ActiveIndex : this.data.ActiveIndex + 1
              this.setData({
                CanSubmit: true
              })
            }, 1000)
          }
        }
      })
      this.setData({
        List: arr
      })
      setTimeout(() => {
        let aIndex = this.data.ActiveIndex === this.data.List.length - 1 ? this.data.ActiveIndex : this.data.ActiveIndex + 1
        this.setData({
          ActiveIndex: aIndex,
          TapOff: true
        })
      }, 1000)
    }
  },
  PrevQuestion() { // 上一题
    let aIndex = this.data.ActiveIndex === 0 ? this.data.ActiveIndex : this.data.ActiveIndex - 1
    this.setData({
      ActiveIndex: aIndex,
      TapOff: true,
      CanSubmit: false
    })
  }
})