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

  /**
   * 页面的初始数据
   */
  data: {
    statusBar: app.globalData.statusBar,
    isSex:0,
    ageList:[
      {
        ageNum:'3岁~5岁',
        status:''
      },
      {
        ageNum: '5岁~10岁',
        status: ''
      },
      {
        ageNum: '10岁~18岁',
        status: ''
      },
      {
        ageNum: '18岁以上',
        status: ''
      },
    ],
    sexList:[
      {
        sexNum: '男',
        img:'../../image/man.png',
        status: ''
      },
      {
        sexNum: '女',
        img: '../../image/women.png',
        status: ''
      },
    ]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData(options)
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    if (app.sharePageDefaultCtl) {
      return app.sharePageDefaultCtl()
    }
  },
  /**
   * 底部导航跳转
   */
  redirectCtl: app.redirectCtl,

  /**
   * 路由跳转
   */
  navigateCtl: app.navigateCtl,
  /**
    * 返回上一级页面
    */
   blockCtl: function () {
    this.redirectCtl({ url: 'me', method: {} }, true)
  },
  /**
   * 选择年龄
   */
  selectSex:function(){
    const {isSex}=this.data
    this.setData({ isSex:!isSex})
  },
  /**
   * input
   */
  inputCtl: function (e) {
    const mobileExp = /^1[0-9]{10}$/;
    const { temp = {}, type } = e.currentTarget.dataset
    const { value } = e.detail
    temp[type] = value;
    const { phone, name,age,school} = temp;
    if (type == 'phone') {
        this.setData({ phone })
        } else if (type == 'name') {
          return this.setData({ name })
        } else if (type == 'age') {
          return this.setData({ age })
        } else if (type == 'school') {
          return this.setData({school})
        } 
    
  },
  /**
   * 选择性别
   */
  setSex: function (e) {
    const { sidx, sex } = e.currentTarget.dataset;
    const {isSex}=this.data
    this.setData({ sex, sexIdx: sidx,isSex:!isSex })
    
  },
  /**
   * 选择年龄
   */
  setAge:function(e){
    const { idx, age } = e.currentTarget.dataset;
    this.setData({ age, ageIdx:idx})
    const { ageList}=this.data;
    var temp={};
    temp[`ageList[${idx}].status`] = ageList[idx].status == '' ? 'active' : ''
    this.setData(temp);
  },
  
  /**
   * 开始测试
   */
  setInfo:throttle(function(){
    const mobileExp = /^1[0-9]{10}$/;
    const postData = { fn: this.setInfo, param: { ...arguments } }
    const {sexIdx, age,name,phone,sex,school}=this.data;
    var that=this;
    console.log('dianji')
    if (!name) {
      wx.showToast({
        title: '请输入姓名',
        icon: 'none'
      })
      return
    }
    if (age>150) {
      wx.showToast({
        title: '年龄不能大于150',
        icon: 'none'
      })
      return
    }
    if (!age) {
      wx.showToast({
        title: '请输入年龄',
        icon: 'none'
      })
      return
    }
    if (!sex) {
      wx.showToast({
        title: '请选择性别',
        icon: 'none'
      })
      return
    }
    if (!school) {
      wx.showToast({
        title: '请输入学校',
        icon: 'none'
      })
      return
    }
    if (!phone) {
      wx.showToast({
        title: '手机号码不能为空',
        icon: 'none'
      })
      return
    } else if (!mobileExp.test(phone)) {
      wx.showToast({
        title: '手机号码位数不对',
        icon: 'none',
        duration: 1000
      })
      return
    }
    app.post('/api/game/v2/saveRespondentInfo', { sex:sexIdx+1, age,name,phone,school}, 0, postData).then(res => {
        const{gameRespondentId}=res.data
        that.redirectCtl({ url: 'tu_xing_tui_li', method: { level: 0, ageGroup:age,gameRespondentId } }, true)
    })
  },1000)
})