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

Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		currentHasUserInfo:{
      type: Boolean,
      type: false
    },
    tabBar:{
      type: Object,
      value: null
		},
		currHomePageId: {
			type: String,
			value: ''
		}
  },
	ready() {
		this.setData({
			currentRoute: `/${utils.getCurrentPageUrl()}`
		})
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		currentRoute: '',
		tabs: [],
    type: 1
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		_getUserInfo(res = {}) {
      const userInfo = res.detail || {}
      console.log('userInfo',userInfo)
      // 先授权再领卡
      if (res.detail.userInfo) {
        utils.getUserInfoByBtn(userInfo).then( (data = {} ) => {
          const {token} = data
          wx.setStorageSync('_accreditUserInfo', userInfo)
          wx.hideNavigationBarLoading()
          if (token) {
            wx.setStorageSync('token', token)
          } else {
            wx.setStorageSync('token', '')
          }
          wx.setStorageSync('_baseUserInfo', data) // 缓存用户信息
          this.setUserInfo()   // 判断有无开卡
        }).catch(err=> {
          if (err) {
            wx.showToast({
              title: `会员系统异常请稍后重试!`,
              icon: 'none'
            })
          }
        })
      }
    },
    setUserInfo () {
      const userInfo = wx.getStorageSync('_baseUserInfo')
      // 新用户去激活领卡 
      if (userInfo && userInfo.member && !userInfo.member.mobile) {
        console.log('去开')
        wxService.openCard()
        return false
      }else {
        console.log('已开')
      }
      // 跳转
      const urls = getCurrentPages()
      setTimeout(() => {
        const item = urls[0].options
        if (item.route) {
          try {
            const options = JSON.parse(item.options)
            wxService.router(`/${item.route}`).search(options).replace()
          } catch (e) {
            wxService.router('/pages/userCenter/userCenter').replace()
          }
        } else {
          wxService.router('/pages/userCenter/userCenter').replace()
        }
      }, 200)
    },
    handelToPage(event){
      const ev = event.currentTarget.dataset;
      let curItemType = ev.item
			const userInfo = wx.getStorageSync('_baseUserInfo')
		
			// link row
			if (curItemType.link) {
				let currentClickType = app.globalData.commonFunc.getLink(curItemType.link.type, curItemType)

				if (curItemType.link.type == 1) {
					// 触发父组件更新页面 外部小程序
					
				} else if (curItemType.link.type == 2) {
					// 触发父组件更新页面
					wxService.nextTick(() => {
						this.triggerEvent('updatePage', { pageId: currentClickType.url })
					})
				} else if (curItemType.link.type == 3) {
					// 预览
					wx.previewImage({
						current: `${app.globalData.imageUrl}/${currentClickType.imageUrl}`, // 当前显示图片的http链接
						urls: [`${app.globalData.imageUrl}/${currentClickType.link.url}`]  // 需要预览的图片http链接列表
					})
				} else if (curItemType.link.type == 4) {
					wxService.router(`${currentClickType}`)
				} else if (curItemType.link.type == 5) {
					if (currentClickType == 1) {
						wxService.openCard()
					} else if (currentClickType == 2) {
						wxService.openCard()
					}
				} else if (curItemType.link.type == 6) {
					wxService.router(`/${currentClickType}`)
				} else if (curItemType.link.type == 7) {
					// 集点详情
				  wxService.router(`${currentClickType}`)
				}
      } else if (curItemType.row) {
        let rowObj = JSON.parse(curItemType.row)
				let currentClickRow = app.globalData.commonFunc.getLinkRow(rowObj.type, rowObj)

				if (rowObj.type == 1) { }
				else if (rowObj.type == 2) { }
				else if (rowObj.type == 3) { }
				else if (rowObj.type == 4) { }
				else if (rowObj.type == 5) { }
				else if (rowObj.type == 6) { }
				else if (rowObj.type == 7) { }
				else if (rowObj.type == 8) {
					// 商品
					wxService.router(`/pages/productDetail/productDetail?productId=${currentClickRow}`)
				}
				else if (rowObj.type == 9) { 
					// 分类
					wxService.router(`/subPackage/page/pages/categoryPro/categoryPro?${currentClickRow}`)
				}
      }
    }
  }
})
