// pages/components/auth/auth.js
const api = require('../../../config');
const getReq = require('../../../config').getReq;
const ald = require('../../../utils/ald-stat.js')
import transformCode from '../../../utils/base64.js'
const wxauthen = require('../../../config.js').wxauthen



let app = getApp();
Component({
  /**
   * 组件的属性列表
   */
  properties: {

  },

  /**
   * 组件的初始数据
   */
  data: {
    phone_iv: '',
    phone_encryptedData: '',
    userInfoE: {},
    showAuthName: true,
    showAuthPhone: false,
  },

  lifetimes: {
    attached: function () {
      if (!app.globalData.userInfo) {
        this.setData({ showAuthName: true, showAuthPhone: false })
      }
      else if (!app.globalData.hasmobile) {
        this.setData({ showAuthName: false, showAuthPhone: true })
      }
    },
    detached: function () {
    },
  },
  /**
   * 组件的方法列表
   */
  methods: {
    userPhoneHandler: function (e) {
      if (e.detail.errMsg == "getPhoneNumber:ok") {
        wx.aldstat.sendEvent('wxAuthen', { phone: true })
        this.bindphone(e)
      }
    },
    userInfoHandler(e) {
      if (e.detail.errMsg == "getUserInfo:ok") {
        app.showLoading();
        app.globalData.userInfo = e.detail.userInfo;
        app.globalData.userInfo.nickname = e.detail.userInfo.nickName;
        let userInfo;
        if (app.globalData.userId.unionid) {
          userInfo = Object.assign({}, app.globalData.userId, app.globalData.userInfo);
        }
        else {
          console.log('wxauthen einfo is not empty');
          userInfo = e.detail.userInfo;
          let { encryptedData, iv, signature } = e.detail
          userInfo = Object.assign({}, { encryptedData }, { signature }, { iv }, userInfo)
        }

        let self = this;
        wxauthen(app, userInfo, (res) => {
          let datas = res.datas;
          app.globalData.member_id = datas.member_id
          app.globalData.hasmobile = datas.hasmobile

          if (datas.hasmobile == false) {
            self.setData({ showAuthName: false, showAuthPhone: true })
          }
          else {
            self.triggerEvent('getAuth', {
              userInfo: userInfo, phoneInfo: null
            })
          }
          app.hideLoading();
        }, (err) => {
          app.hideLoading();
          app.relogin();
          wx.showToast({
            icon: 'none',
            title: '登陆遇到一点麻烦，请重试~~~',
            duration: 2000
          })
        });
      }
    },

    bindphone: function (eInfo) {
      console.log('bindphone:', eInfo.detail)
      let phoneInfo = { iv: eInfo.detail.iv, encryptedData: eInfo.detail.encryptedData }
      let params = {
        phone_info: transformCode(JSON.stringify(phoneInfo)),
        act: "login",
        op: "wxbind_phone"
      }

      let self = this;

      app.showLoading();
      getReq(app, params, function (res) {
        app.hideLoading();
        if (res.code == 200) {
          let datas = res.datas;
          app.globalData.member_id = datas.member_id
          app.globalData.hasmobile = datas.hasmobile

          if (datas.hasmobile == false) {
            self.setData({ showAuthName: false, showAuthPhone: true })
          }
          else {
            self.triggerEvent('getAuth', {
              userInfo: null, phoneInfo: phoneInfo
            })
          }
        }
        else {
          app.relogin();
          wx.showToast({
            icon: 'none',
            title: '登陆遇到一点麻烦，请重试~~~',
            duration: 2000
          })
        }
      })

    }
  }
})
