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

Component({
  properties: {
    currentHasUserInfo: {
      type: Boolean,
      type: false
    },
    personData: {
      type: Object,
      value: {},
    },
    integralNum: {
      type: Number,
      value: 0,
    },
    // cardMember: {
    //   type: Object,
    //   value: {},
    // },
    baseUserInfo: {
      type: Object,
      value: {},
      observer: '_userInfoChange'
    },
    couponNum: {
      type: Number,
      value: 0,
    },
    token: {
      type: String,
      observer(newVal) {
        this.setData({
          _token: newVal
        })
      }
    },
    isVip: {
      type: Boolean,
      type: false
    }
  },
  /**
   * 页面的初始数据
   */
  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'),
    isPlusVip: false,
    currentPlusStyle: null
  },
  observers: {
    '**': function (e) {
      // 每次 setData 都触发
    },
  },
  attached() {
    // console.log("isvip", this.data.isVip)
    const { imageUrl } = app.globalData
    const { personData, isVip } = this.data
    this.setData({
      personCenter: personData,
      baseImgUrl: imageUrl,
      isPlusVip: isVip,
      currentPlusStyle: isVip ? personData.plus.filter(item => item.purchased == 1)[0] : personData.plus.filter(item => item.purchased == 0)[0]
    })
  },
  methods: {
    toPlusPay(e) {
      const { row }= e.currentTarget.dataset
      let toPlus = typeof(row) == 'string'?  JSON.parse(row) : row
      if(toPlus.type == 2) {
       let pageId = toPlus.url
        wxService.router(`/subPackage/page/pages/subPage/subPage`).search({ pageId })
      }
    },
    _getUserInfo(res = {}) {
      wxService.nextTick(() => {
        this.triggerEvent('getAuth', res);
      })
    },
    _userInfoChange(newVal, oldVal) {
      if (newVal !== oldVal) {
        // console.log(newVal)
        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
  }
})