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

Component({
	/**
	 * 组件的属性列表
	 */
	properties: {
		currentHasUserInfo:{
      type: Boolean,
      type: false
    },
		categoryType: {
			type: Object,
			value: null
		}
	},
	attached() {
		const { categoryType } = this.data

		let currentTab = categoryType && categoryType.categorys || []
		currentTab.forEach(item => {
			// 存在 row 处理数据 8 商品 9 分类
			let newRow = null
			if (item.link) {
				if (item.link.type == 2) {
					// 父组件 更新
					const urls = getCurrentPages()
					const currentPath = urls[0]
					const pageId = item.link.url
					item.link.newPath = `/${currentPath.route}`
				}
				return
			} else if (item.row) {
				newRow = JSON.parse(item.row)
				if (newRow.type == 8) {
					newRow.newPath = '/pages/productDetail/productDetail'
				} else if (newRow.type == 9) {
					newRow.newPath = '/subPackage/page/pages/categoryPro/categoryPro'
				} 
				item.row = newRow
			}
		})

		// 当前匹配路径是否需要授权 true 需要 false 不需要
		currentTab.forEach(item => {
			let checkUrl = ''
			if (item.link) {
				checkUrl = item.link.newPath ? item.link.newPath : item.link.url
			} else if (item.row) {
				checkUrl = item.row.newPath ? item.row.newPath : item.row.url
			}
			wxService.NoLoginPage(checkUrl).then(res => {
				if(checkUrl)  item.needAuth = res.needAuth
				this.setData({ categoryList: currentTab })
			})
		})
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		// currentTab: 0,
		currentTab: 99,
		isAuthorization: false, // false 不显示 true 显示
		categoryList:[]
	},
  /*组件所在页面的生命周期 */
  pageLifetimes: {
    show: function () {
      // 页面被展示
      this.setData({
        currentTab: 99
      })
    },
    hide: function () {
      // 页面被隐藏
      this.setData({
        currentTab: 99
      })
    },
   
  },
	/**
	 * 组件的方法列表
	 */
	methods: {
		_getUserInfo(res = {}) {
			wxService.nextTick(() => {
				this.triggerEvent('getAuth', res);
			})
		},
		handelToVipCode() {
			wxService.router(`/pages/memberOfOwn/memberOfOwn`)
		},
		handelToCart() {
			wxService.router(`/pages/cart/cart`)
		},
		// 设置tab active
		switchNav(event) {
			utilLink.currentLinkRouter(event, this)
		},
		switchTab(event) {
			var cur = event.detail.current
			this.setData({
				currentTab: cur
			})
		}
	}
})
