
// component/picNav/picNav.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
		},
		picNav: {
			type: Array,
			value: [],
		},
	},
	attached() {
		this.setData({
			baseImgUrl: app.globalData.imageUrl
		})
		
	},
	observers: {
		picNav() {
			const { picNav } = this.data
			picNav.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 不需要
			picNav.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({ newPicNav: picNav })
				})
			})
		}
  },
	/**
	 * 组件的初始数据
	 */
	data: {
		imagewidth: 0,//缩放后的宽 
		imageheight: 0,//缩放后的高 
		isAuthorization: false,
		isAuthorizationActiveText: '',
		isAuthorizationType: 2,
		baseImgUrl: '',
		newPicNav: []
	},
	/**
	 * 组件的方法列表
	 */
	methods: {
		_getUserInfo(res = {}) {
			wxService.nextTick(() => {
				this.triggerEvent('getAuth', res);
			})
		},
		imageLoad(e) {
			let imageSize = utils.imageUtil(e)
			this.setData({
				imagewidth: imageSize.imageWidth,
				imageheight: imageSize.imageHeight
			})
		},
		preview(event) {
			utilLink.currentLinkRouter(event, this)
		},
		activeTocard(e) {
			if (e.detail) {
				wxService.openCard()
			}
		}
	}
})
