// shoppingGuid/page/pages/userInfo/userInfo.js
const wxService = require('../../../../utils/wxService')
const utils = require('../../../../utils/util')

wxService.page({

  /**
   * 页面的初始数据
   */
  data: {
    navBar: [
      { name: '自动标签', type: 1, isActive: true }, 
      { name: '手动标签', type: 2, isActive: false },
      { name: '订单信息', type: 3, isActive: false },
      { name: '退单信息', type: 4, isActive: false },
      { name: '优惠券', type: 5, isActive: false },
      { name: '积分', type: 6, isActive: false },
    ],
    current: 1,
    onlineOrderStatus: [{ name: '线上订单', type: 1, isActive: true }, { name: '线下订单', type: 2, isActive: false }],
    returnOrderStatus: [{ name: '线上订单', type: 1, isActive: true },{ name: '线下订单', type: 2, isActive: false }],
    memberInfo: null
  },

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

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getMemberFind() // 会员详情
  },
  getMemberFind() {
    const baseUserInfo = wx.getStorageSync('_baseUserInfo')
    const { memberId,member } = baseUserInfo
    wx.showLoading({
      title: '加载中'
    })
    const memberInfoFindDto  = {
      memberId,
      mobile: member.mobile,
      cardNoOnline: '',
      alipayUserId: '',
      cardNoOffline: '',
      unionid: member.unionid,
      openid: member.openid,
 
    }
    wxService.post(`/member/find`, memberInfoFindDto).then(res => {
      if (!res) return false
      const { result, data } = res && res.data || res && res.result
      if (result == 0) {
        wx.hideLoading()
        data.member.age = utils.getAge(data.member.birthday)
        this.setData({
          memberInfo: data.member
        })

      }
    })
  },
  handelPhoneCall(e) {
    const {phone} = e.currentTarget.dataset
    wx.makePhoneCall({
      phoneNumber: phone
    })
  },
  //点击事件
  //类型切换
  onTopBarTap(e){
    let index = e.currentTarget.dataset.index,
      item = e.currentTarget.dataset.item;
    this.data.navBar.forEach(nav => {
      nav.isActive = false;
    });

    this.data.navBar[index].isActive = true;
    this.data.current = this.data.navBar[index].type;

    this.setData({
      navBar: this.data.navBar,
      current: this.data.current
    });
  },
  //线上订单状态切换
  onTapChangeOnlineOrderStatus(e){
    let index = e.currentTarget.dataset.index,
      item = e.currentTarget.dataset.item;
    this.data.onlineOrderStatus.forEach(nav => {
      nav.isActive = false;
    });

    this.data.onlineOrderStatus[index].isActive = true;

    this.setData({
      onlineOrderStatus: this.data.onlineOrderStatus,
    });
  }, 
  //退单
  onTapChangeReturnOrderStatus(e){
    let index = e.currentTarget.dataset.index,
      item = e.currentTarget.dataset.item;
    this.data.returnOrderStatus.forEach(nav => {
      nav.isActive = false;
    });

    this.data.returnOrderStatus[index].isActive = true;

    this.setData({
      returnOrderStatus: this.data.returnOrderStatus,
    });
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

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

  }
})