// pages/leaderRegistered/leaderRegistered.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    qrId:'',
    leaderTitle:'',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;

    this.setData({
      qrId: options.qrId,
    })
    //获取qr信息
    wx.request({
      url: 'https://www.koala-online.cn/compass/if/ajax_get_qr_info.php',
      data: {
        qrId: this.data.qrId,
      },
      header: { 'content-type': 'application/json' },
      method: 'GET',
      dataType: 'json',
      success: function (res) {
        var province = res.data.result.qrDat.province
        var city = res.data.result.qrDat.city
        var district = res.data.result.qrDat.district
        that.setData({
          leaderTitle: province + city + district,
        })

      },
      fail: function (res) { },
      complete: function (res) { },
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var config = wx.getStorageSync('config');
    if (config.registed == true && config.unionId !== undefined) {
      //跳转到首页
      wx.switchTab({
        url: '../index/index',
      });
    } else {
      setTimeout(function () {
        config = wx.getStorageSync('config');
        if (config.registed == true && config.unionId !== undefined) {
          //跳转到首页
          wx.switchTab({
            url: '../index/index',
          });
        }
      }, 1000);
    }
    
  },

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

  },

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

  },


  //提交注册
  regist(){
    var that = this;
    var config = wx.getStorageSync('config');
    // 获取用户信息
    wx.getUserInfo({
      success: res => {
        // 可以将 res 发送给后台解码出 unionId
        app.globalData.userInfo = res.userInfo
        // 由于 getUserInfo 是网络请求,可能会在 Page.onLoad 之后才返回
        // 所以此处加入 callback 以防止这种情况
        if (this.userInfoReadyCallback) {
          this.userInfoReadyCallback(res)
        }
        wx.request({
          url: app.url + 'ajax_get_unionid.php',
          data: {
            openid: config.openId,
            iv: res.iv,
            encryptedData: res.encryptedData

          },
          success(resp) {
            if (resp.data.status == 'OK') {
              wx.request({
                url: 'https://www.koala-online.cn/compass/if/ajax_leader_user_register.php',
                data: {
                  unionId: resp.data.result.unionId,
                  openId: config.openId,
                  headerImg: app.globalData.userInfo.avatarUrl,
                  qrId: that.data.qrId,
                },
                header: { 'content-type': 'application/json' },
                method: 'GET',
                dataType: 'json',
                success: function (res) {
                  //写入用户注册状态
                  if (res.data.status == 'OK') {
                    config.registed = true;
                    wx.setStorageSync("config", config);
                  }
                  
                  //跳转到年龄选择
                  wx.navigateTo({
                    url: '../ageChange/ageChange',
                  });
                },
                fail: function (res) { },
                complete: function (res) { },
              })
            }
          }
          });
      }
    })


  }
})