// pages/contest/contest.js
import {
  imine
} from '../../../api/api.js'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    sexList: [{label:'男',value:1},{label:'女',value:2}],
    form: {schoolName:''},
    areaList: [],
    sortList: [
      {name:'actualName',title:'姓名'},
      {name:'sex',title:'性别'},
      {name:'birthday',title:'生日'},
      {name:'area',title:'区域'},
      {name:'school',title:'学校'},
      {name:'grade',title:'年级'}
    ],
    ageList: [],
    gradeList: [
      {label:'一年级',value:'一年级'},
      {label:'二年级',value:'二年级'},
      {label:'三年级',value:'三年级'},
      {label:'四年级',value:'四年级'},
      {label:'五年级',value:'五年级'},
      {label:'六年级',value:'六年级'},
      {label:'七年级',value:'七年级'},
      {label:'八年级',value:'八年级'},
      {label:'九年级',value:'九年级'},
      {label:'高一',value:'高一'},
      {label:'高二',value:'高二'},
      {label:'高三',value:'高三'}
    ],
    schoolList: [],
    jgList: [true,true,true,true,true,true],
    statusBar: 0
  },

  goHome (e) {
    wx.reLaunch({
      url: '/pages/home/home',
    })
  },

  changeInput (e) {
    const id = e.currentTarget.id;
    let value = (id === 'actualName' ? e.detail.value.replace(/[^\u4E00-\u9FA5]/g, '') : e.detail.value)
    this.setData({
      ['form.'+id]: value
    })
  },

  bindAddrChange (e) {
    const id = e.currentTarget.id;
    const value = e.detail.value
    this.setData({
      ['form.'+id]: value.join(' ')
    })
  },

  bindPickerData (e) {
    const id = e.currentTarget.id;
    const value = this.formatYMD(e.detail.value)
    this.setData({
      ['form.'+id]: value
    })
  },

  formatYMD (time) {
    let date = new Date(time)
    let y = date.getFullYear()
    let m = date.getMonth() + 1
    m = m < 10 ? '0' + m : m
    let d = date.getDate()
    d = d < 10 ? '0' + d : d
    return y + '-' + m + '-' + d
  },

  bindPicker (e) {
    const id = e.currentTarget.id;
    const list = e.currentTarget.dataset.list
    const value = e.detail.value
    this.setData({
      ['form.'+id]: this.data[list][value].value
    })
  },

  bindPickerSchool (e) {
    const id = e.currentTarget.id;
    const value = e.detail.value
    this.setData({
      ['form.'+id]: this.data.schoolList[value].id,
      ['form.schoolName']: this.data.schoolList[value].schoolName
    })
  },

  bindPickerChange(e) {
    const id = e.currentTarget.id;
    const value = e.detail.value
    this.setData({
      ['form.'+id]: this.data.areaList[value].id,
      ['form.areaName']: this.data.areaList[value].areaName
    }, () => {
      imine.querySchoolByAreaId({reqdata:{
        id: this.data.form[id]
      }},r => {
        let list = r.list
        let index = list.findIndex(item=>item.schoolName.includes('其他'))
        let obj = list.splice(index,1)
        list.push(obj[0])
        this.setData({
          schoolList: list,
          ['form.school']: null,
          ['form.schoolName']: ''
        })
      })
    })
  },

  submit (e) {
    let {actualName,sex,birthday,area,grade,other,school} = this.data.form
    let {sortList} = this.data
    this.setData({
      jgList: sortList.map(item => !!this.data.form[item.name])
    }, () => {
      if (!sortList.every(item => this.data.form[item.name])) {
        console.log(this.data.jgList);
        return
      }
      let id = wx.getStorageSync('id')
      imine.updateUser({reqdata:{actualName,sex,birthday,area,grade,other,school,id}},r => {
        this.loadMyinformation()
        wx.showModal({
          title: '答题须知：',
          content: '1.每人每天最多答题三次\n\n 2.排行榜以得分情况计算',
          confirmColor: '#ff0000',
          cancelText: '返回',
          success (res) {
            if (res.confirm) {
              wx.navigateTo({
                url: '/pages/contest/answer/answer'
              })
            } else if (res.cancel) {
              wx.reLaunch({
                url: '/pages/home/home',
              })
            }
          }
        })
      })
    })
  },

  queryAllArea () {
    imine.queryAllArea({},r => {
      this.setData({
        areaList: r.list
      })
    })
  },

  loadMyinformation() {
    let id = wx.getStorageSync('id')
    imine.loadMyinformation({reqdata: {id}},r => {
      wx.setStorageSync('user', r.object)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryAllArea()
    wx.getSystemInfo({
      success: e => {
        this.setData({
          statusBar: e.statusBarHeight + 13
        })
      }
    })
    let {ageList} = this.data

    for (let index = 0; index < 100; index++) {
      // const element = array[index];
      ageList[index] = { label: index+1+'岁',value: index+1}
    }
    this.setData({
      ageList,
      endDate: new Date().format('yyyy-MM-dd')
    })
  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  },

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

  }
})