var app= getApp();
Page({
  data: {
    flag: true,
    second: '',
    disabled: true,
    luoData: {left: '',right: ''},//裸眼视力
    jiaoData: {left: '',right: ''},//矫正视力
    glassData: {left: '',right: ''},//眼镜度数
    ruoData: {left: '无',right: '无'},// 弱视
    xieData: {left: '无',right: '无'},// 斜视
    sanData: {left: '无',right: '无'},// 散光
    luoeyeArray: [
      ['4.0','4.1','4.2','4.3','4.4','4.5','4.6','4.7','4.8','4.9','-','5.0','5.1','5.2','5.3'],
      ['4.0','4.1','4.2','4.3','4.4','4.5','4.6','4.7','4.8','4.9','-','5.0','5.1','5.2','5.3']
    ],
    luoeyeIndex: [10, 10],
    jiaoArray: [
      ['4.0','4.1','4.2','4.3','4.4','4.5','4.6','4.7','4.8','4.9','-','5.0','5.1','5.2','5.3'],
      ['4.0','4.1','4.2','4.3','4.4','4.5','4.6','4.7','4.8','4.9','-','5.0','5.1','5.2','5.3']
    ],
    jiaoIndex: [10, 10],
    glassArray: [
      ['-','300度以下','300-600度','600度以上'],
      ['-','300度以下','300-600度','600度以上']
    ],
    glassIndex: [0, 0],
    ruoArray: [// 弱视选项
      ['无','中度','重度'],
      ['无','中度','重度']
    ],
    ruoIndex: [0, 0],
    xieArray: [// 斜视选项
      ['无','有'],
      ['无','有']
    ],
    xieIndex: [0, 0],
    sanArray: [// 散光选项
      ['无','<=100度','>100度'],
      ['无','<=100度','>100度']
    ],
    sanIndex: [0, 0],
    show: false,// 是否显示详细选项
    allLeft: "无",// 多个集合左
    allRight: "无",// 多个集合右
    allLeftStatus: 0,// 多个集合左(状态)
    allRightStatus: 0,// 多个集合右(状态)
    first: true,// 是否第一次点击下一步

    isClass: 0, //是否配镜
    date: '2012-01-01',// 配镜日期
  },
  onLoad: function (options) {
    let me = this;
  },
  eyesightBack: function() {
    let me = this;
    wx.navigateTo({
      url: '/pages/childInfo/childInfo'
    })
  },
  eyesightNext: function () {
    let me = this;
    // 是否第一次点击下一步
    if(me.data.first){
      // 如果是有弱视斜视散光，弹窗
      if(me.data.allLeft=='有'||me.data.allRight=='有'){
        let num = 5;
        me.setData({
          flag: false,
          disabled: true,
          second: '('+num+'s)'
        })
        let timer = setInterval(function () {
          num--;
          if (num <= 0) {
            clearInterval(timer);
            me.setData({
              second: '',
              disabled: false
            })
          } else {
            me.setData({
              second: '('+num+'s)',
              disabled: true
            })
          }
        }, 1000)
      }else{
        // 请求前加Loading
        wx.showLoading({
          title: '加载中',
          mask: true
        })
        // 模拟请求
        setTimeout(function () {
          // 请求成功后关闭Loading
          wx.hideLoading()

          // 第一次点击没有视觉缺陷
          console.log('第一次没有----下一步')
          me.next();
        }, 50)
      }
    }else{
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      // 模拟请求
      setTimeout(function () {
        // 请求成功后关闭Loading
        wx.hideLoading()

        // 弹窗后----下一步
        console.log('弹窗后----下一步')
        me.next();
      }, 50)
    }
  },
  // 下一步请求
  next: function(e){
    let me = this;
    // 请求前加Loading
    // wx.showLoading({
    //   title: '加载中',
    //   mask: true
    // })
    if(me.data.ruoData.left=='无'){
      me.setData({
        ruoData:{left: '',right: me.data.ruoData.right}
      })
    }
    if(me.data.ruoData.right=='无'){
      me.setData({
        ruoData:{left: me.data.ruoData.left,right: ''}
      })
    }
    if(me.data.xieData.left=='无'){
      me.setData({
        xieData:{left: '',right: me.data.xieData.right}
      })
    }
    if(me.data.xieData.right=='无'){
      me.setData({
        xieData:{left: me.data.xieData.left,right: ''}
      })
    }
    if(me.data.sanData.left=='无'){
      me.setData({
        sanData:{left: '',right: me.data.sanData.right}
      })
    }
    if(me.data.sanData.right=='无'){
      me.setData({
        sanData:{left: me.data.sanData.left,right: ''}
      })
    }

    let nearsightedTime = ''
    if(me.data.isClass==0){
      nearsightedTime = ''
    }else{
      nearsightedTime = me.data.date
    }
    wx.request({
      url: app.globalData.apiUrl + 'teenages/vision',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        leftEye: me.data.luoData.left,// 左眼裸眼视力
        rightEye: me.data.luoData.right,// 右眼裸眼视力
        leftCorrectedEye: me.data.jiaoData.left,// 左眼矫正视力
        rightCorrectedEye: me.data.jiaoData.right,// 右眼矫正视力
        leftGlassesDegree: me.data.glassData.left,// 左眼眼镜度数
        rightGlassesDegree: me.data.glassData.right,// 右眼眼镜度数
        leftOtherEyeStatus: me.data.allLeftStatus,// 左眼弱视、斜视、散光情况，1：有、0：无
        rightOtherEyeStatus: me.data.allRightStatus,// 右眼弱视、斜视、散光情况，1：有、0：无
        leftWeekEye: me.data.ruoData.left,// 左眼弱视程度
        rightWeekEye: me.data.ruoData.right,// 右眼弱视程度
        leftLeerEye: me.data.xieData.left,// 左眼斜视情况
        rightLeerEye: me.data.xieData.right,// 右眼斜视情况
        leftCylEye: me.data.sanData.left,// 左眼散光度数
        rightCylEye: me.data.sanData.right,// 右眼散光度数
        nearsightedStatus: me.data.isClass,// 佩戴眼镜情况 1：戴眼镜； 0：未戴眼镜
        nearsightedTime: nearsightedTime// 配镜时间
      },
      success: function (res) {
        // 请求成功后关闭Loading
        wx.hideLoading();
        if(res.data.errno==200){
          // 填写成功
          wx.showToast({
            title: '填写成功',
            mask: true,
            // icon: 'none',
            duration: 2000
          });
          wx.navigateTo({
            url: '/pages/setting/setting'
          });
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })

          if(me.data.ruoData.left==''){
            me.setData({
              ruoData:{left: '无',right: me.data.ruoData.right}
            })
          }
          if(me.data.ruoData.right==''){
            me.setData({
              ruoData:{left: me.data.ruoData.left,right: '无'}
            })
          }
          if(me.data.xieData.left==''){
            me.setData({
              xieData:{left: '无',right: me.data.xieData.right}
            })
          }
          if(me.data.xieData.right==''){
            me.setData({
              xieData:{left: me.data.xieData.left,right: '无'}
            })
          }
          if(me.data.sanData.left==''){
            me.setData({
              sanData:{left: '无',right: me.data.sanData.right}
            })
          }
          if(me.data.sanData.right==''){
            me.setData({
              sanData:{left: me.data.sanData.left,right: '无'}
            })
          }
        }
      }
    })
  },
  showList: function () {
    this.setData({
      show: !this.data.show
    })
  },
  eyesightKnow: function() {
    this.setData({
      flag: true,
      first: false
    })
    // let me = this;
  },
  luoeyeChange: function (e) {// 裸眼视力
    let luoData = {
      left: this.data.luoeyeArray[0][e.detail.value[0]],
      right: this.data.luoeyeArray[1][e.detail.value[1]]
    }
    this.setData({
      luoeyeIndex: e.detail.value,
      luoData: luoData
    })
    console.log('裸眼视力：')
    console.log(luoData)
  },
  jiaoChange: function (e) {// 矫正视力
    let jiaoData = {
      left: this.data.jiaoArray[0][e.detail.value[0]],
      right: this.data.jiaoArray[1][e.detail.value[1]]
    }
    this.setData({
      jiaoIndex: e.detail.value,
      jiaoData: jiaoData
    })
    console.log('矫正视力：')
    console.log(this.data.jiaoData)
  },
  glassChange: function (e) {// 矫正视力
    let glassData = {
      left: this.data.glassArray[0][e.detail.value[0]],
      right: this.data.glassArray[1][e.detail.value[1]]
    }
    this.setData({
      glassIndex: e.detail.value,
      glassData: glassData
    })
    console.log('眼镜度数：')
    console.log(this.data.glassData)
  },
  ruoChange: function (e) {// 弱视
    let ruoData = {
      left: this.data.ruoArray[0][e.detail.value[0]],
      right: this.data.ruoArray[1][e.detail.value[1]]
    }
    this.setData({
      ruoIndex: e.detail.value,
      ruoData: ruoData
    })
    // 调用判断整体状态
    this.setAll();
    console.log('弱视程度：')
    console.log(this.data.ruoData)
  },
  xieChange: function (e) {// 斜视
    let xieData = {
      left: this.data.xieArray[0][e.detail.value[0]],
      right: this.data.xieArray[1][e.detail.value[1]]
    }
    this.setData({
      xieIndex: e.detail.value,
      xieData: xieData
    })
    // 调用判断整体状态
    this.setAll();
    console.log('斜视情况：')
    console.log(this.data.xieData)
  },
  sanChange: function (e) {// 散光
    let sanData = {
      left: this.data.sanArray[0][e.detail.value[0]],
      right: this.data.sanArray[1][e.detail.value[1]]
    }
    this.setData({
      sanIndex: e.detail.value,
      sanData: sanData
    })
    // 调用判断整体状态
    this.setAll();
    console.log('散光度数：')
    console.log(this.data.sanData)
  },
  // 判断弱视斜视散光 有、无 状态
  setAll: function (e) {
    if(this.data.ruoData.left!="无"||this.data.xieData.left!="无"||this.data.sanData.left!="无"){
      this.setData({
        allLeft: '有',
        allLeftStatus: 1
      })
    }else{
      this.setData({
        allLeft: '无',
        allLeftStatus: 0
      })
    }
    if(this.data.ruoData.right!="无"||this.data.xieData.right!="无"||this.data.sanData.right!="无"){
      this.setData({
        allRight: '有',
        allRightStatus: 1
      })
    }else{
      this.setData({
        allRight: '无',
        allRightStatus: 0
      })
    }
  },
  // 是否配镜开关
  classSwitch(e) {
    console.log('是否配镜', e.detail.value)
    if(e.detail.value){
      this.setData({
        isClass: 1
      })
    }else{
      this.setData({
        isClass: 0
      })
    }
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
})