var app = getApp();
Page({
  data: {
    name: '',//姓名
    date: '2010-01-01',// 生日
    sex: 1,// 性别
    sexList: [{ id: 1, checked: true, name: '男孩' }, { id: 2, name: '女孩' }],
    isNull: false
  },
  onLoad: function (options) {
    let me = this;
  },
  bindDateChange: function(e) {
    this.setData({
      date: e.detail.value
    })
  },
  // 改变性别
  sexTap: function(e) {
    this.setData({
      sex: e.detail.value
    })
    console.log(e.detail.value)
  },
  nameInput: function (e) {
    this.setData({
      isNull: false,
      name: e.detail.value
    })
  },
  // 下一步
  childInfoNext: function (e) {
    console.log(11111)
    // 授权
    if(e.detail.errMsg!='getUserInfo:ok'){
      //拒绝授权
      console.log(e.detail.errMsg)
      return
    }
    //授权成功后
    console.log(e.detail.userInfo)

    wx.request({
      url: app.globalData.apiUrl + '/users/addSmallProgramUser',
      method: 'POST',
      data: {
        token: wx.getStorageSync('token'),
        avatarUrl: e.detail.userInfo.avatarUrl,
        city: e.detail.userInfo.city,
        country: e.detail.userInfo.country,
        gender: e.detail.userInfo.gender,
        language: e.detail.userInfo.language,
        nickName: e.detail.userInfo.nickName,
        province: e.detail.userInfo.province
      },
      success: function (res) {
        if(res.data.errno==200){
        }else{
          wx.showModal({
            title: '提示',
            showCancel: false,
            content: res.data.msg
          })
        }
      }
    })

    let me = this;
    if(me.data.name==''){// 没填姓名高亮
      me.setData({
        isNull: true
      })
      wx.showToast({
        title: '姓名不能为空！',
        icon: 'none'
      })
    } else{
      // 请求前加Loading
      wx.showLoading({
        title: '加载中',
        mask: true
      })
      wx.request({
        url: app.globalData.apiUrl + 'teenages/info',
        method: 'POST',
        data: {
          token: wx.getStorageSync('token'),
          name: me.data.name,
          birthday: me.data.date,
          gender: me.data.sex,//性别
        },
        success: function (res) {
          // 请求成功后关闭Loading
          wx.hideLoading();
          if(res.data.errno==200){
            // 绑定成功
            wx.showToast({
              title: '填写成功',
              mask: true,
              // icon: 'none',
              duration: 2000
            });
            wx.navigateTo({
              url: '/pages/eyesight/eyesight'
            })
          }else{
            wx.showModal({
              title: '提示',
              showCancel: false,
              content: res.data.msg
            })
          }
        }
      })
    }
  },
})