// component/bottomTabs/bottomTabs.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
		},
		tabBar: {
			type: Object,
			value: null
		},
		currHomePageId: {
			type: String,
			value: ''
		}
	},
	attached() {
		this.setData({
			baseImgUrl: app.globalData.imageUrl
		})
		const { tabBar } = this.data

		let currentTab = tabBar.images
		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({ tabBarList: currentTab })
			})
		})
	},
	ready() {
		this.setData({
			currentRoute: `/${utils.getCurrentPageUrl()}`
		})
	},
	/**
	 * 组件的初始数据
	 */
	data: {
		currentRoute: '',
		type: 1,
		baseImgUrl: '',
		tabBarList: []
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		_getUserInfo(res = {}) {
			this.triggerEvent('getAuth', res);
		},
		handelToPage(event) {
			utilLink.currentLinkRouter(event)
		}
	}
})
