// component/personCenter/personCenter.js
var app = getApp()
const wxService = require('../../utils/wxService')
const envInfo = require('../../config/index').envInfo

Component({
  properties: {
    personData: {
      type: Object,
      value: {},
    },
    integralNum: {
      type: Number,
      value: 0,
    },
    // cardMember: {
    //   type: Object,
    //   value: {},
    // },
    baseUserInfo: {
      type: Object,
      value: {},
      observer: '_userInfoChange'
      // observer(newVal, oldVal){
      //   console.log('---newVal',newVal,oldVal)
      //   if (newVal !== oldVal) {
      //     this.setData({
      //       memberActivateStatus: newVal && newVal.memberActivateStatus || false,
      //       memberCardNo: newVal && newVal.memberCardNo || '',
      //       currentMobile: newVal && newVal.member && newVal.member.mobile  || '',
      //     })
      //   }
      // }
    },
    couponNum: {
      type: Number,
      value: 0,
    },
    token: {
      type: String,
      observer(newVal) {
        this.setData({
          _token: newVal
        })
      }
    }
  },
  /**
   * 页面的初始数据
   */
  data: {
    personCenter: {},
    memberCardNo: "",
    memberActivateStatus: false,
    integralNum: 0,
    couponNum: 0,
    // baseUrl: '/assets/imgs/060610.png',
    baseUrl: '#02bb70',
    isAuthorization: false,
    _token: '',
    currentMobile: '',
    baseImgUrl: '',
    storgeUserInfo:  wx.getStorageSync('_baseUserInfo')
  },
  observers: {
    '**': function(e) {
      // 每次 setData 都触发
    },
  },
  attached() {
    this.setData({
      personCenter: this.data.personData,
      baseImgUrl: app.globalData.imageUrl
    })

    // let base64 = wx.getFileSystemManager().readFileSync(this.data.baseUrl, "base64");
    // this.setData({
    //   baseUrl: "data:image/png;base64," + base64
    // })
  },
  methods: {
    _userInfoChange(newVal, oldVal) {
      if (newVal !== oldVal) {
        this.setData({
          memberActivateStatus: newVal && newVal.memberActivateStatus || false,
          memberCardNo: newVal && newVal.memberCardNo || '',
          currentMobile: newVal && newVal.member && newVal.member.mobile || '',
        })
      }
    },
    handleToMemberOfOwn() {
      wxService.router(`/pages/memberOfOwn/memberOfOwn`)
    },
    toBonusDetails() {
      wxService.router(`/pages/integralWater/integralWater`)
    },
    toCouponList() {
      wxService.router(`/pages/coupons/coupons`)
    },
    handleTtoActiveMemebr() {
      if (wx.getStorageSync('_baseUserInfo')) {
        wxService.openCard()
      } else {
        this.setData({
          isAuthorization: true
        })
      }
    },
    handleGoWxCard() {
      wxService.openCard()
    }
  },
  detached() {
    this._userInfoChange = null
  }
})