const wx2my = require('../../wx2my');
const Behavior = require('../../Behavior');
// component/personCenter/personCenter.js
var app = getApp();

const wxService = require('../../utils/wxService');

const envInfo = require('../../config/index').envInfo;

Component({
  props: {
    personData: {},
    integralNum: 0,
    baseUserInfo: {},
    couponNum: 0,
    token: null
  },

  /**
   * 页面的初始数据
   */
  data: {
    personCenter: {},
    memberCardNo: "",
    memberActivateStatus: false,
    // baseUrl: '/assets/imgs/060610.png',
    baseUrl: '#02bb70',
    isAuthorization: false,
    _token: '',
    currentMobile: '',
    baseImgUrl: '',
    // accreditUserInfo: ,
    avatar: '',
    nickName: ''
  },
  observers: {
    '**': function (e) {// 每次 setData 都触发
    }
  },
  onInit() {
    const accreditUserInfo = wx2my.getStorageSync('_accreditUserInfo') && wx2my.getStorageSync('_accreditUserInfo').data
    const userMember = this.props && this.props.baseUserInfo && this.props.baseUserInfo.member
    const {integralNum=0, couponNum=0} = this.props

    this.setData({
      avatar: accreditUserInfo && accreditUserInfo.avatar || '',
      nickName: accreditUserInfo && accreditUserInfo.nickName|| '',
      memberCardNo: userMember && userMember.code || '',
      currentMobile: userMember && userMember.mobile ||'',
      personCenter: this.props && this.props.personData,
      baseImgUrl: app.globalData.imageUrl
    })

  },
  didUpdate(prevProps, prevData) {
    const accreditUserInfo = wx2my.getStorageSync('_accreditUserInfo').data
    const {code, mobile} = prevProps && prevProps.baseUserInfo && prevProps.baseUserInfo.member
    const {avatar, nickName} = accreditUserInfo
    console.log('didUpdate', mobile)
    this.setData({
      avatar,
      nickName,
      memberCardNo: code || '',
      currentMobile: mobile ||'',
      personCenter: prevProps && prevProps.personData,
      baseImgUrl: app.globalData.imageUrl
    })
  },
  methods: {
    handleToMemberOfOwn() {
      wxService.router(`/pages/memberOfOwn/memberOfOwn`);
    },

    toBonusDetails() {
      wxService.router(`/pages/integralWater/integralWater`);
    },

    toCouponList() {
      wxService.router(`/pages/coupons/coupons`);
    },

    handleTtoActiveMemebr() {
      if (wx2my.getStorageSync('_baseUserInfo')) {
        wxService.openCard();
      } else {
        this.setData({
          isAuthorization: true
        });
      }
    },

    handleGoWxCard() {
      wxService.openCard();
    }

  },
  detached() {}

});